Skip to content

Commit 031ea04

Browse files
committedJul 20, 2021
fix(exit-handler): always warn if not called
If the exit handler wasn't called it is always a problem, even if there is no exit code.
1 parent b6e0997 commit 031ea04

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed
 

‎lib/utils/exit-handler.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ process.on('exit', code => {
5252

5353
if (!code)
5454
npm.log.info('ok')
55-
else {
55+
else
5656
npm.log.verbose('code', code)
57-
if (!exitHandlerCalled) {
58-
npm.log.error('', 'Exit handler never called!')
59-
console.error('')
60-
npm.log.error('', 'This is an error with npm itself. Please report this error at:')
61-
npm.log.error('', ' <https://github.com/npm/cli/issues>')
62-
// TODO this doesn't have an npm.config.loaded guard
63-
writeLogFile()
64-
}
57+
58+
if (!exitHandlerCalled) {
59+
process.exitCode = code || 1
60+
npm.log.error('', 'Exit handler never called!')
61+
console.error('')
62+
npm.log.error('', 'This is an error with npm itself. Please report this error at:')
63+
npm.log.error('', ' <https://github.com/npm/cli/issues>')
64+
// TODO this doesn't have an npm.config.loaded guard
65+
writeLogFile()
6566
}
6667
// In timing mode we always write the log file
6768
if (npm.config.loaded && npm.config.get('timing') && !wroteLogFile)

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,14 @@ t.test('defaults to log error msg if stack is missing', (t) => {
336336
t.end()
337337
})
338338

339-
t.test('exits cleanly when emitting exit event', (t) => {
340-
t.plan(1)
339+
t.test('exits uncleanly when only emitting exit event', (t) => {
340+
t.plan(2)
341341

342342
npm.log.level = 'silent'
343343
process.emit('exit')
344-
t.match(
345-
npm.log.record.find(r => r.level === 'info'),
346-
{ prefix: 'ok', message: '' }
347-
)
344+
const logData = fs.readFileSync(logFile, 'utf8')
345+
t.match(logData, 'Exit handler never called!')
346+
t.match(process.exitCode, 1, 'exitCode coerced to 1')
348347
t.end()
349348
})
350349

0 commit comments

Comments
 (0)