Search code examples
javascriptgulpeslint

Linting passes when executed from a gulp task, but it should fail


I am doing linting on my JavaScript code with this command:

eslint src/**/*.js

This finds hundreds of linting errors.

I am trying to make linting part of a pipeline. Therefore, I made a gulp task for it.

const gulp = require('gulp');
const eslint = require('gulp-eslint');

gulp.task('lint', () => {
    gulp.src(['src/**/*.js'])
        .pipe(eslint())
        .pipe(eslint.format())
        .pipe(eslint.failAfterError());
});

However, when I run gulp lint, it passes:

Starting 'lint'...
Finished 'lint' after 35 ms

I don't understand why it passes. I also tried this:

gulp.task('lint', () => {
    gulp.src(['src/**/*.js'])
        .pipe(eslint())
});

But this also passes.

Any idea why gulp lint doesn't fail with a linting error?


Solution

  • If you need Gulp to call the task and wait for it to complete then you need to return a JavaScript Promise from the task function.

    In this case, adding the keyword return before your gulp.src call in your lint funciton means that the Promise that the gulp.src returns is passed up to the underlying mechanism that Gulp uses and as such, Gulp can get the result from your linting process.

    const gulp = require('gulp');
    const eslint = require('gulp-eslint');
    
    gulp.task('lint', () => {
        return gulp.src(['src/**/*.js'])
            .pipe(eslint())
            .pipe(eslint.format())
            .pipe(eslint.failAfterError());
    });