Repository navigation
fix(shell): propagate nushell wrapper exit codes without spawning sh - #3945
Conversation
The nushell wrapper reported a failing wt by spawning `sh -c "exit N"`. Shell::Nushell is not platform-gated, so wt config shell install writes this wrapper on Windows whenever nu is on PATH — and there sh does not exist. Every failing wt command then printed a nushell "Command `sh` not found" trace over the wrapper's own source and reported exit 1 regardless of the real code. Re-invoke nushell itself instead. Using one command on every platform keeps the branch Unix CI exercises identical to the one Windows runs. The --execute path still calls sh; those bodies are POSIX snippets by construction, so which shell should run them on Windows is an open design question, noted inline. Closes #3944
worktrunk-bot
left a comment
There was a problem hiding this comment.
Verified the core swap against nushell 0.115 rather than by reading: ^$nu.current-exe --no-config-file -c $"exit ($exit_code)" propagates the code identically to ^sh -c (both exit 3 for exit_code = 3), prints nothing on the way out, and — unlike sh, which answers exit -5 with Illegal number and exit 2 — silently wraps out-of-range and negative codes the POSIX way. $nu.current-exe and --no-std-lib are both present in nushell 0.96.0 (src/command.rs at that tag declares the no-std-lib switch), so the installer's floor holds for the line as written and for the suggestion below. The new comment's "it only fires for --execute" also checks out: output::execute is the sole writer of the exec-directive file, execute_user_command its sole caller, and switch.rs's --execute arm the only thing that reaches it.
Four notes inline, none blocking. The one I'd most want addressed is the exit-code assertion: I ran the pre-fix template under a PATH with no sh and it exits 1 too, so assert_eq!(output.exit_code, 1) passes against both templates. The half of #3944 about a real exit code being flattened to 1 is currently uncovered.
Not approving because this is my own PR — GitHub rejects self-approvals.
…cover a non-1 code Review follow-ups on #3945: - `--no-std-lib` halves the propagation command's startup (9.4 ms to 4.9 ms on nu 0.115, measured with `timeit`). `exit` is a core builtin, so the code still propagates; the switch is declared in nushell 0.96.0, the floor the installer requires. - Assert a non-1 exit code. `wt config alias show <unknown>` exits 2 from inside wt, so the assertion fails on the pre-fix template (a failed `sh` spawn flattens every code to 1) where the exit-1 case did not. - Put `rm` on the test's restricted PATH: the Unix cleanup branch calls `^rm` externally inside a `try`, so without it every `mktemp` file leaked silently, and the test diverged from Windows, where the builtin runs. - Cite the PR alongside the issue in the changelog entry, matching the neighbouring entries.
worktrunk-bot
left a comment
There was a problem hiding this comment.
All four inline points from the last round are applied, and I checked each one against the code rather than the reply. The one that mattered — coverage for the exit-code half of #3944 — now holds: I re-ran test_nushell_wrapper_failure_needs_no_posix_shell against the pre-fix template and it fails with the exact symptom from the report (Command `sh` not found, wrapper source rendered at the user), and passes on this one. The discriminating half checks out independently too: with sh off PATH, ^sh -c $"exit (2)" reports 1 while ^$nu.current-exe --no-config-file --no-std-lib -c $"exit (2)" reports 2, so the second call is what covers "a wt that exited 3 exits 1". --no-std-lib is declared in nushell 0.96.0's src/command.rs, and exit still propagates under it. The snapshot body matches the rendered template with no regeneration pass needed.
One thing to fix before merge, in the PR description rather than the diff: the trailer Closes #3944 is registered as a closing reference (closingIssuesReferences returns #3944), so merging auto-closes it — while the What this does not change section says "#3944 stays open for it", and the comment this PR ships in templates/nushell.nu sends readers there ("which shell should run those bodies on Windows is an open question — see issue #3944"). Merge as-is and that comment points every future reader at a closed issue, with the --execute half of the report silently gone. Refs #3944 in the trailer keeps the link without the auto-close.
Minor, same file: the opening paragraph still says the test "restricts PATH to nu and git" — it symlinks rm too, as of the cleanup-leak fix. The Testing section is already correct.
Not approving because this is my own PR — GitHub rejects self-approvals. test (linux), test (macos), code-coverage, and both codecov checks are green; test (windows) is still running.
|
Both description points from the follow-up review are applied: the trailer is now No diff changes in that round, so the green checks on |
Cuts 0.76.0. `cargo semver-checks` fails ten lints, so the bump is minor. ## Changelog filing fix Five entries had been appended to the already-tagged `## 0.75.0` section after v0.75.0 shipped (#3977/#2860, #3949 ×2, #3959, #3945). They describe changes that ship *here*, and GitHub's 0.75.0 release notes — built from the tag — never showed them. They are moved into `## 0.76.0` and rewritten to the length ceiling; `## 0.75.0` is restored byte-identical to `git show v0.75.0:CHANGELOG.md`. The mechanism is systemic: a PR appends under whatever heading is currently top, so every post-release PR lands in the shipped section until the next release opens a new one. ## `--execute` template guidance `#3977` switched `--execute` expansion to `ShellEscapeMode::Literal`, which is correct for an argv model. But the migration that v0.53.0–v0.75.0's deprecation warning *printed* — `-x sh -- -c '<old body>'` — splices a template variable into text `sh` re-parses, so a value with spaces word-splits where the old POSIX-escaped model kept it intact. Worst case is `rm -rf` against unintended paths. `--execute`'s help now documents passing the variable as a separate argument and referencing it positionally. `extending.md` already modelled that form, so no recipe changed. ## Validation - `nightly` on the cut-from tip (`baf161bf6`): all 14 jobs green — [run 33538866753](https://github.com/max-sixty/worktrunk/actions/runs/33538866753) - `wt hook pre-merge --yes`: 4713 passed - Data-loss surface reviewed across the 45-commit diff by four independent finders; adjudication in the release thread. No new destructive path — `#3977` removes one, deleting the EXEC directive file from all five shell wrappers. > _This was written by Claude Code on behalf of max-sixty_
worktrunk 0.76.0 Created-by: HarmonybrewBot Commit-by: HarmonybrewBot Merged-by: HarmonybrewBot Description: Created by `brew bump` --- Created with `brew bump-formula-pr`.<details> <summary>release notes</summary> <pre>## Release Notes ### Improved - **`wt switch --execute` takes a program, not a shell string**: `-x` names one program, with everything after `--` passed as literal argv. Worktrunk spawns it as a child rather than running it in your interactive shell, so shell functions and its `cd` no longer reach you. `-x sh -- -c '…'` recovers shell syntax, not functions. (Breaking: existing `-x` strings, plus `WORKTRUNK_DIRECTIVE_EXEC_FILE` and `WORKTRUNK_SHELL`.) ([#3977](max-sixty/worktrunk#3977), closes [#2860](max-sixty/worktrunk#2860), fixes [#3944](max-sixty/worktrunk#3944), thanks @omgreenfield for testing the migration path) - **Worktrunk decides tracking for the branches it creates**: a new branch gets an upstream only when its name matches the remote branch it starts from, whatever `branch.autoSetupMerge` says: `--create release --base origin/release` tracks, `--create feature --base origin/release` does not. Under `autoSetupMerge = false` this previously exited 128 after creating the worktree. ([#3913](max-sixty/worktrunk#3913), [#3950](max-sixty/worktrunk#3950), fixes [#3937](max-sixty/worktrunk#3937), thanks @mjakl for reporting and diagnosing) - **Retired config keys are no longer read**: `commit.generation.template-file` and `squash-template-file` are gone — put the file's contents in `template` or `squash-template`. There is no migration: the key stays, warns as unknown on every load, and the prompt reverts to the built-in default. Setting both `template` and `template-file` used to fail the load; it now loads `template`. `switch.picker.timeout-ms` likewise warns rather than being stripped. (Breaking.) ([#3949](max-sixty/worktrunk#3949)) - **Project aliases and hooks can use `wt switch --execute`**: their bodies previously refused it. Worktrunk now starts the program itself instead of handing shell text to your shell, and the command-approval gate remains the control on project-defined commands. ([#3977](max-sixty/worktrunk#3977)) - **Established automation and LLM customization interfaces are now stable**: `wt step eval`, `wt step for-each`, `wt step prune`, LLM branch summaries, `wt config state vars`, and `commit.generation.template-append` are no longer marked experimental. ([#3949](max-sixty/worktrunk#3949)) - **`wt step prune` stages removals concurrently again**: only the `git worktree remove` teardowns serialize, so dirty checks, fsmonitor shutdown, and the trash rename overlap. The `prune_e2e/live` benchmark median went from 377 ms to 222 ms. ([#3954](max-sixty/worktrunk#3954)) - **`wt config approvals add` reads as a review rather than a warning**: it opens with a cyan `Review 1 command for repo:` instead of a yellow `▲ … needs approval to execute`. The execution-time gate is unchanged. ([#3953](max-sixty/worktrunk#3953)) ### Fixed - **The `wt switch` picker survives a preview longer than 65,535 lines**: skim keeps the pane's line count in a `u16`, so a larger diff aborted `wt` with exit 101 seconds after it painted — on screen the picker looked like it closed by itself. Panes now cap at 60,000 lines and say so. ([#3959](max-sixty/worktrunk#3959), fixes [#3958](max-sixty/worktrunk#3958), thanks @sandertammesoo for reporting and diagnosing) - **The nushell wrapper propagates exit codes without a POSIX shell**: it reported a failing `wt` by spawning `sh`, so on Windows every failing command printed a ``Command `sh` not found`` trace and exit 1 whatever the real code was. Nushell's wrapper is a static file, so rerun `wt config shell install` to pick this up. ([#3945](max-sixty/worktrunk#3945)) - **The picker no longer shows one detached worktree's diff for another**: previews were keyed by branch name, so every detached row shared the key `(detached)`. Rows now key by canonical identity, which also lets `wt remove feature ~/repo.feature` plan that worktree once instead of failing on the second. ([#3926](max-sixty/worktrunk#3926)) - **`wt switch pr:<n>` gives `pre-switch` hooks the real branch**: `branch` and `target` held the literal `pr:3933`, and `target_worktree_path` was unset even when that branch had a worktree. `pr_number` and `pr_url` are now set for same-repo PRs, not only forks. ([#3941](max-sixty/worktrunk#3941), fixes [#3934](max-sixty/worktrunk#3934), thanks @robsonpeixoto for reporting) - **`wt config shell install` no longer replaces an rc file created while it was checking**: the missing-file path refuses to clobber and fails with a rerun hint. A dangling rc symlink is preserved and rejected rather than overwritten. ([#3929](max-sixty/worktrunk#3929)) - **`wt switch --create X --base pr:<n>` works when the PR branch has no local branch**: the fetch writes only the remote-tracking ref, and git won't expand a bare name to one, so the command failed with `No branch, tag, or commit named …`. The base is now named under its remote. ([#3951](max-sixty/worktrunk#3951)) - **`wt switch --create` streams `git worktree add` instead of stalling**: where a sandbox denied the timed wait's pipe or `sigaction`, the command waited out git's entire run and then reported `Failed to wait for command` for work that had already succeeded. ([#3881](max-sixty/worktrunk#3881)) - **Claude Code activity markers stay on the session's launch worktree**: the plugin's marker hooks pass `-C "$CLAUDE_PROJECT_DIR"`, so a `cd` mid-session no longer marks another repository. A session launched outside a repository gets no marker. ([#3956](max-sixty/worktrunk#3956)) ### Documentation - **`--execute` documents how to get a variable into a shell body**: each position is substituted verbatim, so a variable spliced into `sh -c` text is re-parsed by that shell and splits on spaces. The help now shows passing it as a separate argument and referencing it positionally. ([#3977](max-sixty/worktrunk#3977)) - **The clone-local default-branch override is documented**: `wt config state default-branch set` writes `worktrunk.default-branch` to the clone's local git config — machine-local, never committed, and shared by every linked worktree. `set` warns when the branch isn't checked out locally, and `wt list` skips its comparisons until it is. ([#3947](max-sixty/worktrunk#3947), [#3948](max-sixty/worktrunk#3948), fixes [#3946](max-sixty/worktrunk#3946), thanks @danielo515 for the request) - **The docs site renders code with the CLI's own colors**: terminal examples reproduce each snapshot's exact ANSI roles, while command references and the homepage command comparison get clap and shell syntax color from paired light/dark themes. File excerpts gain a path tab, and code wraps instead of scrolling on mobile. ([#3936](max-sixty/worktrunk#3936), [#3938](max-sixty/worktrunk#3938), [#3939](max-sixty/worktrunk#3939), [#3942](max-sixty/worktrunk#3942)) - **The `pre-start` upstream example uses `--base`**: it showed `wt switch --create feature origin/feature`, which `wt switch` rejects. ([#3955](max-sixty/worktrunk#3955)) ### Internal - **Library API rework** (Breaking library API): `cargo-semver-checks` fails ten lints — `RemoteRefProvider` and its provider structs give way to `ForgeKind` dispatch, the `--execute` rework retires the fish and PowerShell `ShellEscapeMode` escapes and `WORKTRUNK_SHELL_ENV_VAR`, `BranchRef` swaps `full_ref`/`worktree_path` for `id`, and accessors including `Branch::unset_upstream` and `ProjectConfig::ci_platform` are gone. ([#3913](max-sixty/worktrunk#3913), [#3926](max-sixty/worktrunk#3926), [#3949](max-sixty/worktrunk#3949), [#3963](max-sixty/worktrunk#3963), [#3977](max-sixty/worktrunk#3977)) - **Hook pipelines and `wt list` collection lost two indirection layers**: the background pipeline carries `PreparedStep` directly, and 15 marker task types collapse into one exhaustive dispatch. The synthesized-`--force` submodule path keeps its dirty re-check and destructive command under one lock, so prune's narrower locking doesn't widen that window. ([#3964](max-sixty/worktrunk#3964), [#3954](max-sixty/worktrunk#3954)) ## Install worktrunk 0.76.0 ### Install prebuilt binaries via shell script ```sh curl --proto '=https' --tlsv1.2 -LsSf https://github.com/max-sixty/worktrunk/releases/download/v0.76.0/worktrunk-installer.sh | sh && wt config shell install ``` ### Install prebuilt binaries via powershell script ```sh powershell -ExecutionPolicy Bypass -c "irm https://github.com/max-sixty/worktrunk/releases/download/v0.76.0/worktrunk-installer.ps1 | iex"; git-wt config shell install ``` ### Install prebuilt binaries via Homebrew ```sh brew install worktrunk && wt config shell install ``` ## Download worktrunk 0.76.0 | File | Platform | Checksum | |--------|----------|----------| | [worktrunk-aarch64-apple-darwin.tar.xz](https://github.com/max-sixty/worktrunk/releases/download/v0.76.0/worktrunk-aarch64-apple-darwin.tar.xz) | Apple Silicon macOS | [checksum](https://github.com/max-sixty/worktrunk/releases/download/v0.76.0/worktrunk-aarch64-apple-darwin.tar.xz.sha256) | | [worktrunk-x86_64-ap See merge request: Harmonybrew/homebrew-core!18482
The nushell wrapper reported a failing
wtby spawningsh -c "exit N", butShell::Nushellis not platform-gated —wt config shell installwrites this wrapper on Windows whenevernuis on PATH, and stock Windows has nosh. It now re-invokes nushell itself, which exists by construction wherever the wrapper is installed. Verified by a new test that restricts PATH tonu,git, andrm; it fails on the old template with the exact reported symptom and passes on the new one.Problem
#3944 was a code-reading finding. Reproduced here by removing the POSIX shell from PATH, which is the only property of Windows this path depends on:
Two things go wrong on every failing
wtcommand, not just--executeones: the wrapper's own source is printed at the user, and the real exit code is flattened to 1 (awtthat exited 3 exits 1). That is the precise opposite of what the branch exists for — its comment says it uses a failing external command specifically to avoid a verbose source trace.Solution
^$nu.current-exe --no-config-file --no-std-lib -c $"exit ($exit_code)".The alternative was to branch on
$nu.os-info.familyand keep^shon Unix, matching the platform branch the temp-file cleanup already uses. One command on every platform is better here: it makes the path Unix CI exercises the same path Windows runs, which is exactly the gap that let this bug exist. The cost is ~4 ms per failing command on Unix (measured withtimeiton nu 0.115: 1.0 ms forsh, 9.4 ms fornu --no-config-file, 4.9 ms with--no-std-libas well), on a path that has already finished its git work and is about to hand control back to the prompt.exitis a core builtin rather than stdlib, so skipping the stdlib doesn't affect propagation.$nu.current-exeand--no-std-libboth predate the 0.96 nushell floor the installer requires (vendor/autoload) — the switch is declared in 0.96.0'ssrc/command.rs.What this does not change
The
--executepath attemplates/nushell.nu#L135still callssh. Exec bodies are POSIX snippets by construction (ShellEscapeMode::Posix), so there is no nushell-native command to hand them to, and the options — requiresh, refuse the nu wrapper on Windows, document the limitation — are a maintainer call. #3944 stays open for it; the code now carries an inline note pointing there, so a reader doesn't take the file to besh-free.Testing
test_nushell_wrapper_failure_needs_no_posix_shellruns the real wrapper through a PTY with PATH restricted to symlinks fornu,git, and thermthe wrapper's Unix cleanup branch spawns externally. It drives two failing commands: awt switch --createof a branch that already exists (exit 1), asserting wt's own error still reaches the terminal with no nushell external-command trace appended; andwt config alias show <unknown>, which exits 2 entirely inside wt, asserting the real code survives rather than flattening to 1. The second case is what covers the exit-code half of the bug — a failedshspawn reports 1, so an exit-1 command alone can't tell the templates apart.Confirmed failing before the fix and passing after:
Before / after
Against the unmodified template:
With the fix, and the rest of the module alongside it:
Also run:
configure_shell(60 passed),config_show(128 passed),test_docs_are_in_sync,cargo fmt --check,cargo clippy --tests --features shell-integration-tests. Theinit_nusnapshot is regenerated.Refs #3944 — automated triage. Deliberately not a closing reference: the
--executehalf of the report is unaddressed here, and the comment this PR adds totemplates/nushell.nupoints readers at that issue.