From 009ad1e683aa061d7e5c78b9362b0bd1b14ee643 Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 20 Jul 2021 07:53:08 -0700 Subject: [PATCH] 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. PR-URL: https://github.com/npm/cli/pull/3561 Credit: @wraithgar Close: #3561 Reviewed-by: @lukekarrys --- lib/utils/exit-handler.js | 19 ++++++++++--------- test/lib/utils/exit-handler.js | 11 +++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/utils/exit-handler.js b/lib/utils/exit-handler.js index 95c9655a716c..7be138d2c361 100644 --- a/lib/utils/exit-handler.js +++ b/lib/utils/exit-handler.js @@ -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('', ' ') - // 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('', ' ') + // 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) diff --git a/test/lib/utils/exit-handler.js b/test/lib/utils/exit-handler.js index 981ac9a32b68..c88a1aef6792 100644 --- a/test/lib/utils/exit-handler.js +++ b/test/lib/utils/exit-handler.js @@ -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() })