Skip to content

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

Merge stderr to stdout for consistent output

Maximilian Roos692dbe02651e
diff --git a/templates/fish.fish b/templates/fish.fish
index d1ad5c208..c9e737c4e 100644
--- a/templates/fish.fish
+++ b/templates/fish.fish
@@ -34,6 +34,8 @@ if type -q {{ cmd_prefix }}
# Use psub (process substitution) to preserve NUL bytes
# Command substitution $(...) strips NUL bytes, but psub preserves them
+ # Redirect stderr to stdout (2>&1) to capture child process output
+ # (child output goes to stderr per output system architecture)
# Redirect directly from psub output, and save exit code to temp file
set -l chunk_count 0
while read -z chunk
@@ -65,7 +67,7 @@ if type -q {{ cmd_prefix }}
end
printf '%s\n' $chunk
end
- end < (begin; command $_WORKTRUNK_CMD $argv; echo $status > $exit_code_file; end | psub)
+ end < (begin; command $_WORKTRUNK_CMD $argv 2>&1; echo $status > $exit_code_file; end | psub)
if test $debug_mode -eq 1
echo "[DEBUG] Finished reading, total chunks: $chunk_count" >&2
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index 7d1ec9971..45450db5a 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -130,7 +130,22 @@ fn build_shell_script(shell: &str, repo: &TestRepo, subcommand: &str, args: &[&s
}
script.push('\n');
- script
+ // Merge stderr to stdout to simulate real terminal behavior
+ // In a real terminal, both streams interleave naturally by the OS.
+ // The .output() method captures them separately, so we merge them here
+ // to preserve temporal locality (output appears when operations complete, not batched at the end)
+ match shell {
+ "fish" => {
+ // Fish uses begin...end for grouping
+ // Note: This exposes a Fish wrapper buffering bug where child output appears out of order
+ // (see templates/fish.fish - psub causes buffering). Tests document current behavior.
+ format!("begin\n{}\nend 2>&1", script)
+ }
+ _ => {
+ // bash/zsh use parentheses for subshell grouping
+ format!("( {} ) 2>&1", script)
+ }
+ }
}
/// Execute a command through a shell wrapper
@@ -170,11 +185,9 @@ fn exec_through_wrapper_from(
.output()
.unwrap_or_else(|_| panic!("Failed to execute {} wrapper", shell));
- // Combine stdout and stderr as users would see in a terminal
- let mut combined = String::from_utf8_lossy(&output.stdout).to_string();
- if !output.stderr.is_empty() {
- combined.push_str(&String::from_utf8_lossy(&output.stderr));
- }
+ // With 2>&1 in the script, both streams are merged to stdout
+ // This preserves temporal locality (output appears when operations complete)
+ let combined = String::from_utf8_lossy(&output.stdout).to_string();
ShellOutput {
combined,
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_already_on_target_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_already_on_target_bash.snap
new file mode 100644
index 000000000..a40888224
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_already_on_target_bash.snap
@@ -0,0 +1,6 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+πŸ”„ Validating merge...
+βœ… Already on main, nothing to merge
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_already_on_target_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_already_on_target_fish.snap
new file mode 100644
index 000000000..a40888224
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_already_on_target_fish.snap
@@ -0,0 +1,6 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+πŸ”„ Validating merge...
+βœ… Already on main, nothing to merge
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_no_hooks_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_no_hooks_bash.snap
new file mode 100644
index 000000000..d8b334233
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_no_hooks_bash.snap
@@ -0,0 +1,17 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+πŸ”„ Validating merge...
+πŸ”„ Rebasing onto main...
+πŸ”„ Pushing 1 commit to main @ 986886a
+
+* 986886a (HEAD -> feature-nohooks) Add feature
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+βœ… Pushed to main (1 commit, 1 file, +1) 
+πŸ”„ Cleaning up worktree...
+
+βœ… Returned to primary at [TMPDIR]/test-repo
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_no_hooks_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_no_hooks_fish.snap
new file mode 100644
index 000000000..2409f6dc9
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_no_hooks_fish.snap
@@ -0,0 +1,16 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+πŸ”„ Validating merge...
+πŸ”„ Rebasing onto main...
+πŸ”„ Pushing 1 commit to main @ 986886a
+
+* 986886a (HEAD -> feature-nohooks) Add feature
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+βœ… Pushed to main (1 commit, 1 file, +1) 
+πŸ”„ Cleaning up worktree...
+βœ… Returned to primary at [TMPDIR]/test-repo
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_post_merge_hooks_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_post_merge_hooks_bash.snap
new file mode 100644
index 000000000..15d90f29e
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_post_merge_hooks_bash.snap
@@ -0,0 +1,32 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+πŸ”„ Validating merge...
+πŸ”„ Rebasing onto main...
+πŸ”„ Pushing 1 commit to main @ c6eae6a
+
+* c6eae6a (HEAD -> feature-post) Add feature
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+βœ… Pushed to main (1 commit, 1 file, +1) 
+πŸ”„ Running post-merge command cleanup:
+  echo 'βœ“ Cleaning up temporary files'
+
+βœ“ Cleaning up temporary files
+
+πŸ”„ Running post-merge command deploy:
+  echo 'βœ“ Triggering deployment pipeline'
+
+βœ“ Triggering deployment pipeline
+
+πŸ”„ Running post-merge command notify:
+  echo 'βœ“ Merge successful - notifying team'
+
+βœ“ Merge successful - notifying team
+
+πŸ”„ Cleaning up worktree...
+
+βœ… Returned to primary at [TMPDIR]/test-repo
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_post_merge_hooks_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_post_merge_hooks_fish.snap
new file mode 100644
index 000000000..f72e369d3
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_post_merge_hooks_fish.snap
@@ -0,0 +1,28 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+πŸ”„ Validating merge...
+πŸ”„ Rebasing onto main...
+πŸ”„ Pushing 1 commit to main @ c6eae6a
+
+* c6eae6a (HEAD -> feature-post) Add feature
+
+ feature.txt | 1 +
+ 1 file changed, 1 insertion(+)
+
+βœ… Pushed to main (1 commit, 1 file, +1) 
+πŸ”„ Running post-merge command cleanup:
+  echo 'βœ“ Cleaning up temporary files'
+
+πŸ”„ Running post-merge command deploy:
+  echo 'βœ“ Triggering deployment pipeline'
+
+πŸ”„ Running post-merge command notify:
+  echo 'βœ“ Merge successful - notifying team'
+
+πŸ”„ Cleaning up worktree...
+βœ… Returned to primary at [TMPDIR]/test-repo
+βœ“ Cleaning up temporary files
+βœ“ Triggering deployment pipeline
+βœ“ Merge successful - notifying team
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap
index 96d8b4f3b..d438c6728 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_bash.snap
@@ -6,11 +6,11 @@ expression: output.normalized().as_ref()
πŸ”„ Running pre-merge command format:
  echo 'βœ“ Code formatting check passed'
+βœ“ Code formatting check passed
πŸ”„ Running pre-merge command test:
  echo 'βœ— Test suite failed: 3 tests failing' && exit 1
-βœ“ Code formatting check passed
βœ— Test suite failed: 3 tests failing
❌ Pre-merge command failed: test
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap
index 8e873bebb..d438c6728 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_fish.snap
@@ -6,10 +6,11 @@ expression: output.normalized().as_ref()
πŸ”„ Running pre-merge command format:
  echo 'βœ“ Code formatting check passed'
+βœ“ Code formatting check passed
+
πŸ”„ Running pre-merge command test:
  echo 'βœ— Test suite failed: 3 tests failing' && exit 1
-βœ“ Code formatting check passed
βœ— Test suite failed: 3 tests failing
❌ Pre-merge command failed: test
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_bash.snap
index f985b23ec..8ffd4517b 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_bash.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_bash.snap
@@ -6,14 +6,17 @@ expression: output.normalized().as_ref()
πŸ”„ Running pre-merge command format:
  echo 'βœ“ Code formatting check passed'
+βœ“ Code formatting check passed
πŸ”„ Running pre-merge command lint:
  echo 'βœ“ Linting passed - no warnings'
+βœ“ Linting passed - no warnings
πŸ”„ Running pre-merge command test:
  echo 'βœ“ All 47 tests passed in 2.3s'
+βœ“ All 47 tests passed in 2.3s
πŸ”„ Rebasing onto main...
πŸ”„ Pushing 1 commit to main @ d067f01
@@ -27,6 +30,3 @@ expression: output.normalized().as_ref()
πŸ”„ Cleaning up worktree...
βœ… Returned to primary at [TMPDIR]/test-repo
-βœ“ Code formatting check passed
-βœ“ Linting passed - no warnings
-βœ“ All 47 tests passed in 2.3s
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_fish.snap
index 16c6bf991..95365dfc9 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_fish.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_fish.snap
@@ -6,12 +6,18 @@ expression: output.normalized().as_ref()
πŸ”„ Running pre-merge command format:
  echo 'βœ“ Code formatting check passed'
+βœ“ Code formatting check passed
+
πŸ”„ Running pre-merge command lint:
  echo 'βœ“ Linting passed - no warnings'
+βœ“ Linting passed - no warnings
+
πŸ”„ Running pre-merge command test:
  echo 'βœ“ All 47 tests passed in 2.3s'
+βœ“ All 47 tests passed in 2.3s
+
πŸ”„ Rebasing onto main...
πŸ”„ Pushing 1 commit to main @ d067f01
@@ -23,6 +29,3 @@ expression: output.normalized().as_ref()
βœ… Pushed to main (1 commit, 1 file, +1) 
πŸ”„ Cleaning up worktree...
βœ… Returned to primary at [TMPDIR]/test-repo
-βœ“ Code formatting check passed
-βœ“ Linting passed - no warnings
-βœ“ All 47 tests passed in 2.3s
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_bash.snap
index 1ede37f78..98117f88b 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_bash.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_bash.snap
@@ -5,10 +5,12 @@ expression: output.normalized().as_ref()
πŸ”„ Executing (post-create):
  echo 'Installing dependencies...'
+Installing dependencies...
πŸ”„ Executing (post-create):
  echo 'Building project...'
+Building project...
πŸ”„ Starting (background):
  echo 'Starting dev server on port 3000'
@@ -17,5 +19,3 @@ expression: output.normalized().as_ref()
  echo 'Watching for file changes'
βœ… Created new worktree for feature-hooks at [TMPDIR]/test-repo.feature-hooks
-Installing dependencies...
-Building project...
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_fish.snap
index a83a945fa..98117f88b 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_fish.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_fish.snap
@@ -5,9 +5,13 @@ expression: output.normalized().as_ref()
πŸ”„ Executing (post-create):
  echo 'Installing dependencies...'
+Installing dependencies...
+
πŸ”„ Executing (post-create):
  echo 'Building project...'
+Building project...
+
πŸ”„ Starting (background):
  echo 'Starting dev server on port 3000'
@@ -15,5 +19,3 @@ expression: output.normalized().as_ref()
  echo 'Watching for file changes'
βœ… Created new worktree for feature-hooks at [TMPDIR]/test-repo.feature-hooks
-Installing dependencies...
-Building project...
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_log_paths_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_log_paths_bash.snap
new file mode 100644
index 000000000..6c313484f
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_log_paths_bash.snap
@@ -0,0 +1,11 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+πŸ”„ Starting (background):
+  sleep 0.1 && echo 'Server output'
+
+πŸ”„ Starting (background):
+  sleep 0.1 && echo 'Watcher output'
+
+βœ… Created new worktree for feature-logs at [TMPDIR]/test-repo.feature-logs
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_log_paths_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_log_paths_fish.snap
new file mode 100644
index 000000000..6c313484f
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_log_paths_fish.snap
@@ -0,0 +1,11 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+πŸ”„ Starting (background):
+  sleep 0.1 && echo 'Server output'
+
+πŸ”„ Starting (background):
+  sleep 0.1 && echo 'Watcher output'
+
+βœ… Created new worktree for feature-logs at [TMPDIR]/test-repo.feature-logs
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_uncommitted_changes_bash.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_uncommitted_changes_bash.snap
new file mode 100644
index 000000000..6d6577aca
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_uncommitted_changes_bash.snap
@@ -0,0 +1,5 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+βœ… Created new worktree for feature at [TMPDIR]/test-repo.feature
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_uncommitted_changes_fish.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_uncommitted_changes_fish.snap
new file mode 100644
index 000000000..6d6577aca
--- /dev/null
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_uncommitted_changes_fish.snap
@@ -0,0 +1,5 @@
+---
+source: tests/integration_tests/shell_wrapper.rs
+expression: output.normalized().as_ref()
+---
+βœ… Created new worktree for feature at [TMPDIR]/test-repo.feature
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 562f99bd0..cd61879ed 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -15,7 +15,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpuVpY4n/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp6EmXqA/test-config.toml
---
success: true
exit_code: 0
@@ -56,6 +56,8 @@ if type -q wt
# Use psub (process substitution) to preserve NUL bytes
# Command substitution $(...) strips NUL bytes, but psub preserves them
+ # Redirect stderr to stdout (2>&1) to capture child process output
+ # (child output goes to stderr per output system architecture)
# Redirect directly from psub output, and save exit code to temp file
set -l chunk_count 0
while read -z chunk
@@ -87,7 +89,7 @@ if type -q wt
end
printf '%s\n' $chunk
end
- end < (begin; command $_WORKTRUNK_CMD $argv; echo $status > $exit_code_file; end | psub)
+ end < (begin; command $_WORKTRUNK_CMD $argv 2>&1; echo $status > $exit_code_file; end | psub)
if test $debug_mode -eq 1
echo "[DEBUG] Finished reading, total chunks: $chunk_count" >&2