Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(alias): pass alias stdout through so 'wt <alias> | ...' works (#2479)
Worktrunk Botf3eb8fe93ded
diff --git a/src/commands/alias.rs b/src/commands/alias.rsindex 05f9e338a..8a479563e 100644--- a/src/commands/alias.rs+++ b/src/commands/alias.rs@@ -562,6 +562,10 @@ fn run_alias(concurrent: true,announce: AnnouncePolicy::None,pipe_stdin: false,+ // Aliases pass child stdout through so `wt <alias> | …` works+ // in scripts (#2478). Worktrunk's own messages still go to+ // stderr.+ redirect_stdout_to_stderr: false,error_wrapper: alias_error_wrapper(alias_name.clone()),}})diff --git a/src/commands/command_executor.rs b/src/commands/command_executor.rsindex 635c6a08c..100466138 100644--- a/src/commands/command_executor.rs+++ b/src/commands/command_executor.rs@@ -90,6 +90,11 @@ pub struct ForegroundStep {/// the parent's stdin so interactive children keep the controlling tty/// (aliases).pub pipe_stdin: bool,+ /// Merge the child's stdout onto wt's stderr (`true`, hooks) or pass it+ /// through unchanged (`false`, aliases). Hooks merge so their output stays+ /// ordered with wt's own stderr "Running …" lines; aliases pass through so+ /// `wt <alias> | …` remains usable in scripts (#2478).+ pub redirect_stdout_to_stderr: bool,/// Wraps a per-command failure into the final error returned to the caller.pub error_wrapper: ErrorWrapper,}@@ -483,6 +488,7 @@ fn run_one_command(stdin_json,cmd.log_label.as_deref(),directives.clone(),+ fg_step.redirect_stdout_to_stderr,);match result {diff --git a/src/commands/for_each.rs b/src/commands/for_each.rsindex f34d31f4a..13fd457bb 100644--- a/src/commands/for_each.rs+++ b/src/commands/for_each.rs@@ -86,12 +86,17 @@ pub fn step_for_each(args: Vec<String>, format: crate::cli::SwitchFormat) -> any// Pipe context JSON to stdin for scripts that want structured data.// Directive files are scrubbed — commands run in other worktrees should// not influence the parent shell's working directory.+ // for-each prints decorated headers/footers around child output, so+ // we merge child stdout onto stderr to keep ordering deterministic.+ // The structured `--format=json` output is a separate stdout write+ // emitted after all child commands complete (see below).match execute_shell_command(&wt.path,&command,Some(&context_json),None,DirectivePassthrough::none(),+ true,) {Ok(()) => {if json_mode {diff --git a/src/commands/hooks.rs b/src/commands/hooks.rsindex 6c51a0fbc..7247676a9 100644--- a/src/commands/hooks.rs+++ b/src/commands/hooks.rs@@ -742,6 +742,9 @@ pub(crate) fn run_hooks_foreground(display_path: sourced.display_path,},pipe_stdin: true,+ // Hooks merge stdout onto stderr so child output stays ordered+ // with wt's own "Running …" announcement lines.+ redirect_stdout_to_stderr: true,error_wrapper: hook_error_wrapper(sourced.hook_type),}).collect();diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex 1b0eebe25..2d0c6b8a8 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -1441,7 +1441,7 @@ fn handle_removed_worktree_output(/// (`run_pipeline.rs`) has its own spawning logic since it redirects to log/// files and runs detached.///-/// Capabilities: stdout→stderr redirect for deterministic ordering,+/// Capabilities: optional stdout→stderr redirect for deterministic ordering,/// SIGINT/SIGTERM forwarding to child process group, ANSI reset before child/// runs, `Cmd` tracing/logging, and directive file control.///@@ -1461,6 +1461,18 @@ fn handle_removed_worktree_output(/// only the CD file passes through; in legacy compat mode the single/// legacy file passes through to preserve pre-split behavior.///+/// ## Stdout routing+///+/// `redirect_stdout_to_stderr` controls whether the child's stdout is merged+/// onto wt's stderr (`true`) or passed through unchanged (`false`).+///+/// - Hooks and `for-each` pass `true`: their output is decoration around+/// wt's own stderr messages, and merging keeps "Running …" / progress lines+/// ordered with the child's own writes.+/// - Aliases pass `false`: `wt <alias>` is a user-defined command and its+/// stdout must remain pipeable, so `wt my-alias | jq` and similar+/// compositions work (#2478).+////// ## ANSI reset////// Resets ANSI codes on stderr before the child runs. Terminal emulators@@ -1473,6 +1485,7 @@ pub fn execute_shell_command(stdin_content: Option<&str>,command_log_label: Option<&str>,directives: DirectivePassthrough,+ redirect_stdout_to_stderr: bool,) -> anyhow::Result<()> {// Flush stdout before executing command to ensure all our messages appear// before the child process output@@ -1484,12 +1497,14 @@ pub fn execute_shell_command(eprint!("{}", anstyle::Reset);stderr().flush().ok(); // Ignore flush errors - reset is best-effort, command execution should proceed- // Execute with stdout→stderr redirect for deterministic orderinglet mut cmd = Cmd::shell(command).current_dir(working_dir)- .stdout(Stdio::from(std::io::stderr())).forward_signals();+ if redirect_stdout_to_stderr {+ cmd = cmd.stdout(Stdio::from(std::io::stderr()));+ }+if let Some(label) = command_log_label {cmd = cmd.external(label);}diff --git a/tests/integration_tests/step_alias.rs b/tests/integration_tests/step_alias.rsindex da5db881f..974909648 100644--- a/tests/integration_tests/step_alias.rs+++ b/tests/integration_tests/step_alias.rs@@ -273,6 +273,42 @@ deploy = [assert_cmd_snapshot!(cmd);}+/// Alias body output is delivered on stdout so it can be piped into other+/// commands (#2478). Worktrunk's own progress/announcement messages still go+/// to stderr; only the child shell's stdout passes through.+#[rstest]+fn test_alias_body_writes_to_stdout(mut repo: TestRepo) {+ repo.write_project_config(+ r#"+[aliases]+emit = "echo hello"+"#,+ );+ repo.commit("Add alias config");+ let feature_path = repo.add_worktree("feature");++ let output = repo+ .wt_command()+ .args(["-y", "emit"])+ .current_dir(&feature_path)+ .output()+ .unwrap();++ assert!(+ output.status.success(),+ "alias failed:\nstdout: {}\nstderr: {}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr)+ );+ let stdout = String::from_utf8_lossy(&output.stdout);+ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ stdout.lines().any(|line| line.trim() == "hello"),+ "expected `hello` on stdout (so `wt <alias> | …` is usable in scripts), \+ got stdout={stdout:?} stderr={stderr:?}"+ );+}+/// Alias command failure propagates exit code (-y bypasses approval)#[rstest]fn test_step_alias_exit_code(mut repo: TestRepo) {diff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_append_executes_both.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_append_executes_both.snapindex ffe8d4691..5ce3343e3 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_append_executes_both.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_append_executes_both.snap@@ -43,7 +43,7 @@ info:success: trueexit_code: 0----- stdout -----+USER+PROJECT----- stderr ------[0mUSER-[0mPROJECTdiff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_approval_already_approved.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_approval_already_approved.snapindex a5c1350c7..0c468094b 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_approval_already_approved.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_approval_already_approved.snap@@ -42,6 +42,6 @@ info:success: trueexit_code: 0----- stdout -----+deploying feature----- stderr ------[0mdeploying featurediff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_approval_user_and_project_both_need_approval.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_approval_user_and_project_both_need_approval.snapindex 323eac9cc..492d3c8bd 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_approval_user_and_project_both_need_approval.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_approval_user_and_project_both_need_approval.snap@@ -43,7 +43,7 @@ info:success: trueexit_code: 0----- stdout -----+user deploy+project deploy----- stderr ------[0muser deploy-[0mproject deploydiff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_approval_user_config_skips.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_approval_user_config_skips.snapindex 66c551968..726c4b6d3 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_approval_user_config_skips.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_approval_user_config_skips.snap@@ -42,6 +42,6 @@ info:success: trueexit_code: 0----- stdout -----+deploying from user config----- stderr ------[0mdeploying from user configdiff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_approval_yes_first_run.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_approval_yes_first_run.snapindex d3382e3e1..36f2673ee 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_approval_yes_first_run.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_approval_yes_first_run.snap@@ -43,6 +43,6 @@ info:success: trueexit_code: 0----- stdout -----+deploying----- stderr ------[0mdeployingdiff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_pipeline_announcement.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_pipeline_announcement.snapindex ea4ca751e..b753e9679 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_pipeline_announcement.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_pipeline_announcement.snap@@ -43,9 +43,9 @@ info:success: trueexit_code: 0----- stdout -----+INSTALL----- stderr -----[36m◎[39m [36mRunning alias [1mdeploy[22m: [1minstall[22m; [1mbuild[22m, [1mlint[22m[39m-[0mINSTALL-[1m[36mbuild[0m │ BUILD+[0m[1m[36mbuild[0m │ BUILD[1m[35mlint [0m │ LINTdiff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_args_shell_escaped.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_args_shell_escaped.snapindex abb72c37f..4a474c144 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_args_shell_escaped.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_args_shell_escaped.snap@@ -44,6 +44,7 @@ info:success: trueexit_code: 0----- stdout -----+hello foo bar baz----- stderr -----[2m○[22m template variables:@@ -67,4 +68,3 @@ exit_code: 0[107m [0m [2m[0m[2m[34mecho[0m[2m hello [0m[2m[32m{{[0m[2m args [0m[2m[32m}}[0m[2m[107m [0m [2m→[22m[107m [0m [2m[0m[2m[34mecho[0m[2m hello foo [0m[2m[32m'bar baz'[0m[2m-[0mhello foo bar bazdiff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_variable_table.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_variable_table.snapindex 618e83183..272d7ad57 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_variable_table.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_variable_table.snap@@ -43,6 +43,7 @@ info:success: trueexit_code: 0----- stdout -----+hello world----- stderr -----[2m○[22m template variables:@@ -66,4 +67,3 @@ exit_code: 0[107m [0m [2m[0m[2m[34mecho[0m[2m hello [0m[2m[32m{{[0m[2m args [0m[2m[32m}}[0m[2m[107m [0m [2m→[22m[107m [0m [2m[0m[2m[34mecho[0m[2m hello world-[0mhello worlddiff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_args_sequence_access.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_args_sequence_access.snapindex 1f6ba9dee..d623a9285 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_args_sequence_access.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_args_sequence_access.snap@@ -44,8 +44,8 @@ info:success: trueexit_code: 0----- stdout ------------ stderr ------[0mfirst=alpha+first=alphacount=2each= alpha beta gamma++----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_double_dash_escape.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_double_dash_escape.snapindex 28a3dcde8..7037fab58 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_double_dash_escape.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_double_dash_escape.snap@@ -45,6 +45,6 @@ info:success: trueexit_code: 0----- stdout -----+got --env=staging literal----- stderr ------[0mgot --env=staging literaldiff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_empty_args_renders_empty.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_empty_args_renders_empty.snapindex 6ba320e4d..27d67b313 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_empty_args_renders_empty.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_empty_args_renders_empty.snap@@ -42,6 +42,6 @@ info:success: trueexit_code: 0----- stdout -----+[]----- stderr ------[0m[]diff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_forwards_positional_args.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_forwards_positional_args.snapindex 1b6bf9b97..bb3c4cbc0 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_forwards_positional_args.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_forwards_positional_args.snap@@ -45,6 +45,6 @@ info:success: trueexit_code: 0----- stdout -----+got one two three four----- stderr ------[0mgot one two three fourdiff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_from_project_config.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_from_project_config.snapindex 07acb0bcc..ee551cb4b 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_from_project_config.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_from_project_config.snap@@ -43,6 +43,6 @@ info:success: trueexit_code: 0----- stdout -----+Hello from feature----- stderr ------[0mHello from featurediff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_from_user_config.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_from_user_config.snapindex 0db6e1afd..c03127beb 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_from_user_config.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_from_user_config.snap@@ -42,6 +42,6 @@ info:success: trueexit_code: 0----- stdout -----+Greetings from feature----- stderr ------[0mGreetings from featurediff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_multi_step_binds_across_pipeline.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_multi_step_binds_across_pipeline.snapindex 1d1f7602a..60ee06814 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_multi_step_binds_across_pipeline.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_multi_step_binds_across_pipeline.snap@@ -45,8 +45,8 @@ info:success: trueexit_code: 0----- stdout -----+step1 env=prod+step2 region=us-east----- stderr -----[36m◎[39m [36mRunning alias [1mdeploy[22m: [1mpublish[22m[39m-[0mstep1 env=prod-[0mstep2 region=us-eastdiff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_unreferenced_key_forwards_to_args.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_unreferenced_key_forwards_to_args.snapindex 1958a0913..808c30a1f 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_unreferenced_key_forwards_to_args.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_unreferenced_key_forwards_to_args.snap@@ -44,6 +44,6 @@ info:success: trueexit_code: 0----- stdout -----+got --env=staging foo----- stderr ------[0mgot --env=staging foodiff --git a/tests/snapshots/integration__integration_tests__step_alias__step_alias_user_var_overshadows_builtin.snap b/tests/snapshots/integration__integration_tests__step_alias__step_alias_user_var_overshadows_builtin.snapindex 2ca17594b..2d3d6ba9b 100644--- a/tests/snapshots/integration__integration_tests__step_alias__step_alias_user_var_overshadows_builtin.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__step_alias_user_var_overshadows_builtin.snap@@ -43,6 +43,6 @@ info:success: trueexit_code: 0----- stdout -----+branch=override----- stderr ------[0mbranch=overridediff --git a/tests/snapshots/integration__integration_tests__step_alias__top_level_alias_dispatch.snap b/tests/snapshots/integration__integration_tests__step_alias__top_level_alias_dispatch.snapindex 34204883b..9318b3a7d 100644--- a/tests/snapshots/integration__integration_tests__step_alias__top_level_alias_dispatch.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__top_level_alias_dispatch.snap@@ -42,6 +42,6 @@ info:success: trueexit_code: 0----- stdout -----+Hello from feature----- stderr ------[0mHello from featurediff --git a/tests/snapshots/integration__integration_tests__step_alias__top_level_alias_with_step_builtin_name.snap b/tests/snapshots/integration__integration_tests__step_alias__top_level_alias_with_step_builtin_name.snapindex 53f4f2e58..75cb96f86 100644--- a/tests/snapshots/integration__integration_tests__step_alias__top_level_alias_with_step_builtin_name.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__top_level_alias_with_step_builtin_name.snap@@ -42,6 +42,6 @@ info:success: trueexit_code: 0----- stdout -----+custom-commit----- stderr ------[0mcustom-commit