Skip to content

Keep @parcel/watcher out of the daemon bundle's static imports - #1930

Merged
SawyerHood merged 2 commits into
mainfrom
bb/fix-1873-thr_tfc2rxph5j
Aug 19, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
bb/fix-1873-thr_tfc2rxph5j

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

What was wrong

The host daemon is supposed to keep the native @parcel/watcher addon out of the parent process (parcel runs in a forked child). The in-process fallback in packages/host-watcher/src/parcel-watcher-backend.ts reached the addon through await import("./real-parcel-watcher.js"). esbuild inlines a dynamic import of an internal module and hoists that module's static import parcelWatcher from "@parcel/watcher" to the top level of dist/daemon-bundle.mjs. The daemon therefore loaded watcher.node (built with NAPI_DISABLE_CPP_EXCEPTIONS) before pty.node (NAPI_CPP_EXCEPTIONS) at startup. On macOS, dyld coalesces the identical weak Napi::details::CallbackData<...>::Wrapper symbol across images to watcher.node's try/catch-less copy. When PtyFork throws Napi::Error on a failed spawn, nothing catches it, std::terminate runs, 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 does const { default: parcelWatcher } = await import("@parcel/watcher"). A dynamic import of the external package stays a real import("@parcel/watcher") in the esbuild ESM bundle, so watcher.node loads 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-watcher with the daemon bundle's esbuild settings, then asserts (1) the output has no static top-level @parcel/watcher import and still has a dynamic import("@parcel/watcher"), and (2) importing that bundle in a child node with a process.dlopen hook loads no @parcel/watcher addon.

No wire shapes changed, so HOST_DAEMON_PROTOCOL_VERSION was 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):

 ❯  @bb/host-daemon  test/parcel-watcher-not-loaded-in-parent.test.ts (2 tests | 2 failed) 175ms
     × does not hoist a static import of @parcel/watcher 36ms
     × does not dlopen a @parcel/watcher addon when the bundle is imported 137ms

AssertionError: static top-level import(s) of @parcel/watcher found in the bundle; watcher.node would load at daemon startup:
import parcelWatcher from "@parcel/watcher";: expected [ Array(1) ] to be null

AssertionError: @parcel/watcher addon loaded on import:
[dlopen] <repo>/node_modules/.pnpm/@[email protected]/node_modules/@parcel/watcher-linux-x64-glibc/watcher.node: expected [ Array(1) ] to deeply equal []

After the fix:

 Test Files  1 passed (1)
      Tests  2 passed (2)

Real daemon bundle, rebuilt with the fix (pnpm exec turbo run build --filter=@bb/host-daemon), then run with the report's process.dlopen logger and NODE_PATH unset:

$ grep -c 'from"@parcel/watcher"' dist/daemon-bundle.mjs
0
$ node --require log-dlopen.cjs dist/daemon-bundle.mjs --definitely-not-a-flag
[dlopen #1] <repo>/node_modules/.pnpm/[email protected]/node_modules/node-pty/build/Release/pty.node
[exit] code=1 addons loaded=1

Before the fix the same bundle contained import VYr from"@parcel/watcher" and the report's daemon-bundle-addon-load-order.log shows watcher.node as dlopen #1 and pty.node as #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-watcher 45 tests passed, @bb/host-daemon 585 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 default RTLD_LOCAL dlopen avoids it), so the macOS SIGABRT itself was not reproduced here. The fix is still correct because the crash requires watcher.node to be resident in the daemon process before pty.node loads, 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/watcher addon at startup, so dyld has no watcher.node copy of the Wrapper symbol to coalesce with when node-pty's image loads; node-pty then uses its own exception-aware copy, which catches the Napi::Error as intended. The forked watcher child (bb-parcel-watcher-child.mjs) still loads watcher.node, which is the intended isolation.

Fixes #1873

AGENT GENERATED: by Claude Opus 5

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

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 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")),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 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 while watcher.node loads. 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.ts is 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.

@SawyerHood
SawyerHood merged commit f0b43b3 into main Aug 19, 2026
12 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1873-thr_tfc2rxph5j branch August 19, 2026 16:13
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.

Uncaught C++ exception from PtyFork crashes the whole app (SIGABRT, no JS error frame)

1 participant