fix: improve binary verification process for cross-compilation in forge configuration
All checks were successful
Release Electron App / release-desktop (push) Successful in 10m50s
All checks were successful
Release Electron App / release-desktop (push) Successful in 10m50s
This commit is contained in:
@@ -103,33 +103,37 @@ const config: ForgeConfig = {
|
||||
{ cwd: modDir, stdio: 'inherit' },
|
||||
);
|
||||
|
||||
// Verify the downloaded binary is NOT an ELF (Linux) file.
|
||||
// node-gyp-build loads from prebuilds/ first, then build/Release/.
|
||||
const prebuildsTarget = path.join(modDir, 'prebuilds', targetKey);
|
||||
if (fs.existsSync(prebuildsTarget)) {
|
||||
for (const f of fs.readdirSync(prebuildsTarget)) {
|
||||
if (f.endsWith('.node')) {
|
||||
// prebuild-install writes the binary to build/Release/<name>.node.
|
||||
// Verify at least one .node file exists there and is NOT an ELF
|
||||
// (Linux) binary — a Linux .node in a Windows package always crashes.
|
||||
const releaseDir = path.join(modDir, 'build', 'Release');
|
||||
if (!fs.existsSync(releaseDir)) {
|
||||
throw new Error(
|
||||
`[forge] FATAL: build/Release/ not found for ${mod} after prebuild-install. ` +
|
||||
`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(prebuildsTarget, f), 'r');
|
||||
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} prebuilt for ${targetKey} is an ELF binary! ` +
|
||||
`[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 ✓`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error(
|
||||
`[forge] FATAL: No prebuilds/${targetKey}/ directory found for ${mod} after prebuild-install. ` +
|
||||
`The native module would fall back to a Linux binary at runtime.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// vectordb uses platform-specific optional deps (@lancedb/vectordb-<platform>-<arch>-*).
|
||||
|
||||
Reference in New Issue
Block a user