Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marking async task as failed and have gulp exit with non-zero exit code

Tags:

gulp

Consider this simple async gulp task (this is obviously not a real-life task but nicely illustrates a general problem of marking async tests as failed):

gulp.task('foo', function (done) {
    setTimeout(function () {
        //HERE: how to fail the gulp run and have it exit wit non-zero status?
    }, 1000);
});

I would like to know how to mark the foo task as failed so Gulp aborts the build and return non-zero exit code status to the shell. The things I've tried:

  • done(1) - finishes the task but gulp exists with code 0
  • done(new Error(fail)) - finishes the task but gulp exists with code 0
  • throw new Error(fail) - does the trick but is seems "a bit brutal"

What would be an idiomatic way of marking such an async task as failed?

like image 894
pkozlowski.opensource Avatar asked Dec 05 '25 04:12

pkozlowski.opensource


1 Answers

It turned out to be a bug in the gulp itself that was fixed in the version 3.8.4 via https://github.com/gulpjs/gulp/commit/d8d7542f58046f15d88795e3c48f952f54a02c3c

Starting from 3.8.4 one can write:

gulp.task('foo', function (done) {
    setTimeout(function () {
        done('error');
    }, 1000);
});

and have gulp properly exiting process with the return status 1.

like image 181
pkozlowski.opensource Avatar answered Dec 09 '25 16:12

pkozlowski.opensource



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!