Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Propagate --execute command exit codes
Maximilian Roos28f6b6988f05
diff --git a/src/output/interactive.rs b/src/output/interactive.rsindex 9ce0bdfaf..3161da56f 100644--- a/src/output/interactive.rs+++ b/src/output/interactive.rs@@ -76,6 +76,16 @@ impl InteractiveOutput {// Execute command in the target directory with streaming outputlet exec_dir = self.target_dir.as_deref().unwrap_or_else(|| Path::new("."));+ // TODO: Consider using exec() to replace wt process with the command+ // Currently: wt spawns command as child, waits, then exits+ // Alternative: use std::os::unix::process::CommandExt::exec() to replace wt entirely+ // Trade-offs:+ // - Pro: Removes wt from process tree (cleaner `ps` output)+ // - Pro: Command becomes direct child of shell (more natural process hierarchy)+ // - Con: Can't show wt's success messages before exec (they'd be lost)+ // - Con: Unix-only (no Windows equivalent)+ // - Con: More complex error handling (exec only returns on error)+// Use shared streaming execution (no stdout->stderr redirect for --execute)execute_streaming(&command, exec_dir, false)?;diff --git a/templates/bash.sh b/templates/bash.shindex 778b5d34e..04eb7adca 100644--- a/templates/bash.sh+++ b/templates/bash.sh@@ -40,13 +40,11 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then# Execute command if one was specified# Security: Command is user-provided from -x flag; eval is intentional.# NUL-termination allows multi-line commands.- # Exit code semantics: Returns wt's exit code, not the executed command's.- # This allows detecting wt failures (e.g., branch creation errors).- # The executed command runs for side effects; its failure is logged but doesn't affect exit code.+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if [[ -n "$exec_cmd" ]]; then- if ! eval "$exec_cmd"; then- echo "Warning: Command execution failed (exit code $?)" >&2- fi+ eval "$exec_cmd"+ exit_code=$?fireturn $exit_codediff --git a/templates/elvish.elv b/templates/elvish.elvindex a1f69d6b4..2059128dc 100644--- a/templates/elvish.elv+++ b/templates/elvish.elv@@ -43,8 +43,16 @@ if (or (has-external wt) (has-env WORKTRUNK_BIN)) {}# Execute command if one was specified+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if (!=s $exec-cmd "") {- eval $exec-cmd+ try {+ eval $exec-cmd+ set exit-code = 0+ } catch e {+ # Command failed - set exit code to 1 (elvish exceptions don't always preserve exit codes)+ set exit-code = 1+ }}# Return exit code (will throw exception if non-zero)diff --git a/templates/fish.fish b/templates/fish.fishindex 644a4cd52..10973709b 100644--- a/templates/fish.fish+++ b/templates/fish.fish@@ -76,13 +76,11 @@ if type -q {{ cmd_prefix }}; or set -q WORKTRUNK_BINend# Execute command if one was specified- # Exit code semantics: Returns wt's exit code, not the executed command's.- # This allows detecting wt failures (e.g., branch creation errors).- # The executed command runs for side effects; its failure is logged but doesn't affect exit code.+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if test -n "$exec_cmd"- if not eval $exec_cmd- echo "Warning: Command execution failed (exit code $status)" >&2- end+ eval $exec_cmd+ set exit_code $statusendreturn $exit_codediff --git a/templates/nushell.nu b/templates/nushell.nuindex b12b531fc..3e9aaa865 100644--- a/templates/nushell.nu+++ b/templates/nushell.nu@@ -29,8 +29,11 @@ if (which wt | is-not-empty) or ($env.WORKTRUNK_BIN? | is-not-empty) {}# Execute command if one was specified+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if ($exec_cmd != "") {- nu -c $exec_cmd+ let cmd_result = (do { nu -c $exec_cmd } | complete)+ return $cmd_result.exit_code}# Return the exit codediff --git a/templates/powershell.ps1 b/templates/powershell.ps1index c4c595877..b8a6de670 100644--- a/templates/powershell.ps1+++ b/templates/powershell.ps1@@ -40,8 +40,11 @@ if ((Get-Command wt -ErrorAction SilentlyContinue) -or $env:WORKTRUNK_BIN) {}# Execute command if one was specified+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if ($execCmd) {Invoke-Expression $execCmd+ $exitCode = $LASTEXITCODE}# Return the exit codediff --git a/templates/xonsh.xsh b/templates/xonsh.xshindex f2fe4c71e..8c4d122a1 100644--- a/templates/xonsh.xsh+++ b/templates/xonsh.xsh@@ -33,8 +33,12 @@ if shutil.which("wt") is not None or os.environ.get('WORKTRUNK_BIN'):print(chunk)# Execute command if one was specified+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if exec_cmd:execx(exec_cmd)+ # execx() sets __xonsh__.env['LASTRETURNCODE'] to the exit code+ return __xonsh__.env.get('LASTRETURNCODE', 0)# Return the exit code, defaulting to 0 if the subprocess did not set onereturn result.returncode if result.returncode is not None else 0diff --git a/templates/zsh.zsh b/templates/zsh.zshindex 2938ac8d3..d4d89710a 100644--- a/templates/zsh.zsh+++ b/templates/zsh.zsh@@ -45,13 +45,11 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then# Execute command if one was specified# Security: Command is user-provided from -x flag; eval is intentional.# NUL-termination allows multi-line commands.- # Exit code semantics: Returns wt's exit code, not the executed command's.- # This allows detecting wt failures (e.g., branch creation errors).- # The executed command runs for side effects; its failure is logged but doesn't affect exit code.+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if [[ -n "$exec_cmd" ]]; then- if ! eval "$exec_cmd"; then- echo "Warning: Command execution failed (exit code $?)" >&2- fi+ eval "$exec_cmd"+ exit_code=$?fireturn $exit_codediff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rsindex ddf0651af..c579d6b40 100644--- a/tests/integration_tests/shell_wrapper.rs+++ b/tests/integration_tests/shell_wrapper.rs@@ -573,6 +573,48 @@ mod tests {}}+ /// Test that --execute command exit codes are propagated+ /// Verifies that when wt succeeds but the --execute command fails,+ /// the wrapper returns the command's exit code, not wt's.+ #[rstest]+ #[case("bash")]+ #[case("zsh")]+ #[case("fish")]+ fn test_wrapper_execute_exit_code_propagation(#[case] shell: &str) {+ let repo = TestRepo::new();+ repo.commit("Initial commit");++ // Use --force to skip approval prompt in tests+ // wt should succeed (creates worktree), but the execute command should fail with exit 42+ let output = exec_through_wrapper(+ shell,+ &repo,+ "switch",+ &[+ "--create",+ "test-exit-code",+ "--execute",+ "exit 42",+ "--force",+ ],+ );++ // Shell-agnostic assertions+ assert_eq!(+ output.exit_code, 42,+ "{}: Should propagate execute command's exit code (42), not wt's (0)",+ shell+ );+ output.assert_no_directive_leaks();++ // Should still show wt's success message (worktree was created)+ assert!(+ output.combined.contains("Created new worktree"),+ "{}: Should show wt's success message even though execute command failed",+ shell+ );+ }+/// Test switch --create with post-create-command (blocking) and post-start-command (background)/// Note: bash and fish disabled due to flaky PTY buffering race conditions///diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex 8da83d212..7235e4d54 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -12,10 +12,11 @@ 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/.tmpbBYDwj/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpi1D9uO/test-config.toml---success: trueexit_code: 0@@ -62,13 +63,11 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then# Execute command if one was specified# Security: Command is user-provided from -x flag; eval is intentional.# NUL-termination allows multi-line commands.- # Exit code semantics: Returns wt's exit code, not the executed command's.- # This allows detecting wt failures (e.g., branch creation errors).- # The executed command runs for side effects; its failure is logged but doesn't affect exit code.+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if [[ -n "$exec_cmd" ]]; then- if ! eval "$exec_cmd"; then- echo "Warning: Command execution failed (exit code $?)" >&2- fi+ eval "$exec_cmd"+ exit_code=$?fireturn $exit_codediff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snapindex de54ad433..ea9ecd71a 100644--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpZyxEdr/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpQUjOPT/test-config.toml---success: trueexit_code: 0@@ -99,13 +99,11 @@ if type -q wt; or set -q WORKTRUNK_BINend# Execute command if one was specified- # Exit code semantics: Returns wt's exit code, not the executed command's.- # This allows detecting wt failures (e.g., branch creation errors).- # The executed command runs for side effects; its failure is logged but doesn't affect exit code.+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if test -n "$exec_cmd"- if not eval $exec_cmd- echo "Warning: Command execution failed (exit code $status)" >&2- end+ eval $exec_cmd+ set exit_code $statusendreturn $exit_codediff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 80164e517..ee114d442 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -12,10 +12,11 @@ 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/.tmpcRX9We/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpfdRKfo/test-config.toml---success: trueexit_code: 0@@ -67,13 +68,11 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then# Execute command if one was specified# Security: Command is user-provided from -x flag; eval is intentional.# NUL-termination allows multi-line commands.- # Exit code semantics: Returns wt's exit code, not the executed command's.- # This allows detecting wt failures (e.g., branch creation errors).- # The executed command runs for side effects; its failure is logged but doesn't affect exit code.+ # Exit code semantics: If wt fails, returns wt's exit code (command never executes).+ # If wt succeeds but command fails, returns the command's exit code.if [[ -n "$exec_cmd" ]]; then- if ! eval "$exec_cmd"; then- echo "Warning: Command execution failed (exit code $?)" >&2- fi+ eval "$exec_cmd"+ exit_code=$?fireturn $exit_code