fix: don't override `process.exitCode` (#268)

* fix: don't override exit codes

* chore: update nock files
This commit is contained in:
Kristoffer K 2023-05-24 00:31:58 +02:00 committed by GitHub
parent 321180492d
commit 17d1f3dd41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 15 deletions

View File

@ -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,
@ -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,
@ -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({
@ -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;
});
}

View File

@ -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 not shown.

Binary file not shown.

Binary file not shown.