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

Allow Sass compilation to restart after gulp dev errors #3034

Merged
merged 2 commits into from
Nov 28, 2022

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Nov 23, 2022

This PR fixes an issue with our Sass compilation compile:scss Gulp task

  1. Begin npm start
  2. Make any Sass *.scss invalid, save it
  3. Wait for gulp.watch() to kick in
  4. Sass compilation compile:scss Gulp task shows error + hangs 🔥
  5. Uh oh, have to cancel and run npm start again 😭

Development: we now .emit('end') for the Sass compiler to resume on next save
Production: we still .emit('error') for the Sass compiler to crash and break the build

Merge this PR and you can save, error, save, error, save, fix—all day long 😊

@colinrotherham
Copy link
Contributor Author

Probably also comes under #2243

@colinrotherham colinrotherham force-pushed the gulp-npm-errors branch 2 times, most recently from 6310282 to e3665b4 Compare November 23, 2022 18:20
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3034 November 23, 2022 18:24 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3034 November 23, 2022 23:46 Inactive
Comment on lines +28 to +29
compileStylesheets,
npmScriptTask('build:sassdoc')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, makes more sense to only build the docs if we could actually build the stylesheet 🙌🏻

Comment on lines 89 to 97
.pipe(sass(options).on('error', (cause) => {
const error = new PluginError('gulp-sass', cause.messageFormatted)
error.showProperties = false

// Gulp continue (watch)
if (isDev) {
console.error(error.toString())
return stream.emit('end')
}

// Gulp exit with error
return stream.emit('error', error)
Copy link
Member

@romaricpascal romaricpascal Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick I see what this does, but it feels a bit narrow. If we'd introduce a new plugin or custom transform that'd emit error, we'd be back to square one.

Wondering if something like adding a final handling of error on the watch stream (found via this StackOverflow comment) could get us not to worry about task errors in dev long term? It may be an outdated solution though 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry @romaricpascal I've only taken this from the docs:
https://github.com/dlmanning/gulp-sass#render-your-css

Their approach suggests we swallow all errors with this.emit('end') inside the logger:

.pipe(sass().on('error', sass.logError))

This is perfect for development and doesn't stop gulp.watch()
This is awful for production as we no longer "break the build"

So I've gone one step better and emitted an error when not developing

Wondering if something like adding a final handling of error on the watch stream (found via this StackOverflow comment) could get us not to worry about task errors in dev long term? It may be an outdated solution though 😬

We'd lose Gulp's PluginError() flagging gulp-sass as the culprit (with fancy error output). Plus we'd lose per-plugin control of error.showProperties = false where we know the log formatter already includes this

Also if you use stream.on('error') to trigger stream.emit('error', error) we get an infinite loop

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer to the gulp-sass recommendation 😄

I think what I was looking for is a more systematic approach to handling transforms breaking, as for now it happens with Sass, but could happen on other transforms and the watch task would break again. It felt very gulp-sass-specific, hence my link to the example that turns error into end (not error so no looping) globally on the watch streams (so the build still breaks when not watching).

Similarly, the PluginError bit should ideally be part of any piping, imho. This way it won't just be Sass letting us know that it's gulp-sass breaking (though the error and stacktrace will likely already provide plenty to pinpoint things, no?)

If we're happy to revisit if a newer plugin breaks, then fine going with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, yeah I understand, it's good feedback

I've pushed again with the error handler moved into gulp-plumber so it listens for PostCSS errors too

Similarly, the PluginError bit should ideally be part of any piping, imho. This way it won't just be Sass letting us know that it's gulp-sass breaking (though the error and stacktrace will likely already provide plenty to pinpoint things, no?)

Sounds good, I'll nominate you for taking this further 😆

Hopefully we're good to get this first improvement PR in though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! Yeah, using plumber to handle makes things more generic 🙌🏻 Shame Gulp doesn't have that built in 😢

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3034 November 28, 2022 13:59 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3034 November 28, 2022 14:04 Inactive
Base automatically changed from gulp-npm-errors to main November 28, 2022 14:33
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3034 November 28, 2022 14:43 Inactive
@colinrotherham colinrotherham merged commit db4f50d into main Nov 28, 2022
@colinrotherham colinrotherham deleted the gulp-plumber-sass branch November 28, 2022 14:51
@colinrotherham colinrotherham added the 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) label Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working the way it should (including incorrect wording in documentation) sass / css tooling
Projects
Development

Successfully merging this pull request may close these issues.

3 participants