diff --git a/lib/fs.js b/lib/fs.js index 58704e529747..e0a6ad32fea9 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1278,7 +1278,7 @@ function StatWatcher() { this._handle.onchange = function(current, previous, newStatus) { if (oldStatus === -1 && - newStatus === -1 && + newStatus < 0 && current.nlink === previous.nlink) return; oldStatus = newStatus; diff --git a/test/parallel/test-fs-watch-file-nonexistent.js b/test/parallel/test-fs-watch-file-nonexistent.js new file mode 100644 index 000000000000..cd45056c63fd --- /dev/null +++ b/test/parallel/test-fs-watch-file-nonexistent.js @@ -0,0 +1,13 @@ +'use strict'; + +var fs = require('fs'); + +var filename = '/path/to/file/that/does/not/exist'; + +fs.watchFile(filename, function() { + throw new Error('callback should not be called for non-existent files'); +}); + +setTimeout(function() { + fs.unwatchFile(filename); +}, 10);