Repository navigation
fix(desktop): load the packaged shell PATH deterministically - #2415
Closed
SawyerHood wants to merge 1 commit into
Closed
SawyerHood wants to merge 1 commit into
SawyerHood wants to merge 1 commit into
Conversation
A packaged macOS bb launched from the GUI inherits launchd's restricted PATH (/usr/bin:/bin:/usr/sbin:/sbin). ensurePackagedUserShellPath was a single `/bin/zsh -ilc 'printf "%s" "$PATH"'` spawnSync with a 2,000 ms timeout, and every failure branch (ETIMEDOUT, signal, non-zero status, empty stdout) kept the inherited PATH. spawnSync also waits for the pipes to close, so a login file that leaves a background job attached to stdout reports ETIMEDOUT even after the shell exited and printed its PATH, and raw stdout was used as the PATH, so a banner or `nvm use` line became part of the first entry. bb-app, the server, the host daemon, and in-process server plugins inherit process.env.PATH through startOwnedRuntime -> startBbAppProcess, so every child lost `node` and `gh` while the app looked healthy, and the only warning went to the Electron main process stderr, which a Dock launch discards. The probe is now staged, fenced, and merging: - The shell prints its PATH between marker lines. A complete block is authoritative even when the shell then times out, is signalled, or exits non-zero; everything else on stdout is ignored. - The default spawn is async: stdin closed, settle on the shell's exit plus a 250 ms pipe grace instead of on pipe close, SIGKILL at the budget. A lingering background job no longer costs the whole budget and a TERM-trapping rc file cannot extend it. - `$SHELL -ilc` with a 5 s budget, then `$SHELL -lc` with 3 s, where `$SHELL` is the launcher-provided login shell taken verbatim (fallback /bin/zsh on macOS, /bin/bash on Linux), matching the host daemon's resolver. When `$SHELL` itself cannot be started (spawn ENOENT/EACCES: an uninstalled login shell or a stale account record) and is not the platform default, the stages run once more with the platform default shell; the missing shell is not retried with `-lc` since that would fail identically. - A probed PATH is merged with the inherited one (probed entries first, then inherited entries the shell did not report). - When an earlier stage failed, well-known tool directories that exist on disk (~/.volta/bin, nvm's default Node bin resolved from the alias files without running nvm, fnm's default alias, the mise/asdf/nodenv/proto shims, ~/.n/bin, pnpm's home, ~/.bun/bin, ~/.deno/bin, ~/.cargo/bin, ~/go/bin, ~/.local/bin, ~/bin, Homebrew, /usr/local/bin, /snap/bin on Linux) are appended to the probed PATH, because `-lc` skips the rc file where nvm and Volta install their hooks; when every stage fails they are appended to the inherited PATH. They are appended, so they only order among themselves and never shadow a tool the shell or launcher already put on PATH. An nvm default alias chain that ends at `system` (or `iojs`/`unstable`) adds no nvm bin: the user opted out of nvm's node. - Every fallback logs one warn line naming each failed stage with its shell, the platform-default retry when it happened, and any appended directory, to stderr and to desktop.log in the log directory. - The probe runs after the single-instance lock and handler registration and before app.whenReady(), so a duplicate launch quits without paying for it and the repair still precedes initializeRuntime, the only path that spawns a child. Linux goes through the same stages and augmentation. No server/daemon wire change, so HOST_DAEMON_PROTOCOL_VERSION is unchanged. Scope: this change covers PATH acquisition in the desktop main process and its propagation to bb-app and everything bb-app spawns. The issue's second ask, a visible degraded state in the affected plugin reader when a required executable is still missing, is a separate feature in the plugin/Companion reader and is left for a follow-up. "Fixes" rather than "Refs" because the reported defect, packaged processes silently losing `node` and `gh`, is resolved end to end with regression coverage for the timeout fallback, the missing-shell retry, and child inheritance. Fixes #2343 Co-Authored-By: Claude <[email protected]>
Collaborator
Author
|
Closing. Superseded or out of date; will be redone on current main if still needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
A packaged macOS bb launched from the GUI inherits launchd's restricted PATH (
/usr/bin:/bin:/usr/sbin:/sbin).ensurePackagedUserShellPathwas a single/bin/zsh -ilc 'printf "%s" "$PATH"'spawnSyncwith a 2,000 ms timeout, and every failure branch (ETIMEDOUT, signal, non-zero status, empty stdout) kept the inherited PATH. Two further mechanisms discarded valid PATHs:spawnSyncwaits for the pipes to close, so a login file that leaves a background job attached to stdout reports ETIMEDOUT (with a numeric status, exactly the reporter'sstatus 13) even after the shell exited and printed its PATH; and raw stdout was used as the PATH, so a banner ornvm useline became part of the first entry. bb-app, the server, the host daemon, and in-process server plugins inheritprocess.env.PATHthroughstartOwnedRuntime → startBbAppProcess, so every child lostnodeandghwhile the app looked healthy, and the only warning went to the Electron main-process stderr, which a Dock launch discards. (#2343)What changed
apps/desktop/src/desktop-shell-path.ts(probe rewritten; pure, injectable),apps/desktop/src/main.ts(call site),apps/desktop/README.md:$SHELL -ilc(5 s budget), then$SHELL -lc(3 s),$SHELLtaken verbatim (fallback/bin/zshon macOS,/bin/bashon Linux). When$SHELLcannot be started (spawn ENOENT/EACCES) and is not the platform default, the stages run once more with the platform default shell.~/.local/bin,~/bin, Homebrew,/usr/local/bin,/snap/bin) are appended, because-lcskips the rc file where nvm and Volta install their hooks. Appended entries never shadow a tool the shell already put on PATH. An nvm default alias ending atsystemadds no nvm bin.desktop.login the log directory.app.whenReady(), so a duplicate launch quits without paying for it and the repair still precedesinitializeRuntime, the only path that spawns a child.Linux goes through the same stages and augmentation. No server/daemon wire change, so
HOST_DAEMON_PROTOCOL_VERSIONis unchanged. Worst-case blocked startup is ~8.5 s and only when the interactive shell hangs before printing.Scope: PATH acquisition in the desktop main process and its propagation to bb-app and everything bb-app spawns. The issue's second ask, a visible degraded state in the affected plugin reader when a required executable is still missing, is a separate feature and is left for a follow-up. Related follow-up: the host daemon's own
resolveUserShellPathstill settles on pipe close and has no augmentation, so daemons started by launchd/systemd without the desktop keep the old behavior.How you verified
apps/desktop/test/desktop-shell-path.test.ts(28 tests; 20 of them fail against theorigin/mainsource): marker parsing (banners, CRLF, truncated and missing blocks), complete block kept on timeout / status 13,-ilctimeout →-lcfallback, merge with inherited entries, augmentation only on the degraded paths, nvm alias chains (lts/*, partial,system),$SHELLhonored and the platform-default retry on ENOENT/EACCES, Linux parity. Three real-shell tests (bash) exercise the default spawn end to end: settle on exit with a background job holding stdout, SIGKILL at budget for a TERM-trapping shell, and a login shell that prints a banner and leaves a background job (run 3× under turbo, no flake). A pipe-close-settling regression was proven to fail the shape assertions deterministically.Commands:
pnpm exec turbo run typecheck --filter=@bb/desktop --forceandpnpm exec turbo run test --filter=@bb/desktop --forceon the rebased tip: 35 files / 252 tests pass. Three independent adversarial review rounds. EAP codename scan oforigin/main..HEAD: clean.Not done: no packaged macOS smoke (this machine is Linux); the default spawn and marker command were exercised against real bash shells instead.
Fixes #2343