Skip to content

Commit

Permalink
fix: replacing macro {napi_build_versions} in binary filename by usin…
Browse files Browse the repository at this point in the history
…g `binary.napi_build_versions` in package.json. Fixes: electron#554
  • Loading branch information
mmaietta committed Apr 12, 2023
1 parent 29365ad commit dc1ce32
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/module-type/node-gyp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export class NodeGyp extends NativeModule {

async buildArgsFromBinaryField(): Promise<string[]> {
const binary = await this.packageJSONFieldWithDefault('binary', {}) as Record<string, string>;
let napi_build_version: number | undefined = undefined
if (Array.isArray(binary.napi_versions)) {
napi_build_version = this.nodeAPI.getNapiVersion(binary.napi_versions as number[])
}
const flags = await Promise.all(Object.entries(binary).map(async ([binaryKey, binaryValue]) => {
if (binaryKey === 'napi_versions') {
return;
Expand All @@ -67,7 +71,9 @@ export class NodeGyp extends NativeModule {
.replace('{arch}', this.rebuilder.arch)
.replace('{version}', await this.packageJSONField('version') as string)
.replace('{libc}', await detectLibc.family() || 'unknown');

if (napi_build_version !== undefined) {
value = value.replace('{napi_build_version}', napi_build_version.toString())
}
for (const [replaceKey, replaceValue] of Object.entries(binary)) {
value = value.replace(`{${replaceKey}}`, replaceValue);
}
Expand Down
3 changes: 2 additions & 1 deletion test/fixture/native-app1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"farmhash": "3.2.1",
"level": "6.0.0",
"native-hello-world": "2.0.0",
"ref-napi": "1.4.2"
"ref-napi": "1.4.2",
"sqlite3": "5.1.6"
},
"optionalDependencies": {
"bcrypt": "3.0.6"
Expand Down
31 changes: 31 additions & 0 deletions test/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,35 @@ describe('rebuilder', () => {
await expectNativeModuleToBeRebuilt(testModulePath, 'ffi-napi');
});
});

describe('rebuild with napi_build_versions in binary config', async function () {
this.timeout(10 * MINUTES_IN_MILLISECONDS);

before(async () => await resetTestModule(testModulePath));
after(async() => await cleanupTestModule(testModulePath));

// https://github.com/electron/rebuild/issues/554
const archs = ['x64', 'arm64']
for (const arch of archs) {
it(`${ arch } arch should have rebuilt bianry with napi_build_versions array provided`, async () => {
const napiBuildVersion = 6;
const binaryPath = path.resolve(testModulePath, `node_modules/sqlite3/lib/binding/napi-v${ napiBuildVersion }-${ process.platform }-unknown-${ arch }/node_sqlite3.node`);
if (await fs.pathExists(binaryPath)) {
fs.removeSync(binaryPath)
}
expect(await fs.pathExists(binaryPath)).to.be.false;

await rebuild({
buildPath: testModulePath,
electronVersion: testElectronVersion,
arch,
onlyModules: ['sqlite3'],
});
await expectNativeModuleToBeRebuilt(testModulePath, 'sqlite3');

expect(await fs.pathExists(binaryPath)).to.be.true;
fs.removeSync(binaryPath);
});
}
});
});

0 comments on commit dc1ce32

Please sign in to comment.