Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(shell_exec): write small command input to a pipe, not a temp file (#4519)
Maximilian Roos6826904e3d0b
diff --git a/src/commands/picker/pager.rs b/src/commands/picker/pager.rsindex bb17cd349..52edd3920 100644--- a/src/commands/picker/pager.rs+++ b/src/commands/picker/pager.rs@@ -1,7 +1,7 @@//! Pager detection and execution.//!//! Handles detection and use of diff pagers (delta, bat, etc.) for preview windows.-//! Input is prepared in an anonymous file before spawn. On Unix, one deadline+//! Input is prepared by `buffered_stdin` before spawn. On Unix, one deadline//! bounds both output consumption and the direct child wait,//! including descendants retaining the pipes. Timeout closes our pipe endpoints//! and kills/reaps only the owned pager; preview falls back to the original text.diff --git a/src/shell_exec.rs b/src/shell_exec.rsindex 11d914988..0820d85a4 100644--- a/src/shell_exec.rs+++ b/src/shell_exec.rs@@ -89,9 +89,22 @@ use crate::trace::CommandTrace;pub mod pipe;/// Prepare complete input before spawning, with no producer that can block.-/// The anonymous file is removed when its last handle closes, even on interruption.-/// This is seekable stdin, unlike the pipes connecting live pipeline stages.+///+/// On Unix, input that fits in a pipe's buffer is written there before spawn,+/// so it needs no temporary storage and works with an unusable `TMPDIR`.+/// Larger input, and all input on Windows, goes through an anonymous file,+/// removed when its last handle closes, even on interruption.pub fn buffered_stdin(bytes: &[u8]) -> std::io::Result<Stdio> {+ #[cfg(unix)]+ {+ let (reader, mut writer) = with_process_creation_guard(std::io::pipe)?;+ pipe::set_nonblocking(&writer)?;+ // Writing to a fresh pipe whose reader we hold fails only with+ // `WouldBlock`, once the input outgrows the buffer.+ if writer.write_all(bytes).is_ok() {+ return Ok(reader.into());+ }+ }let mut file = tempfile::tempfile()?;file.write_all(bytes)?;file.rewind()?;@@ -1221,7 +1234,7 @@ pub struct Cmd {shell_wrap: bool,/// Stdout configuration for stream() (defaults to inherit)stdout_cfg: Option<std::process::Stdio>,- /// Stdin configuration for stream() (defaults to null, or a file for stdin_data)+ /// Stdin configuration for stream() (defaults to null; stdin_data takes precedence)stdin_cfg: Option<std::process::Stdio>,/// Observe terminal signals and forward PID-targeted cancellation during stream().forward_signals: bool,@@ -1705,7 +1718,7 @@ impl Cmd {self}- /// Set complete input bytes, delivered through an anonymous temporary file.+ /// Set complete input bytes, prepared by [`buffered_stdin`] before spawn.////// The child reads these bytes followed by EOF, without a pipe writer that/// can block on an unread input. This takes precedence over `.stdin(Stdio)`.diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rsindex 122cf303a..8ce9d2707 100644--- a/tests/integration_tests/list.rs+++ b/tests/integration_tests/list.rs@@ -3718,8 +3718,9 @@ fn test_list_does_not_persist_observation_objects(mut repo: TestRepo) {}/// A broken system temporary-directory setting must not prevent either list-/// entry point from rendering. Probe objects fall back to a temporary directory-/// in Git's metadata and still disappear when the command exits.+/// entry point from rendering, nor fail a task. Probe objects fall back to a+/// temporary directory in Git's metadata and still disappear when the command+/// exits. The untracked file makes the working-tree diff pass paths on stdin.#[cfg(unix)]#[rstest]fn test_list_survives_unavailable_system_temp_directory(mut repo: TestRepo) {@@ -3728,6 +3729,7 @@ fn test_list_survives_unavailable_system_temp_directory(mut repo: TestRepo) {let feature_path = repo.add_worktree("feature");repo.commit("Main diverges");std::fs::write(feature_path.join("file.txt"), "feature\n").unwrap();+ std::fs::write(feature_path.join("untracked.txt"), "untracked\n").unwrap();let before = repo.git_output(&["count-objects", "-v"]);let missing_temp = repo.root_path().join("missing-temp");@@ -3744,7 +3746,7 @@ fn test_list_survives_unavailable_system_temp_directory(mut repo: TestRepo) {.output().unwrap();assert!(- output.status.success(),+ output.status.success() && output.stderr.is_empty(),"{} should fall back from an unavailable TMPDIR; stderr:\n{}",args.join(" "),String::from_utf8_lossy(&output.stderr)diff --git a/tests/integration_tests/remove.rs b/tests/integration_tests/remove.rsindex 8bbd5a240..67fbc652f 100644--- a/tests/integration_tests/remove.rs+++ b/tests/integration_tests/remove.rs@@ -345,6 +345,30 @@ fn test_remove_reap_spares_terminal_process(mut repo: TestRepo) {let _ = child.wait();}+/// Background removal hands its cleanup to a detached process, which must+/// spawn without temporary storage when the system temporary directory is+/// unusable.+#[cfg(unix)]+#[rstest]+fn test_remove_background_with_unavailable_system_temp_directory(mut repo: TestRepo) {+ let worktree_path = repo.add_worktree("feature");+ let missing_temp = repo.root_path().join("missing-temp");+ assert!(!missing_temp.exists());++ let output = repo+ .wt_command()+ .args(["remove", "feature", "--yes"])+ .env("TMPDIR", &missing_temp)+ .output()+ .unwrap();+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ output.status.success() && !stderr.contains("Failed"),+ "remove should succeed with an unavailable TMPDIR; stderr:\n{stderr}"+ );+ assert!(!worktree_path.exists());+}+#[rstest]fn test_remove_internal_mode(mut repo: TestRepo) {let worktree_path = repo.add_worktree("feature-internal");