Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show error log message for workspace run failures #7054

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/commands/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,28 @@
}

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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')
})
})
Loading