Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Fix stdin inheritance for --execute on non-Unix platforms (#191)

Maximilian Roosbc05b5e00f63
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 37d320040..b2430c921 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -762,6 +762,10 @@ fn handle_removed_worktree_output(
///
/// If `stdin_content` is provided, it will be piped to the command's stdin (used for hook context JSON).
///
+/// If `inherit_stdin` is true and `stdin_content` is None, stdin is inherited from the parent process,
+/// enabling interactive programs (like `claude`, `vim`, or `python -i`) to read user input.
+/// If false and `stdin_content` is None, stdin is set to null (appropriate for non-interactive hooks).
+///
/// Returns error if command exits with non-zero status.
///
/// ## Cross-Platform Shell Execution
@@ -782,6 +786,7 @@ pub(crate) fn execute_streaming(
working_dir: &std::path::Path,
redirect_stdout_to_stderr: bool,
stdin_content: Option<&str>,
+ inherit_stdin: bool,
) -> anyhow::Result<()> {
use std::io::Write;
use worktrunk::git::WorktrunkError;
@@ -802,6 +807,8 @@ pub(crate) fn execute_streaming(
let stdin_mode = if stdin_content.is_some() {
std::process::Stdio::piped()
+ } else if inherit_stdin {
+ std::process::Stdio::inherit()
} else {
std::process::Stdio::null()
};
@@ -909,7 +916,8 @@ pub fn execute_command_in_worktree(
stderr().flush().ok(); // Ignore flush errors - reset is best-effort, command execution should proceed
// Execute with stdout→stderr redirect for deterministic ordering
- execute_streaming(command, worktree_path, true, stdin_content)?;
+ // Hooks don't need stdin inheritance (inherit_stdin=false)
+ execute_streaming(command, worktree_path, true, stdin_content, false)?;
// Flush to ensure all output appears before we continue
super::flush()?;
diff --git a/src/output/interactive.rs b/src/output/interactive.rs
index 1b9d91119..cd70e6ef9 100644
--- a/src/output/interactive.rs
+++ b/src/output/interactive.rs
@@ -93,8 +93,9 @@ impl OutputHandler for InteractiveOutput {
// On non-Unix platforms, fall back to spawn-and-wait.
// This uses the shell abstraction (Git Bash if available).
+ // inherit_stdin=true enables interactive programs (like `claude`, `vim`, `python -i`)
let exec_dir = self.target_dir.as_deref().unwrap_or_else(|| Path::new("."));
- if let Err(err) = execute_streaming(&command, exec_dir, false, None) {
+ if let Err(err) = execute_streaming(&command, exec_dir, false, None, true) {
// If the command failed with an exit code, just exit with that code.
// This matches Unix behavior where exec() replaces the process and
// the shell's exit code becomes the process exit code (no error message).
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 43886e022..8f1186f63 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -588,3 +588,49 @@ fn test_switch_missing_argument_shows_hints(repo: TestRepo) {
// Run switch with no arguments - should show clap error plus hints
snapshot_switch("switch_missing_argument_hints", &repo, &[]);
}
+
+/// Test that --execute commands can read from stdin (stdin inheritance).
+///
+/// This verifies the fix for non-Unix platforms where stdin was incorrectly
+/// set to Stdio::null() instead of Stdio::inherit(), breaking interactive
+/// programs like `vim`, `python -i`, or `claude`.
+///
+/// The test pipes input to `wt switch --execute "cat"` and verifies the
+/// cat command receives and outputs that input, proving stdin was inherited.
+#[rstest]
+fn test_switch_execute_stdin_inheritance(repo: TestRepo) {
+ use std::io::Write;
+ use std::process::{Command, Stdio};
+
+ let test_input = "stdin_inheritance_test_content\n";
+
+ let mut cmd = Command::new(env!("CARGO_BIN_EXE_wt"));
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["switch", "--create", "stdin-test", "--execute", "cat"])
+ .current_dir(repo.root_path())
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped());
+
+ let mut child = cmd.spawn().expect("failed to spawn wt");
+
+ // Write test input to stdin and close it to signal EOF
+ {
+ let stdin = child.stdin.as_mut().expect("failed to get stdin");
+ stdin
+ .write_all(test_input.as_bytes())
+ .expect("failed to write to stdin");
+ }
+
+ let output = child.wait_with_output().expect("failed to wait for child");
+ let stdout = String::from_utf8_lossy(&output.stdout);
+
+ // The cat command should have received our input via inherited stdin
+ // and echoed it to stdout
+ assert!(
+ stdout.contains("stdin_inheritance_test_content"),
+ "Expected cat to receive piped stdin. Got stdout: {}\nstderr: {}",
+ stdout,
+ String::from_utf8_lossy(&output.stderr)
+ );
+}