Skip to content

fix(electron): drop packages the target platform cannot run - #48

Merged
stuffbucket merged 1 commit into
mainfrom
fix/prune-wrong-platform-packages
Sep 6, 2026
Merged

stuffbucket merged 1 commit into
mainfrom
fix/prune-wrong-platform-packages

Conversation

@stuffbucket

Copy link
Copy Markdown
Owner

What's in the closure

@reflink/reflink declares eight platform siblings as optional dependencies. The installer places whichever one matches the machine running the install — and that machine is not the target.

Read off the bundle actually built here (189 packed manifests), running the new plan against four targets:

Target Would drop
darwin-arm64 nothing
darwin-x64 @reflink/reflink-darwin-arm64 — declares cpu arm64, not x64
win32-x64 @node-llama-cpp/mac-arm64-metal, @reflink/reflink-darwin-arm64
linux-x64 @node-llama-cpp/mac-arm64-metal, @reflink/reflink-darwin-arm64

@reflink/reflink-darwin-arm64 is a Mach-O .node. It reaches the closure three optional dependencies below anything this package imports: node-llama-cpp → ipull → @reflink/reflink. Nothing named it, because the two prune hooks that already existed each know one scope by name.

What this is not

I did not observe a foreign bundle ship. On a mismatched target pruneLlamaBackends throws first — only the host's @node-llama-cpp package is installed, so its plan keeps nothing and the build dies with @node-llama-cpp ships nothing usable by win32-x64. The wrong-platform copy is masked today by an unrelated hook erroring earlier, which is an accident rather than a property.

I could not get past it to see the real thing: the Electron binary download for win32 times out from this network. So the table above is the plan evaluated against the real closure, not a bundle I unpacked.

That makes this hardening plus a new assertion, not a fix for a bug caught in the act. Worth having on both counts, but I'd rather label it accurately.

The rule

platformPackagePlan reads the os and cpu fields npm publishes, so it needs no knowledge of any package and covers the next one without an edit. llamaPackagePlan is the same rule written for one scope by parsing its names.

It implements npm's actual semantics rather than list membership:

  • a list may exclude, written !win32
  • a list made only of exclusions admits everything it does not name — reading "os": ["!win32"] as an allow-list would drop that package from every build
  • any matches everything; a bare string is a list of one
  • mas is a darwin build
  • a universal build keeps a package either slice needs

The last two are single-value comparisons that would drop native code from a bundle that still builds cleanly.

Three places, one function

  • forge.config.ts deletes what the plan drops — last of the three hooks, so the two that throw on an upstream layout change report first and this stays a backstop rather than removing what they came to inspect.
  • verify-package.mjs stops expecting what the build dropped (otherwise a correct prune fails the check), and adds the half a prune that silently did nothing would still pass: every packed manifest is read back off the archive and asserted to admit the target. 189 of them against 162 closure placements, so it judges what the copy placed as well as what the closure named.
  • tests/platform-packages.test.ts — 17 cases over the plan and the matcher.

Verification

  • vitest run: 1141 passed, 68 files. tsc --noEmit clean. eslint . clean.
  • Repackaged on darwin-arm64 and ran verify:package: all 162 closure placements this target runs are packed, 189 packed manifest(s) were read, all 189 packed package(s) run on darwin-arm64. Nothing is dropped on this target, which is the correct answer for it.

`@reflink/reflink` declares eight platform siblings as optional dependencies
and the installer places whichever matches the machine running the install.
That machine is not the target. Read off the packaged bundle built here, a
`--platform=win32` build would carry `@reflink/reflink-darwin-arm64` -- a
Mach-O `.node` in a Windows application -- and a `darwin-x64` build would carry
it too, an arm64 binary in an x64 bundle.

It arrives three optional dependencies below anything this package imports:
`node-llama-cpp` -> `ipull` -> `@reflink/reflink`. Nothing named it, and the
two prune hooks that already existed each know one scope by name.

What this is NOT: a bundle observed in the wild. On a mismatched target
`pruneLlamaBackends` throws first, because only the host's `@node-llama-cpp`
package is installed and its plan then keeps nothing. So the wrong-platform
copy is masked today by an unrelated hook erroring earlier, which is an
accident rather than a property. A cross build could not be run here to see
past it: the Electron download for win32 times out from this network.

`platformPackagePlan` reads the `os` and `cpu` fields npm publishes, so it
needs no knowledge of any package and covers the next one without an edit. It
implements npm's actual rule rather than list membership: a list may EXCLUDE,
written `!win32`, and one made only of exclusions admits everything it does not
name. Reading `"os": ["!win32"]` as an allow-list would drop that package from
every build.

`mas` is treated as darwin, and a universal build keeps a package either slice
needs -- both of which a single-value comparison gets wrong by dropping native
code from a bundle that builds cleanly.

Three places, one function. `forge.config.ts` deletes what the plan drops, last
of the three hooks so the two that throw on a layout change report first.
`verify-package.mjs` stops expecting what was dropped, and adds the half that a
prune doing nothing would still pass: every packed manifest is read back off
the archive and asserted to admit the target. That is 189 of them, against 162
closure placements, so it judges what the copy placed as well as what the
closure named.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@stuffbucket
stuffbucket merged commit 0e2e707 into main Sep 6, 2026
6 checks passed
@stuffbucket
stuffbucket deleted the fix/prune-wrong-platform-packages branch September 6, 2026 05:58
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.

1 participant