Skip to content

Commit 17d1f3d

Browse files
authored
fix: don't override process.exitCode (#268)
* fix: don't override exit codes * chore: update nock files
1 parent 3211804 commit 17d1f3d

5 files changed

+75
-15
lines changed

sources/main.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async function executePackageManagerRequest({packageManager, binaryName, binaryV
8888
return await corepackUtils.runVersion(installSpec, binaryName, args);
8989
}
9090

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

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

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

135-
return await cli.run(restArgs, context);
134+
const code = await cli.run(restArgs, context);
135+
if (code !== 0) {
136+
process.exitCode ??= code;
137+
}
136138
}
137139
}
138-
139-
// Important: this is the only function that the corepack binary exports.
140-
export function runMain(argv: Array<string>) {
141-
main(argv).then(exitCode => {
142-
process.exitCode = exitCode;
143-
}, err => {
144-
console.error(err.stack);
145-
process.exitCode = 1;
146-
});
147-
}

tests/main.test.ts

+68
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,71 @@ it(`should handle parallel installs`, async () => {
538538
]);
539539
});
540540
});
541+
542+
it(`should not override the package manager exit code`, async () => {
543+
await xfs.mktempPromise(async cwd => {
544+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
545+
packageManager: `yarn@2.2.2`,
546+
});
547+
548+
const yarnPath = ppath.join(npath.toPortablePath(process.env.COREPACK_HOME!), `yarn/2.2.2/yarn.js` as PortablePath);
549+
550+
await xfs.mkdirPromise(ppath.dirname(yarnPath), {recursive: true});
551+
await xfs.writeFilePromise(yarnPath, `
552+
process.exitCode = 42;
553+
`);
554+
555+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
556+
exitCode: 42,
557+
stdout: ``,
558+
stderr: ``,
559+
});
560+
});
561+
});
562+
563+
it(`should not override the package manager exit code when it throws`, async () => {
564+
await xfs.mktempPromise(async cwd => {
565+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
566+
packageManager: `yarn@2.2.2`,
567+
});
568+
569+
const yarnPath = ppath.join(npath.toPortablePath(process.env.COREPACK_HOME!), `yarn/2.2.2/yarn.js` as PortablePath);
570+
571+
await xfs.mkdirPromise(ppath.dirname(yarnPath), {recursive: true});
572+
await xfs.writeFilePromise(yarnPath, `
573+
process.exitCode = 42;
574+
throw new Error('foo');
575+
`);
576+
577+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
578+
exitCode: 42,
579+
stdout: expect.stringContaining(`foo`),
580+
stderr: ``,
581+
});
582+
});
583+
});
584+
585+
it(`should not set the exit code after successfully launching the package manager`, async () => {
586+
await xfs.mktempPromise(async cwd => {
587+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
588+
packageManager: `yarn@2.2.2`,
589+
});
590+
591+
const yarnPath = ppath.join(npath.toPortablePath(process.env.COREPACK_HOME!), `yarn/2.2.2/yarn.js` as PortablePath);
592+
593+
await xfs.mkdirPromise(ppath.dirname(yarnPath), {recursive: true});
594+
await xfs.writeFilePromise(yarnPath, `
595+
process.once('beforeExit', () => {
596+
if (process.exitCode === undefined) {
597+
process.exitCode = 42;
598+
}
599+
});
600+
`);
601+
602+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
603+
exitCode: 42,
604+
stdout: ``,
605+
stderr: ``,
606+
});
607+
});
608+
});
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.
7 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)