Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Ensure consistent output order for child commands
Maximilian Roos55868cb45310
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex d9d9797e8..ad724ea23 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -376,12 +376,14 @@ fn run_pre_merge_checks()?;for prepared in commands {use std::io::Write;+ use worktrunk::styling;+eprintln!("π {CYAN}Running pre-merge check '{name}':{CYAN:#}",name = prepared.name);eprint!("{}", format_with_gutter(&prepared.expanded, "", None)); // Gutter at column 0- let _ = std::io::stderr().flush();+ let _ = styling::stderr().flush();if let Err(e) = execute_command_in_worktree(worktree_path, &prepared.expanded) {return Err(GitError::PreMergeCheckFailed {@@ -390,9 +392,7 @@ fn run_pre_merge_checks(});}- // Flush both streams to ensure command output appears before next progress message- let _ = std::io::stdout().flush();- let _ = std::io::stderr().flush();+ // No need to flush here - the redirect in execute_command_in_worktree ensures ordering}Ok(())diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex ed0551f6d..f8e89236a 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -111,6 +111,13 @@ pub fn handle_remove_output(result: &RemoveResult) -> Result<(), GitError> {/// Execute a command in a worktree directory////// Uses Stdio::inherit() for real-time streaming output in both modes.+/// Redirects child stdout to stderr to ensure all output appears on a single file descriptor,+/// preventing terminal reordering issues between stdout and stderr.+///+/// The redirect ensures that all child output and our progress messages go through the same FD+/// (stderr), which guarantees deterministic ordering: bytes written later cannot appear before+/// bytes written earlier to the same FD.+////// Calls terminate_output() after completion to handle mode-specific cleanup/// (NUL terminator in directive mode, no-op in interactive mode).pub fn execute_command_in_worktree(@@ -119,9 +126,15 @@ pub fn execute_command_in_worktree() -> Result<(), GitError> {use std::process::{Command, Stdio};+ // Redirect child's stdout to parent's stderr so all bytes go to a single FD.+ // This prevents terminal reordering between stdout and stderr writes.+ // Brace group ensures the redirection applies to the whole command (pipes, &&, etc.)+ // TODO: Support Windows with appropriate redirect syntax for cmd.exe: ( command ) 1>&2+ let wrapped = format!("{{ {}; }} 1>&2", command);+let status = Command::new("sh").arg("-c")- .arg(command)+ .arg(&wrapped).current_dir(worktree_path).stdout(Stdio::inherit()).stderr(Stdio::inherit())diff --git a/src/styling.rs b/src/styling.rsindex f236a83ac..33cff4d09 100644--- a/src/styling.rs+++ b/src/styling.rs@@ -25,6 +25,12 @@ pub use anstream::print;/// Auto-detecting eprint that respects NO_COLOR, CLICOLOR_FORCE, and terminal capabilitiespub use anstream::eprint;+/// Auto-detecting stderr stream that respects NO_COLOR, CLICOLOR_FORCE, and terminal capabilities+pub use anstream::stderr;++/// Auto-detecting stdout stream that respects NO_COLOR, CLICOLOR_FORCE, and terminal capabilities+pub use anstream::stdout;+// ============================================================================// Re-exports from anstyle (for composition)// ============================================================================diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_stdout_stderr_ordering.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_stdout_stderr_ordering.snapindex 08a614bd6..d6b22d781 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_stdout_stderr_ordering.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_check_stdout_stderr_ordering.snap@@ -16,24 +16,16 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpnHxfp0/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpmDxVtD/test-config.toml---success: trueexit_code: 0----- stdout -----π [36mValidating merge...[0m-[check1] stdout line 1-[check1] stdout line 2-[check1] stdout line 3-[check1] stdout complete-[check2] stdout line 1-[check2] stdout line 2-[check2] stdout line 3-[check2] stdout completeπ [36mRebasing onto [1m[36mmain[0m...[0mπ [36mPushing 1 commit to [1m[36mmain[0m @ [SHA][0m-* [33m08f205e[m[33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m Add feature file+* [33m9b57ee0[m[33m ([m[1;36mHEAD[m[33m -> [m[1;32mfeature[m[33m)[m Add feature filefeature.txt | 1 [32m+[m1 file changed, 1 insertion(+)@@ -51,14 +43,22 @@ exit_code: 0π [36mRunning pre-merge check 'check1':[0m[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh[40m [0m check1 3+[check1] stdout line 1[check1] stderr line 1+[check1] stdout line 2[check1] stderr line 2+[check1] stdout line 3[check1] stderr line 3+[check1] stdout complete[check1] stderr completeπ [36mRunning pre-merge check 'check2':[0m[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh[40m [0m check2 3+[check2] stdout line 1[check2] stderr line 1+[check2] stdout line 2[check2] stderr line 2+[check2] stdout line 3[check2] stderr line 3+[check2] stdout complete[check2] stderr completediff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_multiple_commands_array.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_multiple_commands_array.snapindex 38b9c5bbd..954453ffe 100644--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_multiple_commands_array.snap+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_multiple_commands_array.snap@@ -16,13 +16,11 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpnEOJYK/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpUXnFk2/test-config.toml---success: trueexit_code: 0----- stdout ------First-Secondβ [32mCreated new worktree for [1mfeature[0m at [REPO].feature[0mTo enable automatic cd, run: wt configure-shell@@ -30,5 +28,7 @@ To enable automatic cd, run: wt configure-shell----- stderr -----π [36mExecuting (post-create):[0m[40m [0m echo 'First'+Firstπ [36mExecuting (post-create):[0m[40m [0m echo 'Second'+Seconddiff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_named_commands.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_named_commands.snapindex 5bde11ba9..8a899304a 100644--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_named_commands.snap+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_named_commands.snap@@ -16,13 +16,11 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpFlWxM5/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpmb9jWp/test-config.toml---success: trueexit_code: 0----- stdout ------Installing deps-Running setupβ [32mCreated new worktree for [1mfeature[0m at [REPO].feature[0mTo enable automatic cd, run: wt configure-shell@@ -30,5 +28,7 @@ To enable automatic cd, run: wt configure-shell----- stderr -----π [36mExecuting (post-create):[0m[40m [0m echo 'Installing deps'+Installing depsπ [36mExecuting (post-create):[0m[40m [0m echo 'Running setup'+Running setupdiff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_single_command.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_single_command.snapindex 24e0c747c..6ed14b4d8 100644--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_single_command.snap+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_single_command.snap@@ -16,12 +16,11 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpnAs7Pp/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp1xSxhC/test-config.toml---success: trueexit_code: 0----- stdout ------Setup completeβ [32mCreated new worktree for [1mfeature[0m at [REPO].feature[0mTo enable automatic cd, run: wt configure-shell@@ -29,3 +28,4 @@ To enable automatic cd, run: wt configure-shell----- stderr -----π [36mExecuting (post-create):[0m[40m [0m echo 'Setup complete'+Setup complete