Skip to content

Commit 013f026

Browse files
committed
fix(exitHandler): write code to logfile
This moves the logging of the exit code to before the logfile is written, when the exit handler was not called. This will ensure that the code shows up in the debug logs. PR-URL: #3469 Credit: @wraithgar Close: #3469 Reviewed-by: @isaacs
1 parent cf55fe0 commit 013f026

3 files changed

Lines changed: 15 additions & 16 deletions

File tree

lib/utils/exit-handler.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ process.on('exit', code => {
5858
if (!code)
5959
log.info('ok')
6060
else {
61+
log.verbose('code', code)
6162
if (!exitHandlerCalled) {
6263
log.error('', 'Exit handler never called!')
6364
console.error('')
@@ -66,7 +67,6 @@ process.on('exit', code => {
6667
// TODO this doesn't have an npm.config.loaded guard
6768
writeLogFile()
6869
}
69-
log.verbose('code', code)
7070
}
7171
// In timing mode we always write the log file
7272
if (npm.config && npm.config.loaded && npm.config.get('timing') && !wroteLogFile)
@@ -107,8 +107,6 @@ const exit = (code, noLog) => {
107107
// background at this point, and this makes sure anything left dangling
108108
// for whatever reason gets thrown away, instead of leaving the CLI open
109109
process.stdout.write('', () => {
110-
// `|| process.exitCode` supports a single use case, where we set the exit
111-
// code to 1 if npm is called with no arguments
112110
process.exit(code)
113111
})
114112
}

tap-snapshots/test/lib/utils/exit-handler.js.test.cjs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
*/
77
'use strict'
88
exports[`test/lib/utils/exit-handler.js TAP handles unknown error > should have expected log contents for unknown error 1`] = `
9-
0 verbose code 1
10-
1 error foo A complete log of this run can be found in:
11-
1 error foo {CWD}/test/lib/utils/tap-testdir-exit-handler/_logs/expecteddate-debug.log
12-
2 verbose stack Error: ERROR
13-
3 verbose cwd {CWD}
14-
4 verbose Foo 1.0.0
15-
5 verbose argv "/node" "{CWD}/test/lib/utils/exit-handler.js"
16-
6 verbose node v1.0.0
17-
7 verbose npm v1.0.0
18-
8 error foo code ERROR
19-
9 error foo ERR ERROR
20-
10 error foo ERR ERROR
21-
11 verbose exit 1
9+
0 verbose stack Error: ERROR
10+
1 verbose cwd {CWD}
11+
2 verbose Foo 1.0.0
12+
3 verbose argv "/node" "{CWD}/test/lib/utils/exit-handler.js"
13+
4 verbose node v1.0.0
14+
5 verbose npm v1.0.0
15+
6 error foo code ERROR
16+
7 error foo ERR ERROR
17+
8 error foo ERR ERROR
18+
9 verbose exit 1
2219
2320
`

test/lib/utils/exit-handler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ process = Object.assign(
109109
// in order for tap to exit properly
110110
t.teardown(() => {
111111
process = _process
112+
})
113+
114+
t.afterEach(() => {
115+
// clear out the 'A complete log' message
112116
npmlog.record.length = 0
113117
})
114118

0 commit comments

Comments
 (0)