Skip to content

Commit

Permalink
fix(exit-handler): always warn if not called
Browse files Browse the repository at this point in the history
If the exit handler wasn't called it is always a problem, even if there
is no exit code.

PR-URL: #3561
Credit: @wraithgar
Close: #3561
Reviewed-by: @lukekarrys
  • Loading branch information
wraithgar committed Jul 22, 2021
1 parent 1fe1c9b commit 009ad1e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
19 changes: 10 additions & 9 deletions lib/utils/exit-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ process.on('exit', code => {

if (!code)
npm.log.info('ok')
else {
else
npm.log.verbose('code', code)
if (!exitHandlerCalled) {
npm.log.error('', 'Exit handler never called!')
console.error('')
npm.log.error('', 'This is an error with npm itself. Please report this error at:')
npm.log.error('', ' <https://github.com/npm/cli/issues>')
// TODO this doesn't have an npm.config.loaded guard
writeLogFile()
}

if (!exitHandlerCalled) {
process.exitCode = code || 1
npm.log.error('', 'Exit handler never called!')
console.error('')
npm.log.error('', 'This is an error with npm itself. Please report this error at:')
npm.log.error('', ' <https://github.com/npm/cli/issues>')
// TODO this doesn't have an npm.config.loaded guard
writeLogFile()
}
// In timing mode we always write the log file
if (npm.config.loaded && npm.config.get('timing') && !wroteLogFile)
Expand Down
11 changes: 5 additions & 6 deletions test/lib/utils/exit-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,14 @@ t.test('defaults to log error msg if stack is missing', (t) => {
t.end()
})

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

npm.log.level = 'silent'
process.emit('exit')
t.match(
npm.log.record.find(r => r.level === 'info'),
{ prefix: 'ok', message: '' }
)
const logData = fs.readFileSync(logFile, 'utf8')
t.match(logData, 'Exit handler never called!')
t.match(process.exitCode, 1, 'exitCode coerced to 1')
t.end()
})

Expand Down

0 comments on commit 009ad1e

Please # to comment.