fix: improve binary verification process for cross-compilation in forge configuration
All checks were successful
Release Electron App / release-desktop (push) Successful in 10m50s

This commit is contained in:
2026-03-03 23:59:33 +01:00
parent 1aa315cddf
commit 0ca54dd20a

View File

@@ -103,32 +103,36 @@ const config: ForgeConfig = {
{ cwd: modDir, stdio: 'inherit' }, { cwd: modDir, stdio: 'inherit' },
); );
// Verify the downloaded binary is NOT an ELF (Linux) file. // prebuild-install writes the binary to build/Release/<name>.node.
// node-gyp-build loads from prebuilds/ first, then build/Release/. // Verify at least one .node file exists there and is NOT an ELF
const prebuildsTarget = path.join(modDir, 'prebuilds', targetKey); // (Linux) binary — a Linux .node in a Windows package always crashes.
if (fs.existsSync(prebuildsTarget)) { const releaseDir = path.join(modDir, 'build', 'Release');
for (const f of fs.readdirSync(prebuildsTarget)) { if (!fs.existsSync(releaseDir)) {
if (f.endsWith('.node')) {
const buf = Buffer.alloc(4);
const fd = fs.openSync(path.join(prebuildsTarget, f), 'r');
fs.readSync(fd, buf, 0, 4, 0);
fs.closeSync(fd);
// ELF magic: 0x7f 'E' 'L' 'F'
if (buf[0] === 0x7f && buf[1] === 0x45 && buf[2] === 0x4c && buf[3] === 0x46) {
throw new Error(
`[forge] FATAL: ${mod} prebuilt for ${targetKey} is an ELF binary! ` +
`Cross-compilation failed — refusing to package a Linux .node for Windows.`,
);
}
console.log(`[forge] Verified ${f} is not ELF ✓`);
}
}
} else {
throw new Error( throw new Error(
`[forge] FATAL: No prebuilds/${targetKey}/ directory found for ${mod} after prebuild-install. ` + `[forge] FATAL: build/Release/ not found for ${mod} after prebuild-install. ` +
`The native module would fall back to a Linux binary at runtime.`, `The native binary was not downloaded.`,
); );
} }
const nodeFiles = fs.readdirSync(releaseDir).filter((f) => f.endsWith('.node'));
if (nodeFiles.length === 0) {
throw new Error(
`[forge] FATAL: No .node files in build/Release/ for ${mod} after prebuild-install.`,
);
}
for (const f of nodeFiles) {
const buf = Buffer.alloc(4);
const fd = fs.openSync(path.join(releaseDir, f), 'r');
fs.readSync(fd, buf, 0, 4, 0);
fs.closeSync(fd);
// ELF magic: 0x7f 'E' 'L' 'F'
if (buf[0] === 0x7f && buf[1] === 0x45 && buf[2] === 0x4c && buf[3] === 0x46) {
throw new Error(
`[forge] FATAL: ${mod} build/Release/${f} is an ELF binary! ` +
`Cross-compilation failed — refusing to package a Linux .node for Windows.`,
);
}
console.log(`[forge] Verified ${f} is not ELF ✓`);
}
} }
} }