Skip to content

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.rs
index 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.rs
index 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.rs
index 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 capabilities
pub 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.snap
index 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: C
LC_ALL: C
SOURCE_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: true
exit_code: 0
----- stdout -----
πŸ”„ Validating merge...
-[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
πŸ”„ Rebasing onto main...
πŸ”„ Pushing 1 commit to main @ [SHA]
-* 08f205e (HEAD -> feature) Add feature file
+* 9b57ee0 (HEAD -> feature) Add feature file
feature.txt | 1 +
1 file changed, 1 insertion(+)
@@ -51,14 +43,22 @@ exit_code: 0
πŸ”„ Running pre-merge check 'check1':
  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh
  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
πŸ”„ Running pre-merge check 'check2':
  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh
  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 complete
diff --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.snap
index 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: C
LC_ALL: C
SOURCE_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: true
exit_code: 0
----- stdout -----
-First
-Second
βœ… Created new worktree for feature at [REPO].feature
To enable automatic cd, run: wt configure-shell
@@ -30,5 +28,7 @@ To enable automatic cd, run: wt configure-shell
----- stderr -----
πŸ”„ Executing (post-create):
  echo 'First'
+First
πŸ”„ Executing (post-create):
  echo 'Second'
+Second
diff --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.snap
index 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: C
LC_ALL: C
SOURCE_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: true
exit_code: 0
----- stdout -----
-Installing deps
-Running setup
βœ… Created new worktree for feature at [REPO].feature
To enable automatic cd, run: wt configure-shell
@@ -30,5 +28,7 @@ To enable automatic cd, run: wt configure-shell
----- stderr -----
πŸ”„ Executing (post-create):
  echo 'Installing deps'
+Installing deps
πŸ”„ Executing (post-create):
  echo 'Running setup'
+Running setup
diff --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.snap
index 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: C
LC_ALL: C
SOURCE_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: true
exit_code: 0
----- stdout -----
-Setup complete
βœ… Created new worktree for feature at [REPO].feature
To enable automatic cd, run: wt configure-shell
@@ -29,3 +28,4 @@ To enable automatic cd, run: wt configure-shell
----- stderr -----
πŸ”„ Executing (post-create):
  echo 'Setup complete'
+Setup complete