gulp.watch: Error: watch ENOENT
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)
cc @shama
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.
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')
}
})
})
})
@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!
:+1:
Rolling into #651
@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.
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
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)
...
In a possibly related issue shama/gaze#114 the fix will be in gaze 0.6
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
Getting the same error, I din't rename the directory, I'm using Linux
Node-v 0.10.33 [email protected]
+1 I'm seeing this too, when creating a directory in a directory that is being watched by images/**/*.{jpg,gif,png,svg}
I'm getting this error with Gulp 4.0 when deleting a directory.
Also getting this with 4.0 when renaming a directory.
@contra do you want to re-open this issue?
Again, please see https://github.com/gulpjs/gulp/issues/651 this is a duplicate