Repository navigation
Keep @parcel/watcher out of the daemon bundle's static imports - #1930
Conversation
The in-process watcher fallback dynamically imported an internal module whose static @parcel/watcher import esbuild hoisted to the top of the daemon bundle, so watcher.node loaded before pty.node at daemon startup. On macOS dyld coalesces the Napi wrapper symbol to watcher.node's exception-free copy and a failed pty spawn aborts the daemon. Import the external package directly so the import stays lazy in the bundle, and add a regression test that bundles @bb/host-watcher with the daemon settings and asserts no static import and no watcher.node dlopen. Fixes #1873 Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
🚨 SLOP COP 🚨 · review
I am the SlopCop. I am reviewing this pull request for security, code quality, performance, architecture, and duplicate code.
I will also run the relevant tests. I will test the full product path if this backend-only change has a browser path.
| .split("\n") | ||
| .filter((line) => line.startsWith("[dlopen] ")); | ||
| expect( | ||
| loadedAddons.filter((line) => line.includes("@parcel/watcher")), |
There was a problem hiding this comment.
🚨 slopcop/review — [P2] Match the native package path that pnpm actually loads
This filter does not detect the addon in this repository. On macOS, a direct import logged this path:
node_modules/.pnpm/@[email protected]/node_modules/@parcel/watcher-darwin-arm64/watcher.node
That line does not contain @parcel/watcher, so this assertion returns an empty array even when Parcel loads. Please match watcher.node or both pnpm and package path forms. This keeps the runtime regression check effective.
There was a problem hiding this comment.
Hardened in 04778c0: the filter now matches /@parcel[/+]watcher/, which covers both the pnpm store path (.pnpm/@parcel+watcher-<platform>@<ver>/...) and the package path (@parcel/watcher-<platform>/watcher.node). Note the original includes("@parcel/watcher") did match the macOS path too (@parcel/watcher-darwin-arm64 contains that substring), and the pre-fix Linux run in the PR body shows the assertion catching watcher.node; the regex just makes the intent explicit.
There was a problem hiding this comment.
🚨 SLOP COP 🚨 · review
Plain English summary
This change keeps a native file watcher out of the main daemon until the fallback needs it. This prevents a macOS library conflict. The normal watcher still runs in its separate child process.
Finding
- [P2] The native load assertion does not match pnpm paths. pnpm uses
@parcel+watcher, but the test checks@parcel/watcher. The test can pass whilewatcher.nodeloads. I left an inline comment with the exact observed path and a suggested fix.
Review results
- Security: I found no issue. The module name is fixed, and no user input controls module resolution.
- Code quality: The direct external dynamic import is correct. It preserves the intended lazy load.
- Architecture: I found no shared native loader to reuse.
real-parcel-watcher.tsis now a one-use alias and could move into the child entry later. - Test architecture: The new test copies some daemon bundle settings. A later production setting change could cause drift. This is not a current failure.
- Performance: I found no regression. The fallback pays one cached dynamic import only when the fallback starts.
- Product test: This change has no browser route. I tested the actual daemon bundle and the native module path instead.
Validation
- The host-daemon suite passed 585 tests.
- The host-watcher suite passed 45 tests.
- Both package type checks passed through Turbo.
- The daemon build passed. Its main bundle contains only the dynamic Parcel import.
- The watcher child bundle contains the expected static Parcel import.
- All current GitHub checks passed.
Review result: comment only. I did not approve or request changes.
Co-Authored-By: Claude <[email protected]>
What was wrong
The host daemon is supposed to keep the native
@parcel/watcheraddon out of the parent process (parcel runs in a forked child). The in-process fallback inpackages/host-watcher/src/parcel-watcher-backend.tsreached the addon throughawait import("./real-parcel-watcher.js"). esbuild inlines a dynamic import of an internal module and hoists that module's staticimport parcelWatcher from "@parcel/watcher"to the top level ofdist/daemon-bundle.mjs. The daemon therefore loadedwatcher.node(built withNAPI_DISABLE_CPP_EXCEPTIONS) beforepty.node(NAPI_CPP_EXCEPTIONS) at startup. On macOS, dyld coalesces the identical weakNapi::details::CallbackData<...>::Wrappersymbol across images to watcher.node's try/catch-less copy. WhenPtyForkthrowsNapi::Erroron a failed spawn, nothing catches it,std::terminateruns, and the app dies with SIGABRT.Issue: #1873. Investigation report: https://get-bb.github.io/reports/issues/1873.html
What changed
packages/host-watcher/src/parcel-watcher-backend.ts: the in-process fallback now doesconst { default: parcelWatcher } = await import("@parcel/watcher"). A dynamic import of the external package stays a realimport("@parcel/watcher")in the esbuild ESM bundle, sowatcher.nodeloads only when a watcher is needed in-process.packages/host-watcher/src/real-parcel-watcher.ts: kept for the forked watcher child entry; its comment now explains why daemon-bundle code must not dynamically import it.apps/host-daemon/test/parcel-watcher-not-loaded-in-parent.test.ts(new): bundles@bb/host-watcherwith the daemon bundle's esbuild settings, then asserts (1) the output has no static top-level@parcel/watcherimport and still has a dynamicimport("@parcel/watcher"), and (2) importing that bundle in a childnodewith aprocess.dlopenhook loads no@parcel/watcheraddon.No wire shapes changed, so
HOST_DAEMON_PROTOCOL_VERSIONwas not bumped. No CLI/guide/doc surfaces are affected.How you verified
The new test fails before the fix (both assertions, for the right reason):
After the fix:
Real daemon bundle, rebuilt with the fix (
pnpm exec turbo run build --filter=@bb/host-daemon), then run with the report'sprocess.dlopenlogger andNODE_PATHunset:Before the fix the same bundle contained
import VYr from"@parcel/watcher"and the report'sdaemon-bundle-addon-load-order.logshowswatcher.nodeas dlopen #1 andpty.nodeas #2.Package suites and typecheck:
pnpm exec turbo run test typecheck --filter=@bb/host-daemon --filter=@bb/host-watcher --force→ 5/5 tasks successful;@bb/host-watcher45 tests passed,@bb/host-daemon585 tests passed.macOS was not re-run. This was verified on Linux only. On Linux the symptom differs (the report shows the same symbol coalescing produces a SIGSEGV analog under
RTLD_GLOBAL, and Node's defaultRTLD_LOCALdlopen avoids it), so the macOS SIGABRT itself was not reproduced here. The fix is still correct because the crash requireswatcher.nodeto be resident in the daemon process beforepty.nodeloads, and the root cause of that is the static import in the bundle. The test and the dlopen log above show that with this change the daemon process loads no@parcel/watcheraddon at startup, so dyld has no watcher.node copy of theWrappersymbol to coalesce with when node-pty's image loads; node-pty then uses its own exception-aware copy, which catches theNapi::Erroras intended. The forked watcher child (bb-parcel-watcher-child.mjs) still loadswatcher.node, which is the intended isolation.Fixes #1873