Skip to content

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

fix(alias): inherit stdin so interactive children keep the tty (#2380)

Worktrunk Bote1f2fbd76a31
diff --git a/src/commands/command_executor.rs b/src/commands/command_executor.rs
index b5b5c3cb7..69d9fd79b 100644
--- a/src/commands/command_executor.rs
+++ b/src/commands/command_executor.rs
@@ -461,10 +461,17 @@ fn run_one_command(
};
let log_label = command_log_label(cmd, origin);
+ // Hooks get a documented JSON context on stdin; aliases inherit stdin so
+ // interactive children (e.g. `wt switch`'s picker) keep their controlling
+ // terminal. Piping JSON into an interactive alias body steals the tty.
+ let stdin_json = match origin {
+ CommandOrigin::Hook { .. } => Some(cmd.context_json.as_str()),
+ CommandOrigin::Alias { .. } => None,
+ };
let result = execute_shell_command(
wt_path,
command_str,
- Some(&cmd.context_json),
+ stdin_json,
log_label.as_deref(),
directives.clone(),
);
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 8d3e619b2..d4d3a263f 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -1439,6 +1439,11 @@ pub fn execute_shell_command(
if let Some(content) = stdin_content {
cmd = cmd.stdin_bytes(content);
+ } else {
+ // Inherit the parent's stdin so interactive children (e.g. TUI
+ // pickers) keep their controlling terminal. Without this, `Cmd`
+ // defaults stdin to null and `stdin().is_terminal()` checks fail.
+ cmd = cmd.stdin(Stdio::inherit());
}
if let Some(path) = directives.cd_file {
diff --git a/tests/integration_tests/step_alias.rs b/tests/integration_tests/step_alias.rs
index b4edfd1d9..aa8c472f3 100644
--- a/tests/integration_tests/step_alias.rs
+++ b/tests/integration_tests/step_alias.rs
@@ -766,6 +766,62 @@ new-branch = "'{wt_toml}' switch --create alias-created"
);
}
+/// Alias subprocesses inherit the parent's stdin so interactive children
+/// (e.g. `wt switch`'s picker) keep the controlling terminal.
+///
+/// Regression test for #406: alias execution used to pipe the template
+/// context JSON into each child's stdin, which displaced the tty and broke
+/// `stdin().is_terminal()` guards in interactive commands. Only hooks have a
+/// documented JSON-on-stdin contract; aliases must leave stdin alone.
+#[rstest]
+fn test_alias_inherits_stdin(repo: TestRepo) {
+ repo.write_test_config(
+ r#"
+[aliases]
+echo-stdin = "cat"
+"#,
+ );
+ repo.commit("initial");
+
+ let sentinel = "INHERITED_STDIN_TOKEN";
+ let mut cmd = repo.wt_command();
+ cmd.args(["step", "echo-stdin"])
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped());
+ let mut child = cmd.spawn().unwrap();
+ {
+ // Drop closes the pipe so `cat` sees EOF and exits.
+ let mut stdin = child.stdin.take().unwrap();
+ stdin.write_all(sentinel.as_bytes()).unwrap();
+ }
+ let output = child.wait_with_output().unwrap();
+
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ output.status.success(),
+ "wt step echo-stdin failed: stdout={stdout}\nstderr={stderr}",
+ );
+
+ // stderr carries the "Running alias" banner; the alias body's stdout is
+ // redirected to stderr for deterministic ordering (see
+ // `execute_shell_command`). Check the combined output.
+ let combined = format!("{stdout}{stderr}");
+ assert!(
+ combined.contains(sentinel),
+ "alias body should read the parent's stdin ({sentinel} expected), \
+ got stdout={stdout:?} stderr={stderr:?}",
+ );
+ // The pre-fix behavior piped the context JSON (which always includes a
+ // "branch" key) to stdin, so `cat` would have echoed that instead.
+ assert!(
+ !combined.contains("\"branch\""),
+ "alias stdin should not receive the hook JSON context, \
+ got stdout={stdout:?} stderr={stderr:?}",
+ );
+}
+
/// Pipeline aliases announce their structure: named serial and concurrent
/// steps appear in the "Running alias" line, joined by `;` and `,`.
///