Skip to content

fix(build): rebuild @parcel/watcher from source for Electron N-API compatibility - #8580

Closed
LesleyMurfin wants to merge 3 commits into
stablyai:mainfrom
LesleyMurfin:fix/parcel-watcher-napi-appimage
Closed

LesleyMurfin wants to merge 3 commits into
stablyai:mainfrom
LesleyMurfin:fix/parcel-watcher-napi-appimage

Conversation

@LesleyMurfin

@LesleyMurfin LesleyMurfin commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #8540. The @parcel/watcher-<platform>-<arch> optionalDependency prebuilds published upstream register via the older constructor-based N-API path and omit the modern napi_register_module_v1 export. Electron's context-aware module loader requires that export, so the packaged Linux AppImage's watcher.node fails to load — even though the runtime dependency closure fix from #4851 / #4860 correctly ships the package files.

Confirmed empirically before fixing: nm -D node_modules/@parcel/watcher-linux-x64-glibc/watcher.node | grep napi_register_module_v1 returns nothing on the currently published @parcel/watcher@2.5.6 prebuild, while its own source (src/binding.cc:268) does use NODE_API_MODULE(watcher, Init) — meaning a from-source rebuild against Electron's headers produces a correctly-registering binary.

No visual change.

Changes

  • config/scripts/rebuild-native-deps.mjs: add @parcel/watcher to the Electron-targeted native rebuild (NATIVE_MODULES) for all three packaged platforms (darwin/win32/linux). After @electron/rebuild compiles node_modules/@parcel/watcher/build/Release/watcher.node against Electron's headers, copy it over the matching @parcel/watcher-<platform>-<arch>[-glibc|-musl] subpackage's watcher.node — index.js's require() resolution order tries that subpackage before falling back to a local build, so the platform subpackage must be the one carrying the fix.
  • config/packaged-runtime-node-modules.cjs: add verifyPackagedParcelWatcherBinding, called from the afterPack hook. It asserts the packaged Linux watcher.node actually exports napi_register_module_v1, failing the build early instead of silently shipping a broken binary. Scoped to Linux only: known-good N-API prebuilds on darwin (e.g. @napi-rs/canvas's darwin-x64 binary) legitimately omit this export with no observed runtime failure there, so a cross-platform gate would false-fail correct macOS/Windows builds.
  • config/electron-builder.config.cjs: wire the new verification into afterPack.
  • Test coverage added/updated in config/scripts/rebuild-native-deps.test.mjs and config/scripts/electron-builder-config.test.mjs for the copy-into-subpackage behavior and the Linux-scoped verification gate (pass/fail/no-op cases).

AI Code Review Summary

  • Cross-platform compatibility: the rebuild-and-copy step in rebuild-native-deps.mjs runs on all three packaged platforms (darwin/win32/linux) via the existing rebuildPlatform/osPlatform() plumbing, so darwin and win32 builds also get a freshly-rebuilt, correctly-registering watcher.node. The new packaging-time verification gate (verifyPackagedParcelWatcherBinding) is intentionally Linux-only, because known-good darwin N-API prebuilds (e.g. @napi-rs/canvas) legitimately omit napi_register_module_v1 with no observed runtime failure — a cross-platform gate would false-fail correct macOS/Windows builds.
  • SSH/remote/local compatibility: this is a build-time/packaging-time change only (native module rebuild + afterPack verification). It does not touch any runtime code path Orca executes against local, remote, or SSH-backed repositories, so there's no SSH/remote-specific behavior to test here.
  • Supported agent and integration compatibility: N/A — no agent, CLI-agent-integration, or git-provider code is touched by this change.
  • Performance risk: adds one additional native module (@parcel/watcher) to the existing @electron/rebuild invocation and one directory scan + file read in afterPack. Both are one-time costs at packaging time, not at app runtime — no measurable user-facing performance impact.
  • UI quality: N/A, no UI change (see "No visual change" above).
  • Security risk: rebuilds @parcel/watcher from its own already-vendored npm source against Electron's official headers (https://electronjs.org/headers/) — the same trusted source and rebuild mechanism already used for node-pty and cpu-features in this file. No new dependency, no new network target, no expanded attack surface.

Test plan

  • pnpm lint — clean (pre-existing warnings only, in files unrelated to this change: src/main/providers/ssh-git-provider.test.ts, src/main/browser/agent-browser-bridge.test.ts, src/renderer/src/components/terminal-pane/keyboard-handlers.ts)
  • pnpm typecheck — clean, no errors
  • pnpm test — 29,153 passed, 40 skipped, 7 pre-existing failures in src/relay/pty-shell-launch.test.ts (4) and src/main/providers/local-pty-shell-ready.test.ts (3). All 7 are bash-OSC133-lifecycle assertions (expect(output.split(oscC)).toHaveLength(4) getting 5) unrelated to this change — verified they reproduce identically on a clean upstream/main checkout with none of this PR's changes applied, in an isolated git worktree. Consistent with a local bash-version difference on the machine running the tests, not a regression from this PR (this PR touches only config/electron-builder.config.cjs, config/packaged-runtime-node-modules.cjs, and the two config/scripts/*.mjs/*.test.mjs files listed above — none of which are anywhere near PTY/bash prompt handling).
  • pnpm build — full build:desktop + build:native, exit 0
  • CI: Linux AppImage build should now fail-fast in afterPack if napi_register_module_v1 is ever missing again, and should succeed once the rebuild is wired in

X (Twitter) handle: @LesleyMurfin

…mpatibility

Upstream's published @parcel/watcher-<platform> prebuilds register via the
older constructor-based N-API path and omit the modern
napi_register_module_v1 export, which Electron's context-aware module
loader requires. This makes the packaged Linux AppImage's watcher.node fail
to load (stablyai#8540), even though the runtime dependency closure fix
in stablyai#4851/stablyai#4860 correctly ships the package.

- rebuild-native-deps.mjs: add @parcel/watcher to the Electron-targeted
  native rebuild, then copy the rebuilt build/Release/watcher.node over the
  matching @parcel/watcher-<platform>-<arch>[-libc] subpackage, since
  index.js resolves that subpackage before its local-build fallback.
- packaged-runtime-node-modules.cjs: verify the packaged Linux watcher.node
  actually exports napi_register_module_v1 in the afterPack hook, failing
  the build early instead of shipping a broken binary. Scoped to Linux only:
  known-good N-API prebuilds on darwin (e.g. @napi-rs/canvas) legitimately
  omit this export with no observed runtime failure there.
@coderabbitai

coderabbitai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The packaging configuration now verifies Linux Parcel watcher bindings after packaging. Native dependency rebuilding includes @parcel/watcher, copies Electron-targeted binaries into platform-specific optional packages, and detects Linux libc variants. Tests cover rebuild selection, binding replacement, hard-linked files, absent packages, fake rebuild output, and packaged binding validation across Linux and macOS.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly matches the main change: rebuilding @parcel/watcher from source for Electron compatibility.
Description check ✅ Passed The description covers the main change, tests, AI review, and security notes, with only minor template-heading differences.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
config/scripts/rebuild-native-deps.test.mjs (1)

499-520: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Missing test coverage for the musl branch of parcelWatcherSubpackageName.

writeFakeParcelWatcherSource hardcodes familySync() to return null (glibc branch), so the -musl suffix path in parcelWatcherSubpackageName (config/scripts/rebuild-native-deps.mjs Lines 200-213) is never exercised by any test. Given this is the most novel/complex logic added in this PR, consider parameterizing the fake detect-libc family and adding a musl-targeted test alongside the existing glibc one.

🧪 Proposed direction
-function writeFakeParcelWatcherSource(projectDir) {
+function writeFakeParcelWatcherSource(projectDir, { libcFamily = null } = {}) {
   const watcherDir = join(projectDir, 'node_modules', '`@parcel`', 'watcher')
   ...
   writeFileSync(
     join(detectLibcDir, 'index.js'),
-    "exports.MUSL = 'musl'\nexports.familySync = function familySync() { return null }\n"
+    `exports.MUSL = 'musl'\nexports.familySync = function familySync() { return ${JSON.stringify(libcFamily)} }\n`
   )
 }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: ad0db646-4797-4b54-b195-6b44a6c836bd

📥 Commits

Reviewing files that changed from the base of the PR and between 7b3103f and 064f519.

📒 Files selected for processing (5)
  • config/electron-builder.config.cjs
  • config/packaged-runtime-node-modules.cjs
  • config/scripts/electron-builder-config.test.mjs
  • config/scripts/rebuild-native-deps.mjs
  • config/scripts/rebuild-native-deps.test.mjs

Comment on lines +200 to +213
function parcelWatcherSubpackageName(platform, arch) {
let name = `@parcel/watcher-${platform}-${arch}`
if (platform === 'linux') {
// Why: resolve detect-libc from @parcel/watcher's own dependency closure
// rather than assuming it is hoisted to the project root, since pnpm does
// not guarantee that for transitive dependencies.
const requireFromParcelWatcher = createRequire(
resolve(projectDir, 'node_modules/@parcel/watcher/package.json')
)
const { familySync, MUSL } = requireFromParcelWatcher('detect-libc')
name += familySync() === MUSL ? '-musl' : '-glibc'
}
return name
}

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.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n## File map\n'
git ls-files config/scripts/rebuild-native-deps.mjs

printf '\n## Relevant ranges\n'
wc -l config/scripts/rebuild-native-deps.mjs
sed -n '1,280p' config/scripts/rebuild-native-deps.mjs

printf '\n## Call sites / option parsing\n'
rg -n "parcelWatcherSubpackageName|--platform|--arch|platform.*arch|arch.*platform|detect-libc" config/scripts/rebuild-native-deps.mjs

printf '\n## Nearby tests or docs\n'
rg -n "rebuild-native-deps|parcelWatcherSubpackageName|detect-libc|`@parcel/watcher-`" -S . --glob '!**/node_modules/**'

Repository: stablyai/orca

Length of output: 18418


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect any local package metadata for `@parcel/watcher` and variant install layout.
rg -n "\"`@parcel/watcher`\"|\"`@parcel/watcher-`" package.json pnpm-lock.yaml .npmrc . -S --glob '!**/node_modules/**' || true

# Look for how the script resolves installed subpackages.
rg -n "node_modules/@parcel/watcher|watcher-linux|watcher-darwin|watcher-win32|glibc|musl" config scripts . -S --glob '!**/node_modules/**' || true

Repository: stablyai/orca

Length of output: 19701


🌐 Web query:

detect-libc familySync current machine libc family documentation

💡 Result:

The detect-libc Node.js module provides the familySync function to synchronously identify the C standard library (libc) family in use on a Linux system [1][2]. Usage and Details: - Function Signature: familySync: string | null [1][2] - Return Values: - 'glibc' if the system uses GNU glibc [1][3]. - 'musl' if the system uses MUSL libc [1][3]. - null if the libc family cannot be determined or if the code is running on a non-Linux platform [1][2]. The library also exports constants GLIBC and MUSL, which hold the strings 'glibc' and 'musl' respectively, allowing for safer comparisons [1][4]. Example usage: const { familySync, GLIBC, MUSL } = require('detect-libc'); switch (familySync) { case GLIBC: // Handle glibc break; case MUSL: // Handle musl break; case null: // Handle unknown or non-Linux break; } [2][4][3] This function is the synchronous counterpart to the asynchronous family function and is part of the detect-libc package, which currently supports detection of GNU glibc and MUSL libc [1][5][2].

Citations:


Use the target libc when resolving @parcel/watcher’s subpackage. detect-libc.familySync() reflects the build host, so a Linux cross-build can choose the wrong @parcel/watcher-<platform>-<arch>-{glibc,musl} package and patch the wrong binary or skip the real target package entirely. Derive the suffix from the target install, or make it explicit.

@brennanb2025

Copy link
Copy Markdown
Contributor

Taking a look!

…package and avoid pnpm store corruption

The afterPack napi_register_module_v1 gate checked every packaged
@parcel/watcher-linux* subpackage, but the rebuild only replaces the one
matching this build's arch and the build host's libc. Packaging keeps all
installed linux prebuilds (glibc/musl x64/arm64) and the ones we don't rebuild
still ship the upstream constructor-based binary that lacks the export, so the
gate threw on a sibling and failed the Linux AppImage build. Scope the gate to
the exact subpackage the rebuild produces (watcher-linux-<arch>-<host-libc>),
passing the afterPack build arch, so it verifies the binary an equal-libc host
actually loads.

Also unlink the subpackage's watcher.node before overwriting it with the
rebuild: pnpm hard-links that file from its global content-addressed store on
Linux/Windows, so an in-place copy rewrites the shared inode and corrupts the
cached prebuild for every other project that links the same version.

Adds regression coverage: sibling-subpackage tolerance, build-arch selection,
and store-blob preservation.

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
config/packaged-runtime-node-modules.cjs (1)

289-300: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low value

Arch mapping only covers x64/arm64; 32-bit ARM Linux prebuilds exist upstream.

@parcel/watcher also publishes watcher-linux-arm-glibc/-arm-musl (32-bit armv7l) prebuilds. If Orca ever ships an armv7l Linux AppImage, electronArch would map to null here and the verification would silently skip that build (safe, but a coverage gap rather than a hard failure).


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: edebe46e-3cbc-4748-aaad-c272e54ec475

📥 Commits

Reviewing files that changed from the base of the PR and between 064f519 and e92bcaa.

📒 Files selected for processing (5)
  • config/electron-builder.config.cjs
  • config/packaged-runtime-node-modules.cjs
  • config/scripts/electron-builder-config.test.mjs
  • config/scripts/rebuild-native-deps.mjs
  • config/scripts/rebuild-native-deps.test.mjs
🚧 Files skipped from review as they are similar to previous changes (3)
  • config/electron-builder.config.cjs
  • config/scripts/rebuild-native-deps.mjs
  • config/scripts/rebuild-native-deps.test.mjs

…arches

Document that rebuiltParcelWatcherLinuxSubpackage intentionally returns null
(skipping the gate) for arches Orca does not ship on Linux, so a future
armv7l/ia32 target is a deliberate extension point rather than a silent gap.
@brennanb2025

Copy link
Copy Markdown
Contributor

Thanks for the careful work here — both the original binary forensics and especially the follow-up where you re-drove the exact failing binary through the real watcher-child path on both Electron 42.3.3 and 43.1.0. That re-test, matching what we found on our side, is what settles this.

Since neither of us can reproduce the failure — including with the original binary on the original Electron version — the missing napi_register_module_v1 isn't the cause of what you saw (the prebuild's older napi_module_register registration works on every path we both tested). That removes the justification for merging this: it would make compiling @parcel/watcher from source a permanent hard dependency of every Linux release, and swap the upstream prebuild for a CI-built binary, to guard against a failure mode we can't demonstrate.

Closing rather than merging, with the branch kept intact — it also carries two build fixes from review (scoping the afterPack gate to the rebuilt subpackage, and unlink-before-copy so the rebuild doesn't corrupt pnpm's hard-linked store), so if your new diagnostics ever catch this live, we can revive it quickly and land it as a confirmed fix. Appreciate the rigor, including flagging the evidence gap yourself.

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

Labels

None yet

Projects

None yet

2 participants