Sequential multi-platform builds package the wrong platform's native module (universal mac build fails with "Expected all non-binary files to have identical SHAs")
Bug summary
When building several platforms sequentially against the same node_modules (a workflow that worked in electron-builder 24), electron-builder ≥ 26 can package a previous platform's native-module binary into the next platform's app. In our case a macOS universal build shipped a Linux ELF in the x64 slice, which @electron/universal caught:
⨯ Expected all non-binary files to have identical SHAs when creating a universal build
but "Contents/Resources/app.asar.unpacked/node_modules/better-sqlite3/build/Release/better_sqlite3.node" did notFor single-platform targets there is no such cross-check, so the wrong-platform binary ships silently and only fails at runtime (invalid ELF header / not a valid Win32 application).
This is a regression introduced by v26 switching native rebuilds from the legacy app-builder path to @electron/rebuild — the root cause is in @electron/rebuild's rebuild-skip marker, but electron-builder ≥ 26 is where users hit it.
To Reproduce
On a macOS machine, with an app depending on a native module that uses prebuild-install-style output in build/Release/ (e.g. better-sqlite3):
electron-builder --linux --x64 # rebuild → Linux ELF in build/Release, marker "x64--143"
electron-builder --mac --universal # x64 pass: marker matches "x64--143" → rebuild SKIPPEDThe mac x64 slice then contains the leftover Linux binary, while the arm64 slice — whose arch differed from the marker, forcing a rebuild — is correct:
$ file mac-universal-x64-temp/…/better_sqlite3.node
ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, not stripped
$ file mac-universal-arm64-temp/…/better_sqlite3.node
Mach-O 64-bit bundle arm64That asymmetry demonstrates the mechanism: the rebuild decision reacts to an arch change but is blind to a platform change. The failure is order-dependent — --mac before --linux happens to work because every consecutive pair of rebuilds then differs in arch.
Root cause
@electron/rebuild skips rebuilds via a .forge-meta marker written to build/Release/, keyed on arch and ABI only:
// @electron/rebuild src/module-rebuilder.ts
get metaData(): string {
return `${this.rebuilder.arch}--${this.rebuilder.ABI}`;
}
async alreadyBuiltByRebuild(): Promise<boolean> {
if (fs.existsSync(this.metaPath)) {
const meta = await fs.promises.readFile(this.metaPath, 'utf8');
return meta === this.metaData;
}
return false;
}The key is not unique across platforms: ABI is the Node ABI version number (via node-abi, e.g. 143 for Electron 40) — a function of the Electron version only, identical on macOS, Windows, and Linux for the same Electron release. It's easy to assume "ABI" implies binary compatibility and therefore encodes the OS; it doesn't. The true identity of a native build is the triple (platform, arch, ABI) — the same triple used everywhere else in the ecosystem (prebuilt artifacts: better-sqlite3-v12.6.2-electron-v143-darwin-x64.tar.gz; @electron/rebuild's own prebuild lookup: bin/${platform}-${arch}-${ABI}). The marker keeps only the last two components, so:
rebuild for linux/x64 → writes Linux ELF, .forge-meta = "x64--143"
rebuild for darwin/x64 → sees "x64--143" → "already built" → SKIPPEDThe legacy (pre-v26) rebuild path did not have this problem, which is why upgrading electron-builder 24 → 26 surfaces it.
Expected behavior
A rebuild for a different target platform must not be skipped. Options:
Upstream fix in
@electron/rebuild— include the platform in the marker (one line;Rebuilderalready carriesplatform):get metaData(): string { return `${this.rebuilder.platform}--${this.rebuilder.arch}--${this.rebuilder.ABI}`; }Old two-segment markers would simply never match, forcing one extra (correct) rebuild after upgrading — a safe migration.
Defensively in electron-builder: invalidate/clean stale
.forge-metamarkers (or force@electron/rebuild'sforceflag) when the target platform differs from the marker's platform.
Workaround
Delete the module's build/ directory (marker included) between per-platform builds:
"build": "… electron-builder --linux --x64 && rimraf node_modules/better-sqlite3/build && electron-builder --mac --universal"Environment
- electron-builder: 26.15.6 (
nativeRebuilder: sequential, the default) @electron/rebuild: 4.2.0 (bug still present onmain:metaDatagetter insrc/module-rebuilder.ts)- Electron: 40.6.1
- Host: macOS (darwin), CI agent building windows + linux + mac sequentially in one workspace
- Native module: better-sqlite3 12.x
Source: electron-userland/electron-builder