gulp icon indicating copy to clipboard operation
gulp copied to clipboard

gulp.watch: Error: watch ENOENT

Open caiotoon opened this issue 12 years ago • 17 comments

I'm watching a folder for changes and deleting some folders and processing image files. I then intermittently get this error.

Sometimes, by restarting the watcher, the errors seems to disappear.

The last error on the stacktrace was errnoException.

As background, I've seem this issue on Gaze. This issue has been fixed already, and I'm not sure if Gulp uses the latest version. I then searched for vinyl-fs, which Gulp uses for watching, and saw this commit, which means we're probably not using the latest version of Gaze for Gulp (not entirely sure, though).

Does it make any sense? Any idea on what could it be and how to investigate further?

Error: watch ENOENT
    at errnoException (fs.js:1019:11)
    at FSWatcher.start (fs.js:1051:11)
    at Object.fs.watch (fs.js:1076:11)
    at Gaze._watchDir (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\gaze.js:289:30)
    at code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\gaze.js:358:10
    at iterate (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\helper.js:52:5)
    at code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\helper.js:61:11
    at code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\gaze.js:420:5
    at iterate (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\helper.js:52:5)
    at code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\helper.js:61:11
    at code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\gaze.js:420:5
    at iterate (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\helper.js:52:5)
    at Object.forEachSeries (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\helper.js:66:3)
    at Gaze._initWatched (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\gaze.js:354:10)
    at Gaze._internalAdd (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\gaze.js:193:10)
    at code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\gaze.js:400:16
    at Array.forEach (native)
    at code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\glob-watcher\node_modules\gaze\lib\gaze.js:396:12
    at ReaddirReq.Req.done (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\graceful-fs\graceful-fs.js:143:5)
    at ReaddirReq.done (code\source\node_modules\gulp\node_modules\vinyl-fs\node_modules\graceful-fs\graceful-fs.js:90:22)
    at Object.oncomplete (fs.js:107:15)

caiotoon avatar Apr 23 '14 21:04 caiotoon

cc @shama

yocontra avatar Apr 23 '14 21:04 yocontra

This is fixed in the latest version of gaze but some other issues are preventing gulp from updating to that version.

This error is strange though and I've never been able to really reproduce. Here is the code in [email protected] where the error isn't caught:

Gaze.prototype._watchDir = function(dir, done) {
  var self = this;
  var timeoutId;
  try {
    this._watchers[dir] = fs.watch(dir, function(event) {
      // race condition. Let's give the fs a little time to settle down. so we
      // don't fire events on non existent files.
      clearTimeout(timeoutId);
      timeoutId = setTimeout(function() {
        // race condition. Ensure that this directory is still being watched
        // before continuing.
        if ((dir in self._watchers) && fs.existsSync(dir)) {
          done(null, dir);
        }
      }, delay + 100);
    });
  } catch (err) {
    return this._handleError(err);
  }
  return this;
};

The try/catch around fs.watch should catch the ENOENT.

Here is a permutation of trying to get fs.watch to throw a ENOENT that isn't caught:

var fs = require('fs');
var path = require('path');

var filename = path.resolve('tmp');
fs.mkdirSync(filename);

try {
  fs.rmdirSync(filename);
  fs.watch(filename, function() {
    console.log('changed');
  });
} catch (err) {
  console.error('error caught', err);
}

The error will be caught. I've tried many variations of the above with the error is being caught every time.

So maybe I'm missing something but I can't seem to reproduce the issue.

shama avatar Apr 24 '14 16:04 shama

Hi @contra, do you think you are close to be able to upgrade the version of Gaze for the next Gulp release?

@caiotoon did you find any workarounds on this issue?

I ended up using Gaze directly, for now... it works fine.

gulp.task('watch', ['build'], function () {
  var gaze = require('gaze')
  var path = require('path')

  return gaze(['src/app/**', 'src/app/**/*'], function () {

    this.on('all', function (action, filepath) {
      gutil.log(color.magenta(filepath), 'was', color.yellow(action))

      if (path.extname(filepath) === '.js') {
        gulp.start('build:js')
      }
      else if (path.relative(__dirname, filepath) === 'src/app/index.html') {
        gulp.start('build:html')
      }
      else if (action !== 'deleted') {
        gulp.start('build:assets')
      }
    })

  })
})

