Search code examples
windowsnode.jsgulpgulp-watch

Constant EBUSY issues on Windows


I have a very simple Gulpfile:

var gulp = require('gulp'),
    prefix = require('gulp-autoprefixer'),
    gsass = require('gulp-sass');
gulp.task('sass', function() {
    gulp.src('scss/*.scss')
        .pipe(gsass({
            unixNewlines: true,
            style: 'compact'
        }))
        .pipe(prefix('last 2 versions'))
        .pipe(gulp.dest('.'));
});

gulp.task('default', ['sass'], function() {
    gulp.watch('scss/*.scss', ['sass']);
});

When the SCSS file is changed and the sass task runs, I occasionally (about 10% of the time) get the following error message on the console:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: EBUSY, open 'C:\whatever\scss\main.scss'

The error in question happens during the call to gulp.src. Passing the read: false option into that function stops the error from happening (but, of course, prevents sass from working).

This error is a problem because the watch task stops running whenever the error is encountered. Neither gulp-plumber nor .on('error', ...) help with this problem.

Is there a way to work around this issue? I'm on Windows, if it's relevant.

A few more relevant points:

  • Removing sass entirely doesn't fix the issue.
  • Putting a try/catch around the contents of either task also doesn't work.
  • I'm using Sublime Text 3 to edit the file. I think the problem is that gulp.src tries to read the file while Sublime is still writing to it (though I'm not entirely sure).

Solution

  • So I'm not entirely sure about this because I don't run Windows, but here's some pointers:

    • Like I mentioned in the comment, always return your streams from gulp tasks. That way gulp's task manager knows when the task finishes and can properly sequence your dependencies.
    • If you are overwriting files that are being used by the system, it's not recommended. See this issue for a little more detail: https://github.com/joyent/node/issues/3017
    • I'm not sure this will help either, but you could try writing your compiled CSS into a subdirectory, say ./dist/css or something along those lines.
    • It may be worth opening an issue (after trying the above points first!) on vinyl-fs which is the module used for gulp.src etc.