diff --git a/packages/api/core/src/api/start.ts b/packages/api/core/src/api/start.ts index b2c36a35aa..7c3ec6d9e4 100644 --- a/packages/api/core/src/api/start.ts +++ b/packages/api/core/src/api/start.ts @@ -165,6 +165,7 @@ export default autoTrace( } as NodeJS.ProcessEnv, }; + spawnOpts.env.NODE_OPTIONS = removePnpLoaderArguments(spawnOpts.env.NODE_OPTIONS); if (runAsNode) { spawnOpts.env.ELECTRON_RUN_AS_NODE = 'true'; } else { @@ -231,3 +232,11 @@ export default autoTrace( return spawned; } ); + +function removePnpLoaderArguments(input: string | undefined): string | undefined { + if (!input) return input; + return input.replace( + /((--require\s+[^"].+\.pnp\.cjs)|(--experimental-loader\s+[^"].+\.pnp\.loader\.mjs)|(--require\s+".+\.pnp\.cjs")|(--experimental-loader\s+".+\.pnp\.loader\.mjs")) ?/g, + '' + ); +} diff --git a/packages/api/core/test/fast/start_spec.ts b/packages/api/core/test/fast/start_spec.ts index 1fd42cfd12..166f7cebe1 100644 --- a/packages/api/core/test/fast/start_spec.ts +++ b/packages/api/core/test/fast/start_spec.ts @@ -207,4 +207,41 @@ describe('start', () => { }) ).to.eventually.equal(fakeChild); }); + + it(`should spawn remove pnp environment variable in NODE_OPTIONS`, async () => { + resolveStub.returnsArg(0); + const oldNodeOptions = process.env.NODE_OPTIONS; + try { + process.env.NODE_OPTIONS = '--require /some/workspace/.pnp.cjs --experimental-loader file:///some/workspace/.pnp.loader.mjs'; + await start({ + dir: __dirname, + interactive: false, + }); + expect(spawnStub.firstCall.args[2].env.NODE_OPTIONS).to.equal(''); + } finally { + process.env.NODE_OPTIONS = oldNodeOptions; + } + }); + + const testNodeOptions = [ + '--hello --require /some/workspace/.pnp.cjs --experimental-loader file:///some/workspace/.pnp.loader.mjs --require /some/debugConnector.js --world --some args', + '--hello --require "/some/space path/.pnp.cjs" --experimental-loader "file:///some/space path/.pnp.loader.mjs" --require /some/debugConnector.js --world --some args', + '--hello --require "C:\\\\some space path\\\\.pnp.cjs" --experimental-loader "file:///c:/some space path/.pnp.loader.mjs" --require /some/debugConnector.js --world --some args', + ]; + for (let i = 0; i < testNodeOptions.length; i++) { + it(`should spawn remove pnp environment variable in NODE_OPTIONS case ${i}`, async () => { + resolveStub.returnsArg(0); + const oldNodeOptions = process.env.NODE_OPTIONS; + try { + process.env.NODE_OPTIONS = testNodeOptions[i]; + await start({ + dir: __dirname, + interactive: false, + }); + expect(spawnStub.lastCall.args[2].env.NODE_OPTIONS).to.equal('--hello --require /some/debugConnector.js --world --some args'); + } finally { + process.env.NODE_OPTIONS = oldNodeOptions; + } + }); + } }); diff --git a/packages/utils/core-utils/src/electron-version.ts b/packages/utils/core-utils/src/electron-version.ts index 16caf0aac6..def6d47d9e 100644 --- a/packages/utils/core-utils/src/electron-version.ts +++ b/packages/utils/core-utils/src/electron-version.ts @@ -66,6 +66,13 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string { async function getElectronPackageJSONPath(dir: string, packageName: string): Promise { const nodeModulesPath = await determineNodeModulesPath(dir, packageName); if (!nodeModulesPath) { + try { + // Yarn PnP + // eslint-disable-next-line node/no-missing-require + return require.resolve('electron/package.json'); + } catch (e) { + // Ignore + } throw new PackageNotFoundError(packageName, dir); }