Skip to content

fix: always set exit code if exiting uncleanly #7674

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

Merged
merged 2 commits into from
Jul 29, 2024
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
5 changes: 3 additions & 2 deletions lib/cli/exit-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class ExitHandler {
}

#handleProcessExit (code) {
// Force exit code to a number if it has not been set
const exitCode = typeof code === 'number' ? code : (this.#exited ? 0 : 1)
const numCode = Number(code) || 0
// Always exit w/ a non-zero code if exit handler was not called
const exitCode = this.#exited ? numCode : (numCode || 1)
this.#process.exitCode = exitCode

if (this.#notLoadedOrExited) {
Expand Down
7 changes: 4 additions & 3 deletions test/lib/cli/exit-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,16 @@ t.test('exit handler never called', async t => {
const { logs, errors } = await mockExitHandler(t, {
config: { loglevel: 'silent' },
})
process.emit('exit', 1)
process.emit('exit', 0)
t.strictSame(logs, [])
t.strictSame(errors(), [''], 'one empty string')
t.equal(process.exitCode, 1)
Copy link
Contributor

@hashtagchris hashtagchris Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Add a dedicated test that verifies 0 is replaced by a non-zero value. The test name might make things click for someone.

})

t.test('loglevel notice', async (t) => {
const { logs, errors } = await mockExitHandler(t)
process.emit('exit', 1)
t.equal(process.exitCode, 1)
process.emit('exit', 2)
t.equal(process.exitCode, 2)
t.match(logs.error, [
'Exit handler never called!',
/error with npm itself/,
Expand Down
Loading