From ca463bab52ae55ff8b5b964047480240176c4dc2 Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:59:06 +0100 Subject: [PATCH] cli: report failures after keep-going scripts (#2836) * cli: report failures after keep-going scripts Keep executing requested steps while retaining the first failure in Report, so failed scripts return nonzero before save/export. * docs: describe first script failure in Report Describe the first failing step without assuming all callers stop immediately. * test: cover keep-going failure reporting and output guards Exercise failures before and after a successful step, preserve the first failure, and require both output paths to remain absent. --- apps/designcraft-cli/src/main.rs | 10 ++++--- apps/designcraft-cli/tests/it/cli.rs | 40 +++++++++++++++++++++++++++- crates/engine/src/script.rs | 2 +- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/apps/designcraft-cli/src/main.rs b/apps/designcraft-cli/src/main.rs index 1e39e906e..84b249590 100644 --- a/apps/designcraft-cli/src/main.rs +++ b/apps/designcraft-cli/src/main.rs @@ -692,11 +692,15 @@ fn script(args: &[String]) -> Result<(), String> { let report = if keep_going { // Run every step; failures become {"error": …} results. let mut results: Vec = Vec::new(); - for st in &steps { + let mut failed = None; + for (i, st) in steps.iter().enumerate() { let r = designcraft_engine::script::resolve(&st.params, &results).and_then(|p| exec(b, &st.command, p)); - results.push(r.unwrap_or_else(|e| json!({"error": e, "command": st.command}))); + results.push(r.unwrap_or_else(|e| { + failed.get_or_insert_with(|| (i, st.command.clone(), e.clone())); + json!({"error": e, "command": st.command}) + })); } - designcraft_engine::script::Report { results, failed: None } + designcraft_engine::script::Report { results, failed } } else { designcraft_engine::script::run(&steps, |id, p| exec(b, id, p)) }; diff --git a/apps/designcraft-cli/tests/it/cli.rs b/apps/designcraft-cli/tests/it/cli.rs index b45c1d449..7fe1d9cea 100644 --- a/apps/designcraft-cli/tests/it/cli.rs +++ b/apps/designcraft-cli/tests/it/cli.rs @@ -151,11 +151,49 @@ fn script_export_of_the_document_in_the_running_app_saves_a_copy() { #[test] fn failing_script_exits_nonzero_with_the_step() { let mut child = cli().args(["script", "-"]).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap(); - child.stdin.take().unwrap().write_all(b"document.inspect\nno.such.command\n").unwrap(); + child.stdin.take().unwrap().write_all(b"document.inspect\nno.such.command\ndocument.inspect\n").unwrap(); let out = child.wait_with_output().unwrap(); assert!(!out.status.success()); let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); assert_eq!(v["failedIndex"], 1); + assert_eq!(v["completed"], 1); +} + +#[test] +fn keep_going_reports_the_first_failure_and_does_not_write_outputs() { + struct Outputs([std::path::PathBuf; 2]); + impl Drop for Outputs { + fn drop(&mut self) { + for path in &self.0 { + let _ = std::fs::remove_file(path); + } + } + } + let root = std::env::temp_dir(); + let name = format!("dc-cli-keep-going-{}", std::process::id()); + let paths = [root.join(format!("{name}.designcraft")), root.join(format!("{name}.png"))]; + assert!(paths.iter().all(|path| !path.exists())); + let outputs = Outputs(paths); + + let mut child = cli() + .args(["script", "-", "--keep-going", "--save", outputs.0[0].to_str().unwrap(), "--export", outputs.0[1].to_str().unwrap()]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + child.stdin.take().unwrap().write_all(b"no.such.command\ndocument.inspect\nno.other.command\n").unwrap(); + let out = child.wait_with_output().unwrap(); + + assert!(!out.status.success()); + let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); + assert_eq!(v["completed"], 3); + assert_eq!(v["failedIndex"], 0); + assert_eq!(v["failedCommand"], "no.such.command"); + assert!(v["results"][0]["error"].is_string()); + assert!(v["results"][1].is_object() && v["results"][1]["error"].is_null()); + assert!(v["results"][2]["error"].is_string()); + assert!(outputs.0.iter().all(|path| !path.exists()), "--save and --export require a successful script"); } #[test] diff --git a/crates/engine/src/script.rs b/crates/engine/src/script.rs index 1ccd6f29c..defaec3bd 100644 --- a/crates/engine/src/script.rs +++ b/crates/engine/src/script.rs @@ -209,7 +209,7 @@ fn resolve_in(params: &Value, results: &[Value], max_ref: usize, budget: &mut us #[derive(Clone, Debug, PartialEq)] pub struct Report { pub results: Vec, - /// The failing step (index, command, message); later steps didn't run. + /// The first failing step (index, command, message), if any. pub failed: Option<(usize, String, String)>, }