Add option for `gulp.watch` to ignore task errors
See original GitHub issueI’ve opened this as a duplicate of this closed issue because conversation is locked there and it still not solved in Gulp 4 as it was promised (proof).
What were you expecting to happen?
I expect gulp.watch
to continue watching files and executing it’s task even after it throws an error.
What actually happened?
If task passed to gulp.watch
throws an error, watching stops and gulp
process exits.
This is undesired behavior.
Please post a sample of your gulpfile (preferably reduced to just the bit that’s not working)
const SCRIPTS = './src/**/*.js';
gulp.task(compileScripts);
gulp.task(watch);
gulp.task('default', watch);
function compileScripts() {
return gulp
.src(SCRIPTS)
.pipe(babel())
.pipe(gulp.dest('dist'));
}
function watch() {
gulp.watch(SCRIPTS, {ignoreInitial: false}, compileScripts);
}
When I run gulp watch
it compiles scripts and wait’s for changes.
The problem is if I make a syntax error in some watched source file gulp
process will be terminated but I want it to continue watching for changes and just log this error to the console.
Of course I could add plumber()
to compileScripts
task but in this case gulp compileScripts
would swallow compilation error and exit with zero code.
So, what I’m trying to achieve is:
- Execution of
gulp compileScripts
from console should fail with compilation error (exit with non-zero code) - Execution of
gulp watch
should not fail on compilation errors, but should just log them.
Is there a way to achieve it with Gulp 4?
What version of gulp are you using? 4.0
What versions of npm and node are you using? node v8.9.3 npm v5.6.0
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:6 (4 by maintainers)
Top GitHub Comments
If you mean something like this:
…then after the first error it’s successfully printed to the console and process doesn’t exit but watcher doesn’t react on file changes anymore.
Here is the solution that works as expected:
…but why do we need all these
plumber
s andthrough2
s for such a simple and obvious task that should be supported out of the box and IMHO should be a default behavior?Why you don’t what to just add some option to
gulp.watch
?Because your code doesn’t work as it should -
gulp watch
fails on syntax error in sources andgulp
process exits.Ok, to support my bare words I’ve created a reproduction repo: https://github.com/th0r/gulp-watch-issue
Setup: clone repo and run
npm i
Reproduction case: runnpm run watch
and make some syntax error inindex.js
What happens: gulp process exits What should happen: gulp process should not exit - it should just log syntax error. Also it should runcompileScripts
task again after you fix this syntax error and continue watchingindex.js
.