Skip to content

Commit

Permalink
fix: always set exit code if exiting uncleanly (#7674)
Browse files Browse the repository at this point in the history
An erroneous assumption was that if this was explicitly set, then the
exit was still intentional. This is not the case.

Closes: #7672

---------

Co-authored-by: Chris Sidi <hashtagchris@github.com>
  • Loading branch information
wraithgar and hashtagchris authored Jul 29, 2024
1 parent 86b05fc commit 4e81a6a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
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)
})

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

0 comments on commit 4e81a6a

Please sign in to comment.