Skip to content

Commit

Permalink
fix: show error log message for workspace run failures
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Dec 2, 2023
1 parent 7788ef2 commit f1274d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/utils/exit-handler.js
Expand Up @@ -131,28 +131,27 @@ const exitHandler = err => {
log.level = level
}

let exitCode = process.exitCode || 0
let noLogMessage = exitCode !== 0
let exitCode = err ? 1 : process.exitCode || 0
let suppressLogMessage = exitCode === 0
let jsonError

if (err) {
exitCode = 1
// if we got a command that just shells out to something else, then it
// will presumably print its own errors and exit with a proper status
// code if there's a problem. If we got an error with a code=0, then...
// something else went wrong along the way, so maybe an npm problem?
const isShellout = npm.isShellout
const quietShellout = isShellout && typeof err.code === 'number' && err.code
if (quietShellout) {
exitCode = err.code
noLogMessage = true
exitCode = err.code || 0
suppressLogMessage = true
} else if (typeof err === 'string') {
// XXX: we should stop throwing strings
log.error('', err)
noLogMessage = true
suppressLogMessage = true
} else if (!(err instanceof Error)) {
log.error('weird error', err)
noLogMessage = true
suppressLogMessage = true
} else {
if (!err.code) {
const matchErrorCode = err.message.match(/^(?:Error: )?(E[A-Z]+)/)
Expand Down Expand Up @@ -208,11 +207,11 @@ const exitHandler = err => {
npm.flushOutput(jsonError)
}

log.verbose('exit', exitCode || 0)
log.verbose('exit', exitCode)

showLogFileError = (hasLoadedNpm && npm.silent) || noLogMessage
showLogFileError = (hasLoadedNpm && npm.silent) || suppressLogMessage
? false
: !!exitCode
: exitCode !== 0

// explicitly call process.exit now so we don't hang on things like the
// update notifier, also flush stdout/err beforehand because process.exit doesn't
Expand Down
20 changes: 20 additions & 0 deletions smoke-tests/test/workspace-run-script-error-logs.js
@@ -0,0 +1,20 @@

const t = require('tap')
const setup = require('./fixtures/setup.js')

t.test('basic', async t => {
const { npm } = await setup(t)

await t.test('npm install sends correct user-agent', async t => {
await npm('init', '-y')
await npm('init', '-y', `--workspace=pkga`)
await npm('init', '-y', `--workspace=pkgb`)

await npm('pkg', 'set', '-w=pkga', `scripts.hello=echo a`)
await npm('pkg', 'set', '-w=pkgb', `scripts.hello=exit 1`)

const logs = await npm('run', 'hello', '-ws').catch((r) => r.stderr)

t.match(logs, 'A complete log of this run can be found in:', 'has log message')
})
})

0 comments on commit f1274d3

Please sign in to comment.