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

fix: don't override process.exitCode #268

Merged
merged 2 commits into from
May 23, 2023
Merged
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
22 changes: 7 additions & 15 deletions sources/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function executePackageManagerRequest({packageManager, binaryName, binaryV
return await corepackUtils.runVersion(installSpec, binaryName, args);
}

async function main(argv: Array<string>) {
export async function runMain(argv: Array<string>) {
// Because we load the binaries in the same process, we don't support custom contexts.
const context = {
...Cli.defaultContext,
Expand All @@ -99,10 +99,9 @@ async function main(argv: Array<string>) {
const [firstArg, ...restArgs] = argv;
const request = getPackageManagerRequestFromCli(firstArg, context);

let cli: Cli<Context>;
if (!request) {
// If the first argument doesn't match any supported package manager, we fallback to the standard Corepack CLI
cli = new Cli({
const cli = new Cli({
binaryLabel: `Corepack`,
binaryName: `corepack`,
binaryVersion: corepackVersion,
Expand All @@ -116,7 +115,7 @@ async function main(argv: Array<string>) {
cli.register(HydrateCommand);
cli.register(PrepareCommand);

return await cli.run(argv, context);
await cli.runExit(argv, context);
} else {
// Otherwise, we create a single-command CLI to run the specified package manager (we still use Clipanion in order to pretty-print usage errors).
const cli = new Cli({
Expand All @@ -132,16 +131,9 @@ async function main(argv: Array<string>) {
}
});

return await cli.run(restArgs, context);
const code = await cli.run(restArgs, context);
if (code !== 0) {
process.exitCode ??= code;
}
}
}

// Important: this is the only function that the corepack binary exports.
export function runMain(argv: Array<string>) {
main(argv).then(exitCode => {
process.exitCode = exitCode;
}, err => {
console.error(err.stack);
process.exitCode = 1;
});
}
68 changes: 68 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,71 @@ it(`should handle parallel installs`, async () => {
]);
});
});

it(`should not override the package manager exit code`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
packageManager: `yarn@2.2.2`,
});

const yarnPath = ppath.join(npath.toPortablePath(process.env.COREPACK_HOME!), `yarn/2.2.2/yarn.js` as PortablePath);

await xfs.mkdirPromise(ppath.dirname(yarnPath), {recursive: true});
await xfs.writeFilePromise(yarnPath, `
process.exitCode = 42;
`);

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 42,
stdout: ``,
stderr: ``,
});
});
});

it(`should not override the package manager exit code when it throws`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
packageManager: `yarn@2.2.2`,
});

const yarnPath = ppath.join(npath.toPortablePath(process.env.COREPACK_HOME!), `yarn/2.2.2/yarn.js` as PortablePath);

await xfs.mkdirPromise(ppath.dirname(yarnPath), {recursive: true});
await xfs.writeFilePromise(yarnPath, `
process.exitCode = 42;
throw new Error('foo');
`);

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 42,
stdout: expect.stringContaining(`foo`),
stderr: ``,
});
});
});

it(`should not set the exit code after successfully launching the package manager`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
packageManager: `yarn@2.2.2`,
});

const yarnPath = ppath.join(npath.toPortablePath(process.env.COREPACK_HOME!), `yarn/2.2.2/yarn.js` as PortablePath);

await xfs.mkdirPromise(ppath.dirname(yarnPath), {recursive: true});
await xfs.writeFilePromise(yarnPath, `
process.once('beforeExit', () => {
if (process.exitCode === undefined) {
process.exitCode = 42;
}
});
`);

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 42,
stdout: ``,
stderr: ``,
});
});
});
Binary file added tests/nock/4ppY93mauKPrkN4oe1GUkA-1.dat
Binary file not shown.
Binary file added tests/nock/WaR3LFsnTKCCsgSkSI54NA-1.dat
Binary file not shown.
Binary file added tests/nock/bc9X9LCamX_BSH1eZYT1Zg-1.dat
Binary file not shown.