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(cli): ensure an empty event loop counts as an error #6399

Merged
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
23 changes: 23 additions & 0 deletions .yarn/versions/3a4fece1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
releases:
"@yarnpkg/cli": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
Original file line number Diff line number Diff line change
Expand Up @@ -900,5 +900,30 @@ describe(`Commands`, () => {
});
}),
);

test(`it should exit with an error code after an unexpected empty event loop`,
makeTemporaryEnv({}, async ({path, run}) => {
await xfs.writeFilePromise(ppath.join(path, `plugin.cjs`), `
module.exports = {
name: 'test',
factory() {
return {
hooks: {
afterAllInstalled: () => new Promise(() => {}),
},
};
},
};
`);
await xfs.writeJsonPromise(ppath.join(path, Filename.rc), {
plugins: [`./plugin.cjs`],
});

await expect(run(`install`)).rejects.toMatchObject({
code: 42,
stdout: expect.stringContaining(`Yarn is terminating due to an unexpected empty event loop`),
});
}),
);
});
});
12 changes: 12 additions & 0 deletions packages/yarnpkg-cli/sources/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,24 @@ export async function getCli({cwd = ppath.cwd(), pluginConfiguration = getPlugin
export async function runExit(argv: Array<string>, {cwd = ppath.cwd(), selfPath, pluginConfiguration}: {cwd: PortablePath, selfPath: PortablePath | null, pluginConfiguration: PluginConfiguration}) {
const cli = getBaseCli({cwd, pluginConfiguration});

function unexpectedTerminationHandler() {
Cli.defaultContext.stdout.write(`ERROR: Yarn is terminating due to an unexpected empty event loop.\nPlease report this issue at https://github.com/yarnpkg/berry/issues.`);
}

process.once(`beforeExit`, unexpectedTerminationHandler);

try {
// The exit code is set to an error code before the CLI runs so that
// if the event loop becomes empty and node terminates without
// finishing the execution of this function it counts as an error.
// https://github.com/yarnpkg/berry/issues/6398
process.exitCode = 42;
process.exitCode = await run(cli, argv, {selfPath, pluginConfiguration});
} catch (error) {
Cli.defaultContext.stdout.write(cli.error(error));
process.exitCode = 1;
} finally {
process.off(`beforeExit`, unexpectedTerminationHandler);
await xfs.rmtempPromise();
}
}
Loading