Skip to content

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

fix(shell): propagate nushell wrapper exit codes without spawning sh (#3945)

Worktrunk Botd971d0350285
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9486dd5ea..e97777074 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,8 @@
- **JSON list output ignores display-column gates**: `[list] columns` no longer makes `wt list --format json` contact a forge or generate summaries. CI requires `--full`; summaries also require `[list] summary = true` and a configured generator. (Breaking: schema 1 loses config-driven `ci` and `summary` fields.) ([#3812](https://github.com/max-sixty/worktrunk/pull/3812), thanks @emeren for the request)
+- **The nushell wrapper propagates exit codes without a POSIX shell**: `wt config shell install` writes the nushell wrapper on any platform where `nu` is on PATH, but the wrapper reported a failing `wt` by spawning `sh`. On Windows, where `sh` doesn't exist, every failing command printed a nushell ``Command `sh` not found`` trace over the wrapper's own source and reported exit 1 whatever the real code was. It now re-invokes nushell itself. `--execute` bodies still need `sh`, since they are POSIX snippets by construction. ([#3945](https://github.com/max-sixty/worktrunk/pull/3945), fixes [#3944](https://github.com/max-sixty/worktrunk/issues/3944))
+
- **Long Windows paths compare consistently**: paths beyond 260 characters could retain a `\\?\` prefix and appear to be on another drive. `copy-ignored` then refused them, while switch, remove, merge, and relocate landed at the worktree root instead of the original subdirectory. ([#3899](https://github.com/max-sixty/worktrunk/pull/3899), fixes [#3898](https://github.com/max-sixty/worktrunk/issues/3898), thanks @Persedes for reporting and verifying the fix)
- **Shell configuration rechecks before it writes**: overlapping installs lock and reread rc files; Fish completion installs preserve files created after preview; uninstall applies only previewed rc removals and rejects changed Worktrunk-owned files. ([#3853](https://github.com/max-sixty/worktrunk/pull/3853), [#3924](https://github.com/max-sixty/worktrunk/pull/3924))
diff --git a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
index dfdb076b4..747051051 100644
--- a/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
+++ b/src/shell/snapshots/worktrunk__shell__tests__init_nu.snap
@@ -123,6 +123,12 @@ export def --env --wrapped wt [...args] {
# Env changes (export) won't persist in the nushell session, but no
# worktrunk code emits export directives.
#
+ # This path still requires `sh`, unlike the exit propagation below: an
+ # exec body is a POSIX snippet by construction (`ShellEscapeMode::Posix`),
+ # so there is no nushell-native command to hand it to. It only fires for
+ # `--execute`, and which shell should run those bodies on Windows is an
+ # open question — see issue #3944.
+ #
# `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
@@ -166,9 +172,20 @@ export def --env --wrapped wt [...args] {
# Using a failing external command (not `error make`) so nushell treats it
# identically to the original non-zero exit — minimal display in scripts,
# no verbose source trace.
+ #
+ # nushell itself is the external command, not `sh`: `Shell::Nushell` isn't
+ # platform-gated, so this wrapper is installed on Windows whenever `nu` is
+ # on PATH, and there `sh` doesn't exist. A missing `sh` turns every failing
+ # `wt` command into a "Command `sh` not found" trace that prints the
+ # wrapper's own source and flattens the real exit code to 1 — the opposite
+ # of what this branch is for. `$nu.current-exe` is always present by
+ # construction, and using one path on every platform keeps the branch Unix
+ # CI exercises identical to the one Windows runs. `--no-std-lib` halves
+ # the startup this costs (~9 ms to ~4.5 ms on nu 0.115); `exit` is a core
+ # builtin, so propagation is unaffected.
if $exit_code != 0 {
if ($output | is-not-empty) { print -n $output }
- ^sh -c $"exit ($exit_code)"
+ ^$nu.current-exe --no-config-file --no-std-lib -c $"exit ($exit_code)"
} else if ($output | is-not-empty) {
$output
}
diff --git a/templates/nushell.nu b/templates/nushell.nu
index abd08d4e3..49b79ba0b 100644
--- a/templates/nushell.nu
+++ b/templates/nushell.nu
@@ -119,6 +119,12 @@ export def --env --wrapped {{ cmd }} [...args] {
# Env changes (export) won't persist in the nushell session, but no
# worktrunk code emits export directives.
#
+ # This path still requires `sh`, unlike the exit propagation below: an
+ # exec body is a POSIX snippet by construction (`ShellEscapeMode::Posix`),
+ # so there is no nushell-native command to hand it to. It only fires for
+ # `--execute`, and which shell should run those bodies on Windows is an
+ # open question — see issue #3944.
+ #
# `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
@@ -162,9 +168,20 @@ export def --env --wrapped {{ cmd }} [...args] {
# Using a failing external command (not `error make`) so nushell treats it
# identically to the original non-zero exit — minimal display in scripts,
# no verbose source trace.
+ #
+ # nushell itself is the external command, not `sh`: `Shell::Nushell` isn't
+ # platform-gated, so this wrapper is installed on Windows whenever `nu` is
+ # on PATH, and there `sh` doesn't exist. A missing `sh` turns every failing
+ # `wt` command into a "Command `sh` not found" trace that prints the
+ # wrapper's own source and flattens the real exit code to 1 — the opposite
+ # of what this branch is for. `$nu.current-exe` is always present by
+ # construction, and using one path on every platform keeps the branch Unix
+ # CI exercises identical to the one Windows runs. `--no-std-lib` halves
+ # the startup this costs (~9 ms to ~4.5 ms on nu 0.115); `exit` is a core
+ # builtin, so propagation is unaffected.
if $exit_code != 0 {
if ($output | is-not-empty) { print -n $output }
- ^sh -c $"exit ($exit_code)"
+ ^$nu.current-exe --no-config-file --no-std-lib -c $"exit ($exit_code)"
} else if ($output | is-not-empty) {
$output
}
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index a4911fbe5..525ece505 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -811,6 +811,96 @@ mod unix_tests {
});
}
+ /// The nushell wrapper reports a failing `wt` without spawning a POSIX shell.
+ ///
+ /// `Shell::Nushell` is not platform-gated — `wt config shell install` writes
+ /// this wrapper on Windows whenever `nu` is on PATH, and stock Windows has no
+ /// `sh`. Exit-code propagation runs on *every* failing `wt` command, so a `sh`
+ /// dependency there appended a ``Command `sh` not found`` trace (printing the
+ /// wrapper's own source) to each one and flattened the real exit code to 1
+ /// (#3944).
+ ///
+ /// Restricting PATH to `nu` and `git` reproduces the only property of Windows
+ /// that this path depends on — no POSIX shell to spawn — so the regression is
+ /// covered on the Unix runners where the nushell wrapper is actually tested.
+ #[rstest]
+ fn test_nushell_wrapper_failure_needs_no_posix_shell(mut repo: TestRepo) {
+ repo.add_worktree("existing");
+
+ // A PATH with no `sh`/`bash`/`dash`: just the shell under test, the git
+ // that `wt switch` shells out to, and the `rm` the wrapper's own Unix
+ // cleanup branch calls externally (`^rm`). Dropping `rm` would leak every
+ // `mktemp` file, silently — the branch is wrapped in `try` — and would
+ // also make the test less faithful to the platform it stands in for:
+ // Windows takes the `$nu.os-info.family` branch, where cleanup uses the
+ // nushell builtin and does run.
+ let bin_dir = tempfile::tempdir().unwrap();
+ for tool in ["nu", "git", "rm"] {
+ let resolved = which::which(tool)
+ .unwrap_or_else(|e| panic!("{tool} must be installed to run tests: {e}"));
+ std::os::unix::fs::symlink(resolved, bin_dir.path().join(tool)).unwrap();
+ }
+ let sanitized_path = bin_dir.path().display().to_string();
+
+ // Re-creating an existing branch fails, so the wrapper takes its
+ // exit-code propagation branch.
+ let output = exec_through_wrapper_with_env(
+ "nu",
+ &repo,
+ "switch",
+ &["--create", "existing"],
+ repo.root_path(),
+ &[],
+ &[("PATH", &sanitized_path)],
+ );
+
+ assert_eq!(
+ output.exit_code, 1,
+ "failure should propagate as exit 1.\nOutput:\n{}",
+ output.combined
+ );
+ assert!(
+ output.combined.contains("already exists"),
+ "wt's own error should still reach the terminal.\nOutput:\n{}",
+ output.combined
+ );
+ // The shape the bug produced: nushell rendering a spawn failure for the
+ // wrapper's internals on top of wt's error.
+ assert!(
+ !output.combined.contains("External command failed")
+ && !output.combined.contains("not found"),
+ "wrapper leaked a nushell external-command error.\nOutput:\n{}",
+ output.combined
+ );
+
+ // The other half of the bug: a failed spawn flattens every exit code to
+ // 1, so an exit-1 command can't tell the two templates apart. `wt config
+ // alias show <unknown>` exits 2 entirely inside wt (`unknown_alias_error`
+ // in `src/commands/config/alias.rs`), so the code only survives if the
+ // wrapper's propagation command actually ran.
+ let output = exec_through_wrapper_with_env(
+ "nu",
+ &repo,
+ "config",
+ &["alias", "show", "no-such-alias"],
+ repo.root_path(),
+ &[],
+ &[("PATH", &sanitized_path)],
+ );
+
+ assert_eq!(
+ output.exit_code, 2,
+ "wt's exit code should propagate unchanged, not flatten to 1.\nOutput:\n{}",
+ output.combined
+ );
+ assert!(
+ !output.combined.contains("External command failed")
+ && !output.combined.contains("not found"),
+ "wrapper leaked a nushell external-command error.\nOutput:\n{}",
+ output.combined
+ );
+ }
+
#[rstest]
#[case("bash")]
#[case("zsh")]
@@ -989,10 +1079,10 @@ mod unix_tests {
/// 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.
+ /// The wrapper's terminal `^$nu.current-exe -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