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

Ensure npm scripts (via Gulp) emit errors #3033

Merged
merged 2 commits into from
Nov 28, 2022
Merged

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Nov 23, 2022

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

[15:04:48] Starting 'npm run build:jsdoc --silent'...
[15:04:49] Finished 'npm run build:jsdoc --silent' after 1.22 s
[15:04:49] Starting 'npm run build:sassdoc --silent'...
[15:04:51] Finished 'npm run build:sassdoc --silent' after 1.51 s

After

[15:04:48] Starting 'npm run build:jsdoc'...
[15:04:49] Finished 'npm run build:jsdoc' after 1.22 s
[15:04:49] Starting 'npm run build:sassdoc'...
[15:04:51] Finished 'npm run build:sassdoc' after 1.51 s

We only add `--silent` as we have the Gulp runner’s “Starting” and “Finishing” output already
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3033 November 23, 2022 15:23 Inactive
@@ -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])
Copy link
Contributor Author

@colinrotherham colinrotherham Nov 23, 2022

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()))
Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

@colinrotherham colinrotherham Nov 28, 2022

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 🎉

Copy link
Contributor Author

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

@colinrotherham
Copy link
Contributor Author

Also helps reduce complexity logged in #2243

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3033 November 23, 2022 18:15 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3033 November 23, 2022 18:20 Inactive
Copy link
Member

@romaricpascal romaricpascal left a 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 😄

@colinrotherham colinrotherham merged commit 337e444 into main Nov 28, 2022
@colinrotherham colinrotherham deleted the gulp-npm-errors branch November 28, 2022 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

3 participants