Skip to content

Commit 4e81a6a

Browse files
wraithgarhashtagchris
andauthoredJul 29, 2024··
fix: always set exit code if exiting uncleanly (#7674)
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>
1 parent 86b05fc commit 4e81a6a

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed
 

‎lib/cli/exit-handler.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ class ExitHandler {
6666
}
6767

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

7374
if (this.#notLoadedOrExited) {

‎test/lib/cli/exit-handler.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,16 @@ t.test('exit handler never called', async t => {
194194
const { logs, errors } = await mockExitHandler(t, {
195195
config: { loglevel: 'silent' },
196196
})
197-
process.emit('exit', 1)
197+
process.emit('exit', 0)
198198
t.strictSame(logs, [])
199199
t.strictSame(errors(), [''], 'one empty string')
200+
t.equal(process.exitCode, 1)
200201
})
201202

202203
t.test('loglevel notice', async (t) => {
203204
const { logs, errors } = await mockExitHandler(t)
204-
process.emit('exit', 1)
205-
t.equal(process.exitCode, 1)
205+
process.emit('exit', 2)
206+
t.equal(process.exitCode, 2)
206207
t.match(logs.error, [
207208
'Exit handler never called!',
208209
/error with npm itself/,

0 commit comments

Comments
 (0)
Please sign in to comment.