Skip to content

fix(desktop): load the packaged shell PATH deterministically - #2415

Closed
SawyerHood wants to merge 1 commit into
mainfrom
fix-2343-packaged-shell-path
Closed

SawyerHood wants to merge 1 commit into
mainfrom
fix-2343-packaged-shell-path

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 25, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

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. Two further mechanisms discarded valid PATHs: spawnSync waits 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's status 13) 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. (#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:

  • 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.
  • $SHELL -ilc (5 s budget), then $SHELL -lc (3 s), $SHELL taken verbatim (fallback /bin/zsh on macOS, /bin/bash on Linux). When $SHELL cannot be started (spawn ENOENT/EACCES) and is not the platform default, the stages run once more with the platform default shell.
  • 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, nvm's default Node bin resolved from the alias files without running nvm, fnm, mise/asdf/nodenv/proto shims, n, pnpm home, bun, deno, cargo, go, ~/.local/bin, ~/bin, Homebrew, /usr/local/bin, /snap/bin) are appended, because -lc skips 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 at system adds no nvm bin.
  • Every fallback logs one warn line naming each failed stage, the platform-default retry, 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. 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 resolveUserShellPath still 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 the origin/main source): marker parsing (banners, CRLF, truncated and missing blocks), complete block kept on timeout / status 13, -ilc timeout → -lc fallback, merge with inherited entries, augmentation only on the degraded paths, nvm alias chains (lts/*, partial, system), $SHELL honored 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 --force and pnpm exec turbo run test --filter=@bb/desktop --force on the rebased tip: 35 files / 252 tests pass. Three independent adversarial review rounds. EAP codename scan of origin/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

AGENT GENERATED

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]>
@SawyerHood

Copy link
Copy Markdown
Collaborator Author

Closing. Superseded or out of date; will be redone on current main if still needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Packaged Nightly plugin processes lose PATH for node and gh

1 participant