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.rsindex 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 outputsuper::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.rsindex 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.rsindex 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 wrapperscript.push_str(&wrapper_script);- script.push_str("\n");+ script.push('\n');// Run the commandscript.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 bashlet mut cmd = Command::new("bash");@@ -160,23 +160,23 @@ fn exec_through_fish_wrapper(repo: &TestRepo, subcommand: &str, args: &[&str]) -// Source the wrapperscript.push_str(&wrapper_script);- script.push_str("\n");+ script.push('\n');// Run the commandscript.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 fishlet 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.snapindex 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/nullGIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_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: trueexit_code: 0----- stdout -----π [36mValidating merge...[0mπ [36mRunning pre-merge command [1m[36mcheck1[0m:[0m-[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check1 3+[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh+[40m [0m 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π [36mRunning pre-merge command [1m[36mcheck2[0m:[0m-[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check2 3+[40m [0m [PROJECT_ROOT]/tests/fixtures/mixed-output.sh+[40m [0m 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π [36mRebasing onto [1m[36mmain[0m...[0mπ [36mPushing 1 commit to [1m[36mmain[0m @ [SHA][0m@@ -57,3 +52,11 @@ exit_code: 0β [32mReturned to primary at [REPO][0m----- 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