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 2ae7fd8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
20 changes: 13 additions & 7 deletions lib/commands/run-script.js
Expand Up @@ -194,22 +194,28 @@ class RunScript extends BaseCommand {
}

async runWorkspaces (args, filters) {
const res = []
await this.setWorkspaces()
const errors = []

for (const workspacePath of this.workspacePaths) {
const { content: pkg } = await pkgJson.normalize(workspacePath)
const runResult = await this.run(args, {
path: workspacePath,
pkg,
}).catch(err => {
try {
await this.run(args, {
path: workspacePath,
pkg,
})
} catch (err) {
log.error(`Lifecycle script \`${args[0]}\` failed with error:`)
log.error(err)
log.error(` in workspace: ${pkg._id || pkg.name}`)
log.error(` at location: ${workspacePath}`)
errors.push(err)
process.exitCode = 1
})
res.push(runResult)
}
}

if (errors.some(r => r?.message.startsWith('Missing script'))) {
throw new Error(`Missing script: ${args[0]}`)

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 18.17.0

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 18.17.0

Missing script: missing-script

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 18.17.0

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 18.x

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 18.x

Missing script: missing-script

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 18.x

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.5.0

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.5.0

Missing script: missing-script

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.5.0

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.x

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.x

Missing script: missing-script

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - Linux - 20.x

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 18.17.0

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 18.17.0

Missing script: missing-script

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 18.17.0

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 18.x

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 18.x

Missing script: missing-script

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 18.x

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.5.0

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.5.0

Missing script: missing-script

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.5.0

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.x

Missing script: test

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.x

Missing script: missing-script

Check failure on line 218 in lib/commands/run-script.js

View workflow job for this annotation

GitHub Actions / Test - macOS - 20.x

Missing script: test
}
}

Expand Down
21 changes: 8 additions & 13 deletions lib/utils/exit-handler.js
Expand Up @@ -131,28 +131,25 @@ const exitHandler = err => {
log.level = level
}

let exitCode = process.exitCode || 0
let noLogMessage = exitCode !== 0
let exitCode = err ? 1 : process.exitCode || 0
let suppressLogMessage = !err
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) {
if (npm.isShellout && typeof err.code === 'number' && err.code) {
exitCode = err.code
noLogMessage = true
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 +205,9 @@ const exitHandler = err => {
npm.flushOutput(jsonError)
}

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

showLogFileError = (hasLoadedNpm && npm.silent) || noLogMessage
? false
: !!exitCode
showLogFileError = npm?.silent || suppressLogMessage ? false : 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
19 changes: 19 additions & 0 deletions smoke-tests/test/workspace-run-script-error-logs.js
@@ -0,0 +1,19 @@

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`)

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 2ae7fd8

Please sign in to comment.