-
Notifications
You must be signed in to change notification settings - Fork 331
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
Ensure npm scripts (via Gulp) emit errors #3033
Conversation
We only add `--silent` as we have the Gulp runner’s “Starting” and “Finishing” output already
@@ -12,10 +12,15 @@ export async function npmScript (name, args = []) { | |||
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm' | |||
|
|||
return new Promise((resolve, reject) => { | |||
const script = spawn(command, ['run', name, ...args]) | |||
const script = spawn(command, ['run', name, '--silent', ...args]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still add --silent
behind the scenes because we don't want both npm AND Gulp logging progress to the console—one CLI tool is enough
Note: Output from the npm task (linting for example) is not silenced
|
||
// Emit errors to error listener | ||
script.stderr.on('data', (data) => { | ||
script.emit('error', new Error(data.toString())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By emitting an error here it ensures we hit reject()
(below) via the error listener
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Won't that trigger multiple times if a script outputs to stderr
multiple times? Furthermore, I'm worried:
- it'll catch false errors for a script that just outputs to stderr as a convenient second buffer. I thought the actual marker of a script erroring was a non-zero exit (covered by the
close
) - it'll double up with the
close
handling later
question: Why are we emitting an error rather than directly rejecting? That feels an unnecessary extra step there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's confusing, probably why we wanted to remove Gulp 😭
Gulp sources are Node.js Stream
instances (or early "stream" implementations that our Gulp plugins use)
They extend Node.js EventEmitter
so we're going to follow the approach of expecting our tasks to emit:
task.emit('end') // Gulp piped task has finished processing
stream.emit('error') // Gulp stream has encountered an error
If we only reject()
the promise we miss all the lovely error messages (lint output, line numbers etc)
By emitting events we can ensure the Gulp runner stays in control (logs the error and exits) but it also lets us listen via .on('error')
and alternatively NOT exit (log only) like during gulp dev
🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't that trigger multiple times if a script outputs to stderr multiple times?
The first error written will hit script.on('error', reject)
so you'll only see the first one
Also helps reduce complexity logged in #2243 |
cd6f256
to
6310282
Compare
6310282
to
e3665b4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It all makes sense now, cheers for the explanation 😄
This PR ensure npm scripts (via Gulp) emit errors
We currently ignore npm script crashes even when they write to
stderr
(which wasn't intentional)I've also prevented the npm run
--silent
flag being logged to reduce Gulp CLI "log spam"Before
After