Skip to content

Use the bundled npm and npx for server-side package-manager calls - #3914

Merged
ymichael merged 3 commits into
mainfrom
bb/explain-issue-3773-thr_pdnnt6x8hg
Sep 19, 2026
Merged

ymichael merged 3 commits into
mainfrom
bb/explain-issue-3773-thr_pdnnt6x8hg

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A macOS Desktop server launched from Finder inherits PATH=/usr/bin:/bin:/usr/sbin:/sbin. Three server-side code paths resolved a package manager through that PATH instead of the npm that bb already bundles, so each failed with ENOENT on any machine without Node on the system PATH — which is exactly the population bb bundles npm for.

bb-app-artifact.ts passed a bare "npm" to execFile, so GET /install/bb-app.tgz threw spawn npm ENOENT and returned HTTP 500, blocking bb machine setup for Modal sandboxes and every other fresh machine enrollment (#3773). server-move/full-artifact.ts carried runPackCommand, a byte-for-byte copy of the same PATH-dependent runner, so a server move failed the same way while packing the full bb-app. skills/registry-skill-install.ts spawned bare npx and failed with spawn npx ENOENT.

What changed

  • apps/server/src/services/install/bb-app-artifact.ts — defaultCommandRunner resolves the bundled npm CLI and runs it under process.execPath, dropping inherited npm script-policy env. This matches runInstallCommand in the plugin install service, which already used this pattern. Exported so the one runner can be shared.
  • apps/server/src/services/server-move/full-artifact.ts — deleted the duplicate runPackCommand; createFullBbAppArtifactService now defaults commandRunner to the shared runner, matching createBbAppArtifactService. environment.ts no longer wires one explicitly.
  • apps/server/src/services/skills/registry-skill-install.ts — runs the bundled npx-cli.js under process.execPath and forwards ELECTRON_RUN_AS_NODE through the env allowlist so it works when the server runs under Electron.
  • packages/plugin-build/src/npm-cli.ts — added resolveBundledNpxCli beside the npm resolver; renamed resolvePluginNpmCli to resolveBundledNpmCli now that it serves more than plugin installs (5 call sites updated).

No wire, CLI, or configuration changes, so no HOST_DAEMON_PROTOCOL_VERSION bump.

Deliberately unchanged: provider-maintenance-kit.ts (npmCommand() installs provider CLIs into the user's global prefix and renders a copy-pasteable command; provider processes get a login-shell PATH), host-daemon/protocol-self-update.ts (installs into the machine's npm prefix, already prepends dirname(process.execPath)), apps/cli (runs in the user's terminal against their project), and install-machine.sh (runs on the target machine). These need the user's npm by design.

How you verified

Tests (each verified to fail before the fix and pass after):

  • apps/server/test/app/bb-app-artifact.test.ts — builds a real artifact with PATH="". Without the fix: Error: spawn npm ENOENT.
  • apps/server/test/server-move/full-artifact.test.ts — same, through createFullBbAppArtifactService and its production default runner.
  • apps/server/test/skills/registry-skill-install.test.ts — asserts the spawned command is process.execPath with an existing npx-cli.js and a forwarded ELECTRON_RUN_AS_NODE. Without the fix: expected 'npx' to be '/…/node'.

Suites: pnpm exec turbo run typecheck lint test --filter='...[origin/main]' — 97 tasks, all green except one pre-existing parallel-load flake in bb-plugin-provider-codex (cancels an open SSE body after a terminal failure event), which passes on its own re-run. @bb/server 2980 pass, @bb/plugin-build 139 pass.

End-to-end against a real packaged install (npm pack + npm install -g --prefix), running the server under Electron with ELECTRON_RUN_AS_NODE=1 and PATH=/usr/bin:/bin:/usr/sbin:/sbin:

  • GET /install/bb-app.tgz — pre-fix HTTP 500 reproducing the issue's log verbatim ("message": "spawn npm ENOENT", spawnargs: ["pack","--pack-destination",…]); post-fix HTTP 200, byte-identical artifact (sha256=779a2b46…) across node/Electron hosts and restricted/full PATH. The downloaded artifact installs and runs (bb --version → 0.43.1).
  • bb machine setup over the network onto a second Mac — ✓ Downloaded the server's bb-app package → ✓ Installed → ✓ Joined successfully → ● bb machine is ready.
  • A real bb server move to that machine — the Packing bb 0.43.1 step produced bb-app-full-0.43.1-9fd32ebe….tgz (47 MB) with no npm on PATH, and the target downloaded, installed, and took over.

Known gap, pre-existing and not a regression: registry skill install now gets past spawn npx ENOENT and runs the bundled npx, but the third-party skills CLI it fetches carries #!/usr/bin/env node, so on a bare PATH it fails one step later with env: node: No such file or directory. That is #2343 (the packaged login-shell PATH probe timing out), plus a residual gap for users with no Node at all. Left for a follow-up.

Fixes #3773

🤖 Generated with Claude Code

AGENT GENERATED

ymichael and others added 3 commits September 18, 2026 10:33
The bb-app artifact builder passed a bare "npm" to execFile, so packing
the enrolled host runtime required npm on the server process's PATH. A
macOS Desktop server launched from Finder inherits PATH=/usr/bin:/bin:
/usr/sbin:/sbin, so /install/bb-app.tgz threw `spawn npm ENOENT` and
returned HTTP 500, blocking `bb machine setup` for Modal sandboxes and
any other fresh machine enrollment.

Resolve npm through the bundled CLI and run it under process.execPath,
matching runInstallCommand in the plugin install service, and drop
inherited npm script-policy env. Rename resolvePluginNpmCli to
resolveBundledNpmCli now that it serves more than plugin installs.

Fixes #3773

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The bb-app artifact fix left two more server paths resolving a package
manager through PATH, so both still break on a GUI-launched macOS
Desktop whose PATH is /usr/bin:/bin:/usr/sbin:/sbin.

server-move's runPackCommand was a byte-for-byte copy of the artifact
service's PATH-dependent runner. Delete it and default
createFullBbAppArtifactService to the shared runner, matching
createBbAppArtifactService.

Registry skill installs spawned bare npx with an env allowlist. Run the
bundled npx-cli.js under process.execPath and forward
ELECTRON_RUN_AS_NODE so it works when the server runs under Electron.

Add resolveBundledNpxCli beside resolveBundledNpmCli, and widen the
registry-skill-install child_process mock to pass through the real
module instead of only spawn.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
#3566 shipped because electron-builder dropped part of the bundled npm tree
while source builds and the bb-app tarball smoke stayed green. The afterPack
gate added to catch that only resolved and ran bin/npm-cli.js, so the
bin/npx-cli.js this branch now depends on had no packaging coverage.

Resolve both CLIs through insideArtifact, which realpaths them and asserts
containment, and run npx --version through the packaged Electron with an empty
PATH exactly as npm is checked. This also adds a missing-file check for
npm-cli.js, which was previously only path-joined.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@ymichael

Copy link
Copy Markdown
Collaborator Author

Added a third commit after reviewing how #3566 broke 0.43.0.

That release shipped bundled npm without its nested node_modules because electron-builder dropped it, and it got through because source builds and the bb-app tarball smoke both passed — packaging was the only broken mode. The afterPack gate added by #3590 (smoke-packaged-npm.mjs) exists to stop exactly that, but it only resolved and ran bin/npm-cli.js. This branch adds a runtime dependency on bin/npx-cli.js, which had no packaging coverage.

a0076e5e resolves both CLIs through insideArtifact (realpath + containment, so a missing file fails the gate) and runs npx --version through the packaged Electron with an empty PATH, mirroring the npm check. It also adds a missing-file check for npm-cli.js, which was previously only path-joined.

Verified against the installed signed release:

  • gate passes — Packaged npm 11.16.0: 65 direct dependencies, 144 bundled packages, 348 CJS/ESM dependency edges; offline plugin dependency install/update passed with Electron 41.7.0 (Node 24.15.0) and empty PATH; packaged resources unchanged.
  • negative control — pointing npxCli at a non-existent filename fails the gate with ENOENT: no such file or directory, realpath .../npm/bin/npx-cli-MISSING.js
  • both bundled CLIs run standalone under the release Electron with PATH="": npm-cli.js --version and npx-cli.js --version each print 11.16.0

Caveat worth stating plainly: the end-to-end validation in the PR description used npm pack + npm install -g, which is the same mode that stayed green while #3566 was broken. A real turbo run package --filter=@bb/desktop has not been run on this branch; the gate above is what would catch a packaging regression.

AGENT GENERATED

@ymichael

Copy link
Copy Markdown
Collaborator Author

Ran the real packaging, closing the gap I flagged above.

CSC_IDENTITY_AUTO_DISCOVERY=false pnpm exec turbo run package --filter=@bb/desktop — 51/51 tasks passed. The afterPack gate ran against the freshly built artifact with the Electron that will actually ship:

Packaged npm 11.16.0: 65 direct dependencies, 144 bundled packages, 348 CJS/ESM
dependency edges; offline plugin dependency install/update passed with
Electron 44.3.0 (Node 24.20.0) and empty PATH; packaged resources unchanged.

Then drove the packaged app itself. Ran its server as Desktop does — ELECTRON_RUN_AS_NODE=1, PATH=/usr/bin:/bin:/usr/sbin:/sbin — straight out of release/mac-arm64/bb.app:

This was the mode that let #3566 ship — source builds and the tarball smoke passed while packaging was broken. It is now exercised directly.

AGENT GENERATED

@ymichael
ymichael merged commit 4fa1d86 into main Sep 19, 2026
16 checks passed
@ymichael
ymichael deleted the bb/explain-issue-3773-thr_pdnnt6x8hg branch September 19, 2026 04:40
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.

macOS Desktop 0.43.1: /install/bb-app.tgz returns HTTP 500 with spawn npm ENOENT, blocking Modal provisioning

1 participant