Skip to content

Commit

Permalink
fix: enforce extraModules to be a set of strings (#888)
Browse files Browse the repository at this point in the history
* fix: usage of this.prodDeps as a Set<string>

For some reason TypeScript is AOK with stuffing `Set` objects with
random keys. This is problematic as this is not how `Set` are supposed
to be used (like a plain JavaScript object map).

Fix the logic to respect the `-w/--which-module` CLI argument.

* simplify tests

* test: add simple, fast test

---------

Co-authored-by: Mark Lee <[email protected]>
Co-authored-by: Erick Zhao <[email protected]>
  • Loading branch information
3 people authored Nov 12, 2024
1 parent e1eb81a commit 6430e74
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/module-walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ModuleWalker {
}

for (const key of depKeys) {
this.prodDeps[key] = true;
this.prodDeps.add(key);
const modulePaths: string[] = await searchForModule(
this.buildPath,
key,
Expand Down Expand Up @@ -97,11 +97,11 @@ export class ModuleWalker {

const callback = this.markChildrenAsProdDeps.bind(this);
for (const key of Object.keys(childPackageJson.dependencies || {}).concat(Object.keys(childPackageJson.optionalDependencies || {}))) {
if (this.prodDeps[key]) {
if (this.prodDeps.has(key)) {
continue;
}

this.prodDeps[key] = true;
this.prodDeps.add(key);

moduleWait.push(this.findModule(key, modulePath, callback));
}
Expand Down Expand Up @@ -133,7 +133,7 @@ export class ModuleWalker {
}
this.realModulePaths.add(realPath);

if (this.prodDeps[`${prefix}${modulePath}`] && (!this.onlyModules || this.onlyModules.includes(modulePath))) {
if (this.prodDeps.has(`${prefix}${modulePath}`) && (!this.onlyModules || this.onlyModules.includes(modulePath))) {
this.modulesToRebuild.push(realPath);
}

Expand Down
20 changes: 12 additions & 8 deletions src/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class Rebuilder implements IRebuilder {

this.ABIVersion = options.forceABI?.toString();
const onlyModules = options.onlyModules || null;
const extraModules = (options.extraModules || []).reduce((acc: Set<string>, x: string) => acc.add(x), new Set<string>());
const extraModules = new Set(options.extraModules);
const types = options.types || defaultTypes;
this.moduleWalker = new ModuleWalker(
this.buildPath,
Expand Down Expand Up @@ -237,13 +237,7 @@ export class Rebuilder implements IRebuilder {

this.lifecycle.emit('start');

await this.moduleWalker.walkModules();

for (const nodeModulesPath of await this.moduleWalker.nodeModulesPaths) {
await this.moduleWalker.findAllModulesIn(nodeModulesPath);
}

for (const modulePath of this.moduleWalker.modulesToRebuild) {
for (const modulePath of await this.modulesToRebuild()) {
this.rebuilds.push(() => this.rebuildModuleAt(modulePath));
}

Expand All @@ -258,6 +252,16 @@ export class Rebuilder implements IRebuilder {
}
}

async modulesToRebuild(): Promise<string[]> {
await this.moduleWalker.walkModules();

for (const nodeModulesPath of await this.moduleWalker.nodeModulesPaths) {
await this.moduleWalker.findAllModulesIn(nodeModulesPath);
}

return this.moduleWalker.modulesToRebuild;
}

async rebuildModuleAt(modulePath: string): Promise<void> {
if (!(await fs.pathExists(path.resolve(modulePath, 'binding.gyp')))) {
return;
Expand Down
1 change: 1 addition & 0 deletions test/fixture/empty-project/node_modules/extra/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixture/empty-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
17 changes: 16 additions & 1 deletion test/rebuild.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { EventEmitter } from 'events';
import { expect } from 'chai';
import * as fs from 'fs-extra';
import * as path from 'path';

import { cleanupTestModule, MINUTES_IN_MILLISECONDS, TEST_MODULE_PATH as testModulePath, resetMSVSVersion, resetTestModule, TIMEOUT_IN_MILLISECONDS } from './helpers/module-setup';
import { expectNativeModuleToBeRebuilt, expectNativeModuleToNotBeRebuilt } from './helpers/rebuild';
import { getExactElectronVersionSync } from './helpers/electron-version';
import { rebuild } from '../lib/rebuild';
import { Rebuilder, rebuild } from '../lib/rebuild';

const testElectronVersion = getExactElectronVersionSync();

Expand Down Expand Up @@ -179,6 +180,20 @@ describe('rebuilder', () => {
});
});

describe('with extraModules', () => {
it('should rebuild existing modules in extraModules despite them not being found during the module walk', async () => {
const rebuilder = new Rebuilder({
buildPath: path.join(__dirname, 'fixture', 'empty-project'),
electronVersion: testElectronVersion,
lifecycle: new EventEmitter(),
extraModules: ['extra']
});
const modulesToRebuild = await rebuilder.modulesToRebuild();
expect(modulesToRebuild).to.have.length(1);
expect(modulesToRebuild[0].endsWith('extra')).to.be.true;
});
});

describe('debug rebuild', function() {
this.timeout(10 * MINUTES_IN_MILLISECONDS);

Expand Down

0 comments on commit 6430e74

Please sign in to comment.