diff --git a/src/core/processor/executor.rs b/src/core/processor/executor.rs index 5c238f47..ea10a236 100644 --- a/src/core/processor/executor.rs +++ b/src/core/processor/executor.rs @@ -298,11 +298,6 @@ impl PythonExecutor { tmp.set_extension("json.tmp"); tmp }); - if let (Some(src), Some(dst)) = (&output_path, &tmp_path) { - if src.exists() { - let _ = std::fs::rename(src, dst); - } - } let mut cmd = Command::new(&self.python_path); cmd.env("MOMENTRY_OUTPUT_DIR", &*OUTPUT_DIR); @@ -321,7 +316,16 @@ impl PythonExecutor { } cmd.arg(&script_path); - for arg in args { + for (i, arg) in args.iter().enumerate() { + // Route the script's declared output path through a .tmp file so the + // fresh result is written there and promoted to the real path only on + // success (avoids clobbering good output with a stale/partial file). + if i == output_idx { + if let Some(ref tmp) = tmp_path { + cmd.arg(tmp); + continue; + } + } cmd.arg(arg); } @@ -446,11 +450,6 @@ impl PythonExecutor { tmp.set_extension("json.tmp"); tmp }); - if let (Some(src), Some(dst)) = (&output_path, &tmp_path) { - if src.exists() { - let _ = std::fs::rename(src, dst); - } - } let mut cmd = Command::new(&self.python_path); cmd.env("MOMENTRY_OUTPUT_DIR", &*OUTPUT_DIR); @@ -469,7 +468,16 @@ impl PythonExecutor { } cmd.arg(&script_path); - for arg in args { + for (i, arg) in args.iter().enumerate() { + // Route the script's declared output path through a .tmp file so the + // fresh result is written there and promoted to the real path only on + // success (avoids clobbering good output with a stale/partial file). + if i == output_idx { + if let Some(ref tmp) = tmp_path { + cmd.arg(tmp); + continue; + } + } cmd.arg(arg); } @@ -634,11 +642,6 @@ impl PythonExecutor { tmp.set_extension("json.tmp"); tmp }); - if let (Some(src), Some(dst)) = (&output_path, &tmp_path) { - if src.exists() { - let _ = std::fs::rename(src, dst); - } - } let mut cmd = Command::new(&self.python_path); cmd.env("MOMENTRY_OUTPUT_DIR", &*OUTPUT_DIR); @@ -654,7 +657,16 @@ impl PythonExecutor { } cmd.arg(&script_path); - for arg in args { + for (i, arg) in args.iter().enumerate() { + // Route the script's declared output path through a .tmp file so the + // fresh result is written there and promoted to the real path only on + // success (avoids clobbering good output with a stale/partial file). + if i == output_idx { + if let Some(ref tmp) = tmp_path { + cmd.arg(tmp); + continue; + } + } cmd.arg(arg); }