Repository navigation
fix(build): rebuild @parcel/watcher from source for Electron N-API compatibility - #8580
LesleyMurfin wants to merge 3 commits into
Conversation
…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.
📝 WalkthroughWalkthroughThe packaging configuration now verifies Linux Parcel watcher bindings after packaging. Native dependency rebuilding includes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
config/scripts/rebuild-native-deps.test.mjs (1)
499-520: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winMissing test coverage for the musl branch of
parcelWatcherSubpackageName.
writeFakeParcelWatcherSourcehardcodesfamilySync()to returnnull(glibc branch), so the-muslsuffix path inparcelWatcherSubpackageName(config/scripts/rebuild-native-deps.mjsLines 200-213) is never exercised by any test. Given this is the most novel/complex logic added in this PR, consider parameterizing the fakedetect-libcfamily 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
📒 Files selected for processing (5)
config/electron-builder.config.cjsconfig/packaged-runtime-node-modules.cjsconfig/scripts/electron-builder-config.test.mjsconfig/scripts/rebuild-native-deps.mjsconfig/scripts/rebuild-native-deps.test.mjs
| 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 | ||
| } |
There was a problem hiding this comment.
🗄️ 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/**' || trueRepository: 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:
- 1: https://github.com/lovell/detect-libc/blob/main/README.md
- 2: https://npmx.dev/package/detect-libc/v/2.1.0-rc.0
- 3: https://npmx.dev/package/detect-libc/v/2.1.0
- 4: https://npmx.dev/package/detect-libc/v/%5E2.0.4
- 5: https://www.npmjs.com/package/detect-libc
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.
|
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.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
config/packaged-runtime-node-modules.cjs (1)
289-300: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low valueArch mapping only covers x64/arm64; 32-bit ARM Linux prebuilds exist upstream.
@parcel/watcheralso publisheswatcher-linux-arm-glibc/-arm-musl(32-bitarmv7l) prebuilds. If Orca ever ships anarmv7lLinux AppImage,electronArchwould map tonullhere 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
📒 Files selected for processing (5)
config/electron-builder.config.cjsconfig/packaged-runtime-node-modules.cjsconfig/scripts/electron-builder-config.test.mjsconfig/scripts/rebuild-native-deps.mjsconfig/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.
|
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 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. |
Summary
Fixes #8540. The
@parcel/watcher-<platform>-<arch>optionalDependency prebuilds published upstream register via the older constructor-based N-API path and omit the modernnapi_register_module_v1export. Electron's context-aware module loader requires that export, so the packaged Linux AppImage'swatcher.nodefails 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_v1returns nothing on the currently published@parcel/watcher@2.5.6prebuild, while its own source (src/binding.cc:268) does useNODE_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/watcherto the Electron-targeted native rebuild (NATIVE_MODULES) for all three packaged platforms (darwin/win32/linux). After@electron/rebuildcompilesnode_modules/@parcel/watcher/build/Release/watcher.nodeagainst Electron's headers, copy it over the matching@parcel/watcher-<platform>-<arch>[-glibc|-musl]subpackage'swatcher.node—index.js'srequire()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: addverifyPackagedParcelWatcherBinding, called from theafterPackhook. It asserts the packaged Linuxwatcher.nodeactually exportsnapi_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 intoafterPack.config/scripts/rebuild-native-deps.test.mjsandconfig/scripts/electron-builder-config.test.mjsfor the copy-into-subpackage behavior and the Linux-scoped verification gate (pass/fail/no-op cases).AI Code Review Summary
rebuild-native-deps.mjsruns on all three packaged platforms (darwin/win32/linux) via the existingrebuildPlatform/osPlatform()plumbing, so darwin and win32 builds also get a freshly-rebuilt, correctly-registeringwatcher.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 omitnapi_register_module_v1with no observed runtime failure — a cross-platform gate would false-fail correct macOS/Windows builds.afterPackverification). 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.@parcel/watcher) to the existing@electron/rebuildinvocation and one directory scan + file read inafterPack. Both are one-time costs at packaging time, not at app runtime — no measurable user-facing performance impact.@parcel/watcherfrom its own already-vendored npm source against Electron's official headers (https://electronjs.org/headers/) — the same trusted source and rebuild mechanism already used fornode-ptyandcpu-featuresin 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 errorspnpm test— 29,153 passed, 40 skipped, 7 pre-existing failures insrc/relay/pty-shell-launch.test.ts(4) andsrc/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 cleanupstream/maincheckout with none of this PR's changes applied, in an isolatedgit worktree. Consistent with a local bash-version difference on the machine running the tests, not a regression from this PR (this PR touches onlyconfig/electron-builder.config.cjs,config/packaged-runtime-node-modules.cjs, and the twoconfig/scripts/*.mjs/*.test.mjsfiles listed above — none of which are anywhere near PTY/bash prompt handling).pnpm build— fullbuild:desktop+build:native, exit 0AppImagebuild should now fail-fast inafterPackifnapi_register_module_v1is ever missing again, and should succeed once the rebuild is wired inX (Twitter) handle: @LesleyMurfin