Gulp returns 0 when tasks fail

Roberto Bonvallet picture Roberto Bonvallet · Feb 10, 2014 · Viewed 21.1k times · Source

I'm using Gulp in my small project in order to run tests and lint my code. When any of those tasks fail, Gulp always exits with return code 0. If I run jshint by hand, it exits with non-zero code as it should.

Here's my very simple gulpfile.

Do I need to somehow explicitly tell Gulp to return a meaningful value? Is this Gulp's fault, or maybe the gulp-jshint and gulp-jasmine plugins are to blame?

Answer

Shuhei Kagawa picture Shuhei Kagawa · Feb 11, 2014

You need to 'return gulp.src(...' so that the task waits for the returned stream.

EDIT

Gulp tasks are asynchronous by nature. The actual task is not executed yet at the time of 'gulp.src(...).pipe(...);'. In your example, the gulp tasks mark their results as success before the actual tasks are executed.

There are some ways to make gulp to wait for your actual task. Use a callback or return a stream or promise.

https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support

The easiest way is just returning the stream of 'gulp.src(...).pipe(...)'. If gulp task gets a stream, it will listen to 'end' event and 'error' event. They corresponds to return code 0 and 1. So, a full example for your 'lint' task would be:

gulp.task('lint', function () {
    return gulp.src('./*.js')
               .pipe(jshint('jshintrc.json'))
               .pipe(jshint.reporter('jshint-stylish'));
});

A bonus is that now you can measure the actual time spent on your tasks.