diff --git a/forge.config.ts b/forge.config.ts index 6491c9d..a067eb0 100644 --- a/forge.config.ts +++ b/forge.config.ts @@ -103,32 +103,36 @@ 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')) { - 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 { + // prebuild-install writes the binary to build/Release/.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: No prebuilds/${targetKey}/ directory found for ${mod} after prebuild-install. ` + - `The native module would fall back to a Linux binary at runtime.`, + `[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(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 ✓`); + } } }