Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support Yarn PnP #3209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
''
);
}
37 changes: 37 additions & 0 deletions packages/api/core/test/fast/start_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
});
7 changes: 7 additions & 0 deletions packages/utils/core-utils/src/electron-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ function getElectronModuleName(packageJSON: PackageJSONWithDeps): string {
async function getElectronPackageJSONPath(dir: string, packageName: string): Promise<string | undefined> {
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);
}

Expand Down