Skip to content

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

fix(shell): catch a failing --execute body in the nushell wrapper (#3734)

Worktrunk Botbdd7113c95af
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
index 05382aea0..dfdb076b4 100644
--- a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
+++ b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
@@ -122,11 +122,23 @@ export def --env --wrapped wt [...args] {
# variables persist, `cd` affects later commands, etc.).
# Env changes (export) won't persist in the nushell session, but no
# worktrunk code emits export directives.
- if ($exec_file | path exists) and (open $exec_file --raw | str trim | is-not-empty) {
+ #
+ # `try` for the same reason as the binary call above: a body that exits
+ # non-zero raises a ShellError that would unwind the whole `def`, skipping
+ # cleanup (leaking all three temp files) and discarding the stdout this
+ # function still has to return. Catching it also makes the body's exit code
+ # a value we can fold in, rather than one the unwind happens to carry out.
+ let exec_exit = if ($exec_file | path exists) and (open $exec_file --raw | str trim | is-not-empty) {
let script = open $exec_file --raw
- ^sh -c $script
+ try { ^sh -c $script; 0 } catch { $env.LAST_EXIT_CODE }
+ } else {
+ 0
}
+ # First non-zero wins, wt before the body — the precedence the bash, zsh
+ # and fish wrappers apply to their `src_exit`.
+ let exit_code = if $exit_code != 0 { $exit_code } else { $exec_exit }
+
let output = (open $stdout_file --raw)
# Clean up all three temp files at once, after the last read of any of them.
diff --git a/templates/nushell.nu b/templates/nushell.nu
index ea534206f..abd08d4e3 100644
--- a/templates/nushell.nu
+++ b/templates/nushell.nu
@@ -118,11 +118,23 @@ export def --env --wrapped {{ cmd }} [...args] {
# variables persist, `cd` affects later commands, etc.).
# Env changes (export) won't persist in the nushell session, but no
# worktrunk code emits export directives.
- if ($exec_file | path exists) and (open $exec_file --raw | str trim | is-not-empty) {
+ #
+ # `try` for the same reason as the binary call above: a body that exits
+ # non-zero raises a ShellError that would unwind the whole `def`, skipping
+ # cleanup (leaking all three temp files) and discarding the stdout this
+ # function still has to return. Catching it also makes the body's exit code
+ # a value we can fold in, rather than one the unwind happens to carry out.
+ let exec_exit = if ($exec_file | path exists) and (open $exec_file --raw | str trim | is-not-empty) {
let script = open $exec_file --raw
- ^sh -c $script
+ try { ^sh -c $script; 0 } catch { $env.LAST_EXIT_CODE }
+ } else {
+ 0
}
+ # First non-zero wins, wt before the body — the precedence the bash, zsh
+ # and fish wrappers apply to their `src_exit`.
+ let exit_code = if $exit_code != 0 { $exit_code } else { $exec_exit }
+
let output = (open $stdout_file --raw)
# Clean up all three temp files at once, after the last read of any of them.
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index f3141bc92..7afabf467 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -977,6 +977,62 @@ mod unix_tests {
);
}
+ /// A failing `--execute` body must not abort the nushell wrapper before its
+ /// cleanup runs.
+ ///
+ /// Nushell 0.98+ raises a `ShellError` on a non-zero external exit, so the
+ /// wrapper's `^sh -c $script` used to unwind the whole `def` — the three
+ /// `mktemp` files leaked and the stdout the function still had to return was
+ /// discarded. `test_wrapper_execute_exit_code_propagation` covers the same
+ /// command and passes either way: the unwind *happens* to carry exit 42 out
+ /// to the shell, which is all that test asserts. So the leak sat on a path
+ /// with coverage, and the assertion that distinguishes the two is whether the
+ /// wrapper reached its own end — observable here as an empty `TMPDIR`.
+ ///
+ /// The wrapper's terminal `^sh -c $"exit ($exit_code)"` is *meant* to abort
+ /// the caller, since that's how the code propagates; the driving script wraps
+ /// the call in nushell's own `try` so that intended propagation doesn't hide
+ /// whether cleanup ran first.
+ #[rstest]
+ fn test_nu_wrapper_execute_failure_runs_cleanup(repo: TestRepo) {
+ // A dedicated TMPDIR so the wrapper's `mktemp` files are the only
+ // occupants and a leak is directly observable.
+ let tmp = tempfile::tempdir().unwrap();
+ let tmp_path = tmp.path().to_string_lossy().to_string();
+
+ let mut script = String::new();
+ append_wrapper_setup(&mut script, "nu", &repo);
+ script.push_str(
+ "let code = (try { wt switch --create exec-abort --yes --execute \"exit 42\"; 0 } catch { $env.LAST_EXIT_CODE })\n",
+ );
+ script.push_str("print $\"WT_EXIT:($code)\"\n");
+
+ let config_path = repo.test_config_path().to_string_lossy().to_string();
+ let approvals_path = repo.test_approvals_path().to_string_lossy().to_string();
+ let mut env_vars = build_test_env_vars(&config_path, &approvals_path);
+ env_vars.push(("TMPDIR", &tmp_path));
+
+ let (combined, _) =
+ exec_in_pty_interactive("nu", &script, repo.root_path(), &env_vars, &[]);
+
+ // The body's exit code still reaches the caller — the contract
+ // `test_wrapper_execute_exit_code_propagation` pins, now carried by the
+ // wrapper's own propagation rather than by the unwind.
+ assert!(
+ combined.contains("WT_EXIT:42"),
+ "wrapper should propagate the --execute body's exit code (42).\nOutput:\n{combined}"
+ );
+
+ let leftover: Vec<_> = fs::read_dir(tmp.path())
+ .unwrap()
+ .map(|e| e.unwrap().file_name())
+ .collect();
+ assert!(
+ leftover.is_empty(),
+ "wrapper aborted before cleanup and leaked temp files: {leftover:?}\nOutput:\n{combined}"
+ );
+ }
+
/// Test switch --create with pre-start (blocking) and post-start (background)
/// Note: bash and fish disabled due to flaky PTY buffering race conditions
///