From b4c5f4da0eefec7ced90dbea6730f562cc9a18d3 Mon Sep 17 00:00:00 2001 From: durtyrobbin <76844303+durtyrobbin@users.noreply.github.com> Date: Tue, 20 Oct 2026 23:08:08 -0400 Subject: [PATCH] Print: keep dry runs out of live printer discovery --- crates/engine/src/print_cmds.rs | 25 ++++++++++++++++++++----- crates/engine/src/print_cmds/tests.rs | 11 +++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 9b264f0611..5627b65945 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -191,6 +191,7 @@ fn cups_spool(job: &PrintJob) -> std::result::Result { /// `craft-print` (`lpstat`, the Windows spooler) where that is the route; over IPP for a named /// server and where there is no `lp` (a sandbox). fn system_printers(_s: &mut Session, p: &Value) -> Result { + require_live_printer_query(p)?; let named = p.get("printServer").and_then(Value::as_str).filter(|v| !v.is_empty()); if named.is_none() && system_spooler().is_some() { let list = craft_print::printers(); @@ -854,7 +855,18 @@ fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: u32) -> Resul ) } -fn printers(_: &mut Session, _: &Value) -> Result { +// Driver discovery/settings are external I/O, even when no page is spooled. A dry run must +// not block on a disconnected printer or a print server (including in the command panic hunt). +fn require_live_printer_query(p: &Value) -> Result<()> { + if p.get("dryRun").and_then(Value::as_bool).unwrap_or(false) { + Err(other("printer queries are unavailable in a dry run; omit dryRun to query the live printer backend")) + } else { + Ok(()) + } +} + +fn printers(_: &mut Session, p: &Value) -> Result { + require_live_printer_query(p)?; match craft_print::rgb::printers() { Ok(list) => Ok(json!({"supported": true, "printers": list.iter().map(|p| json!({"name": p.name, "default": p.default})).collect::>()})), Err(craft_print::rgb::PrintError::Unsupported) => Ok(json!({"supported": false, "printers": []})), @@ -863,6 +875,7 @@ fn printers(_: &mut Session, _: &Value) -> Result { } fn printer_page(_: &mut Session, p: &Value) -> Result { + require_live_printer_query(p)?; // Checked before the printer, so a bad value is the error whether or not a printer answers. let landscape = match p.get("orientation").filter(|v| !v.is_null()) { None => None, @@ -920,7 +933,9 @@ fn print_one_copy(s: &mut Session, p: &Value) -> Result { let mut q = last_print(s).unwrap_or_else(|| json!({})); // A printer remembered from an earlier session may be gone: then the default takes the job, // as the Print dialog falls back (#7410). A list that can't be read decides nothing. - if let Ok(listed) = system_printers(s, &q) { + if !p.get("dryRun").and_then(Value::as_bool).unwrap_or(false) + && let Ok(listed) = system_printers(s, &q) + { let names: Vec<&str> = listed["printers"].as_array().into_iter().flatten().filter_map(|p| p["name"].as_str()).collect(); drop_vanished_printer(&mut q, &names); } @@ -1107,7 +1122,7 @@ pub fn specs() -> Vec { label: "Printers", menu: &[], shortcut: None, - params: r##"{} → {supported, printers: [{name, default}]} (supported: Windows; elsewhere Print spools with lp)"##, + params: r##"{"dryRun":bool=false} → {supported, printers: [{name, default}]} (supported: Windows; elsewhere Print spools with lp; dryRun refuses live device queries)"##, enabled: |_| Ok(()), journal: false, run: printers, @@ -1117,7 +1132,7 @@ pub fn specs() -> Vec { label: "Printer Page", menu: &[], shortcut: None, - params: r##"{"printer":name? (default printer),"printerSettings":hex?,"orientation":"portrait|landscape"? (turn the settings, or the printer's own, that way)} → {printer, paper: [w,h] pt, printable: [x,y,w,h] pt from the top-left, dpi: [x,y], printerSettings: hex (only with orientation: the turned settings)} (Windows)"##, + params: r##"{"printer":name? (default printer),"printerSettings":hex?,"orientation":"portrait|landscape"? (turn the settings, or the printer's own, that way),"dryRun":bool=false (refuse live device queries)} → {printer, paper: [w,h] pt, printable: [x,y,w,h] pt from the top-left, dpi: [x,y], printerSettings: hex (only with orientation: the turned settings)} (Windows)"##, enabled: |_| Ok(()), journal: false, run: printer_page, @@ -1127,7 +1142,7 @@ pub fn specs() -> Vec { label: "Printers", menu: &[], shortcut: None, - params: r##"{} → {printers:[{name, default}], default:name|null} (the system spooler's printers; a name is Print's "printer")"##, + params: r##"{"printServer":str?,"dryRun":bool=false} → {printers:[{name, default}], default:name|null} (the system spooler's printers; a name is Print's "printer"; dryRun refuses live device queries)"##, enabled: crate::commands::always, journal: false, run: system_printers, diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index 38567ffb6d..38bd2d1286 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -869,3 +869,14 @@ fn windows_pdf_component_is_detected_by_loading_its_dll() { fn the_windows_pdf_check_is_a_no_op_elsewhere() { assert!(require_windows_pdf().is_ok()); } + +#[test] +fn printer_queries_refuse_dry_runs_before_contacting_a_backend() { + let mut s = Session::new(); + for command in ["file.printers", "file.print.printers", "file.print.page"] { + for params in [json!({"dryRun": true}), json!({"dryRun": true, "printServer": "127.0.0.1:1", "printer": "Synthetic unavailable printer"})] { + let error = s.execute(command, params).unwrap_err().to_string(); + assert!(error.contains("printer queries are unavailable in a dry run"), "{command}: {error}"); + } + } +}