lufzle avatar Aug 10 '14 17:08 lufzle

@luisfarzati It's on me atm. Please follow this issue on gaze for when it is ready to upgrade: https://github.com/shama/gaze/issues/137 Thanks!

shama avatar Aug 10 '14 18:08 shama

:+1:

mietek avatar Aug 30 '14 03:08 mietek

Rolling into #651

yocontra avatar Sep 01 '14 16:09 yocontra

@shama I'm having the same issue, and the error is actually being caught by the catch there, but it get thrown up againg at the Gaze's handler:

gaze/lib/gaze.js:433:

// If an error, handle it here
Gaze.prototype._handleError = function(err) {
  if (err.code === 'EMFILE') {
    return this.emit('error', new Error('EMFILE: Too many opened files.'));
  }
  return this.emit('error', err);
};

From there I'm not really sure who works on the error.

lucasconstantino avatar Sep 05 '14 17:09 lucasconstantino

I have some error

Error: watch ENOENT
    at errnoException (fs.js:1028:11)
    at FSWatcher.start (fs.js:1060:11)
    at Object.fs.watch (fs.js:1085:11)
    at Gaze._watchDir (/home/admin/web/default.dev.itgalaxy.company/public_html/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:289:30)
    at /home/admin/web/default.dev.itgalaxy.company/public_html/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:358:10
    at iterate (/home/admin/web/default.dev.itgalaxy.company/public_html/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /home/admin/web/default.dev.itgalaxy.company/public_html/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /home/admin/web/default.dev.itgalaxy.company/public_html/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/home/admin/web/default.dev.itgalaxy.company/public_html/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /home/admin/web/default.dev.itgalaxy.company/public_html/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11

if i deleted folder

alexander-akait avatar Nov 13 '14 13:11 alexander-akait

Got this when switching a git branch while running gulp.

gulp 3.8.10 (includes gaze 0.5.1) node 0.10.33

Error: watch ENOENT
  at errnoException (fs.js:1030:11)
  at FSWatcher.start (fs.js:1062:11)
  at Object.fs.watch (fs.js:1087:11)
  at Gaze._watchDir (/home/antti/work/web/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:289:30)
  ...

Full stack trace

apaatsio avatar Dec 01 '14 12:12 apaatsio

In a possibly related issue shama/gaze#114 the fix will be in gaze 0.6

apaatsio avatar Dec 01 '14 12:12 apaatsio

Still getting this issue in [email protected]

gulp crashed when I renamed a directory.

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: watch ENOENT
    at exports._errnoException (util.js:746:11)
    at FSWatcher.start (fs.js:1172:11)
    at Object.fs.watch (fs.js:1198:11)
    at Gaze._watchDir (/Users/me/Projects/merithon/app/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:289:30)
    at /Users/me/Projects/merithon/app/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:358:10
    at iterate (/Users/me/Projects/merithon/app/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/me/Projects/merithon/app/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/me/Projects/merithon/app/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/me/Projects/merithon/app/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/me/Projects/merithon/app/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11

lindskogen avatar Jun 24 '15 12:06 lindskogen

Getting the same error, I din't rename the directory, I'm using Linux

Node-v 0.10.33 [email protected]

Rolando-Barbella avatar Jun 24 '15 16:06 Rolando-Barbella

+1 I'm seeing this too, when creating a directory in a directory that is being watched by images/**/*.{jpg,gif,png,svg}

andypearson avatar Jul 21 '15 18:07 andypearson

I'm getting this error with Gulp 4.0 when deleting a directory.

adamreisnz avatar Jul 26 '15 00:07 adamreisnz

Also getting this with 4.0 when renaming a directory.

fusepilot avatar Jul 31 '15 00:07 fusepilot

@contra do you want to re-open this issue?

adamreisnz avatar Jul 31 '15 00:07 adamreisnz

Again, please see https://github.com/gulpjs/gulp/issues/651 this is a duplicate

yocontra avatar Jul 31 '15 04:07 yocontra