2

Firstly, here is a repo with very simple example of my problem: https://github.com/nkoder/example-of-gulp-less-zero-exit-on-error

I want to define gulp build pipeline which fails on any error. This pipeline would be used in automated process (eg. Continuous Integration) so I need the build command to return non-zero exit code on failure. It works for me with invalid Jade templates or non-passing tests run with Karma.

The problem is when I use gulp-less to prepare CSS from LESS. Whenever I add any pipeline step after gulp-less execution, all errors are "consumed" and the whole task exits with 0. But it shouldn't.

  • The first question is: How to force wrong task to fail with non-zero exit code? (solved, see answer below)
  • The second question is: Why error event emitted in less() call followed by piped stream is not making a whole task to return non-zero exit code? Non-zero is returned in some other similar cases, eg. in Jade or ESLint failure. (not solved yet)

Versions:

  • node 5.11.0
  • npm 3.8.6
  • gulp 3.9.1
  • gulp-less 3.1.0
Beetroot Paul
  • 101
  • 2
  • 9

1 Answers1

1

Listen for the error event and then exit the process explicitly:

gulp.task('wrong', function () {
  return gulp
    .src('invalid-styles.less')
    .pipe(less().on('error', function(error) {
      console.log(error.message);
      process.exit(1);
    }))
    .pipe(gulp.dest('generated-styles.css'));
});
Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
  • 1
    Thanks for this solution! I'm not a big fan of calling `process.exit(...)` explicitly but if it is the only way to have `less()` failing properly, then OK. – Beetroot Paul May 07 '16 at 20:19