Skip to content

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

fix: Separate child stdout and stderr

Maximilian Roos7beb5a2aae27
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 95b2274a5..0357cf477 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -121,58 +121,23 @@ pub fn execute_command_in_worktree(
worktree_path: &std::path::Path,
command: &str,
) -> Result<(), GitError> {
- use std::io::{BufRead, BufReader, Write};
use std::process::{Command, Stdio};
- use worktrunk::styling::println;
// Flush stdout before executing command to ensure all our messages appear
// before the child process output
super::flush()?;
- // Spawn the command, capturing stdout and stderr so we can write them in order
- let mut child = Command::new("sh")
+ // Run the command with inherited stdio - child stdout and stderr both appear on our stdout and stderr
+ // All output goes through the terminal, which handles the display ordering
+ let status = Command::new("sh")
.arg("-c")
.arg(command)
.current_dir(worktree_path)
- .stdout(Stdio::piped())
- .stderr(Stdio::piped())
- .spawn()
+ .stdout(Stdio::inherit())
+ .stderr(Stdio::inherit())
+ .status()
.git_context("Failed to execute command")?;
- // Read and immediately write output line-by-line to maintain interleaving
- // Both child stdout and stderr go to our stdout (not stderr as before)
- // This ensures all output goes to the same FD and appears in order
- let stdout = child.stdout.take().unwrap();
- let stderr = child.stderr.take().unwrap();
-
- // Use threads to read both streams concurrently
- let stdout_thread = std::thread::spawn(move || {
- let reader = BufReader::new(stdout);
- for line in reader.lines() {
- if let Ok(line) = line {
- println!("{}", line);
- let _ = worktrunk::styling::stdout().flush();
- }
- }
- });
-
- let stderr_thread = std::thread::spawn(move || {
- let reader = BufReader::new(stderr);
- for line in reader.lines() {
- if let Ok(line) = line {
- println!("{}", line);
- let _ = worktrunk::styling::stdout().flush();
- }
- }
- });
-
- // Wait for output threads to complete
- let _ = stdout_thread.join();
- let _ = stderr_thread.join();
-
- // Wait for command to complete
- let status = child.wait().git_context("Failed to wait for command")?;
-
if !status.success() {
return Err(GitError::CommandFailed(format!(
"Command failed with exit code: {}",
diff --git a/src/output/interactive.rs b/src/output/interactive.rs
index 7aeec5436..179648300 100644
--- a/src/output/interactive.rs
+++ b/src/output/interactive.rs
@@ -2,7 +2,7 @@
use std::io::{self, Write};
use std::path::Path;
-use worktrunk::styling::{GREEN, SUCCESS_EMOJI, println, stdout, stderr};
+use worktrunk::styling::{GREEN, SUCCESS_EMOJI, println, stderr, stdout};
/// Interactive output mode for human users
///
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index 054143d08..8e960f35c 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -87,23 +87,23 @@ fn exec_through_bash_wrapper(repo: &TestRepo, subcommand: &str, args: &[&str]) -
// Source the wrapper
script.push_str(&wrapper_script);
- script.push_str("\n");
+ script.push('\n');
// Run the command
script.push_str("wt ");
script.push_str(subcommand);
for arg in args {
- script.push_str(" ");
+ script.push(' ');
// Quote arguments that need special handling (spaces, semicolons, etc.)
if arg.contains(' ') || arg.contains(';') || arg.contains('\'') {
- script.push_str("'");
+ script.push('\'');
script.push_str(&arg.replace('\'', "'\\''"));
- script.push_str("'");
+ script.push('\'');
} else {
script.push_str(arg);
}
}
- script.push_str("\n");
+ script.push('\n');
// Execute through bash
let mut cmd = Command::new("bash");
@@ -160,23 +160,23 @@ fn exec_through_fish_wrapper(repo: &TestRepo, subcommand: &str, args: &[&str]) -
// Source the wrapper
script.push_str(&wrapper_script);
- script.push_str("\n");
+ script.push('\n');
// Run the command
script.push_str("wt ");
script.push_str(subcommand);
for arg in args {
- script.push_str(" ");
+ script.push(' ');
// Quote arguments that need special handling (spaces, semicolons, etc.)
if arg.contains(' ') || arg.contains(';') || arg.contains('\'') {
- script.push_str("'");
+ script.push('\'');
script.push_str(&arg.replace('\'', "'\\''"));
- script.push_str("'");
+ script.push('\'');
} else {
script.push_str(arg);
}
}
- script.push_str("\n");
+ script.push('\n');
// Execute through fish
let mut cmd = Command::new("fish");
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap
index 92321ec7a..36d097e90 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap
@@ -12,37 +12,32 @@ info:
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpJIKOQz/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpKOfRHN/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Validating merge...
πŸ”„ Running pre-merge command check1:
-  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check1 3
+  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh
+  check1 3
[check1] stdout line 1
[check1] stdout line 2
[check1] stdout line 3
[check1] stdout complete
-[check1] stderr line 1
-[check1] stderr line 2
-[check1] stderr line 3
-[check1] stderr complete
πŸ”„ Running pre-merge command check2:
-  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check2 3
+  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh
+  check2 3
[check2] stdout line 1
[check2] stdout line 2
[check2] stdout line 3
[check2] stdout complete
-[check2] stderr line 1
-[check2] stderr line 2
-[check2] stderr line 3
-[check2] stderr complete
πŸ”„ Rebasing onto main...
πŸ”„ Pushing 1 commit to main @ [SHA]
@@ -57,3 +52,11 @@ exit_code: 0
βœ… Returned to primary at [REPO]
----- stderr -----
+[check1] stderr line 1
+[check1] stderr line 2
+[check1] stderr line 3
+[check1] stderr complete
+[check2] stderr line 1
+[check2] stderr line 2
+[check2] stderr line 3
+[check2] stderr complete