Skip to content

Commit f6467b0

Browse files
doc: document fs.watchFile behaviour on ENOENT
When fs.watchFile encounters an ENOENT error, it invokes the given callback with some error data. This caused an issue as it was different behaviour than Node v0.10. Instead of changing this behaviour, document it and add a test. Ref: #1745 Ref: #2028
1 parent 14354a0 commit f6467b0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

doc/api/fs.markdown

+3
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ These stat objects are instances of `fs.Stat`.
576576
If you want to be notified when the file was modified, not just accessed
577577
you need to compare `curr.mtime` and `prev.mtime`.
578578

579+
_Note: when an `fs.watchFile` operation results in an `ENOENT` error, it will
580+
invoke the callback once. This is a change in functionality since v0.10._
581+
579582
_Note: `fs.watch` is more efficient than `fs.watchFile` and `fs.unwatchFile`.
580583
`fs.watch` should be used instead of `fs.watchFile` and `fs.unwatchFile`
581584
when possible._

test/parallel/test-fs-watchfile.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22

33
const fs = require('fs');
4+
const path = require('path');
45
const assert = require('assert');
6+
const common = require('../common');
7+
const fixtures = path.join(__dirname, '..', 'fixtures');
58

69
// Basic usage tests.
710
assert.throws(function() {
@@ -15,3 +18,9 @@ assert.throws(function() {
1518
assert.throws(function() {
1619
fs.watchFile(new Object(), function() {});
1720
}, /Path must be a string/);
21+
22+
// Test ENOENT. Should fire once.
23+
const enoentFile = path.join(fixtures, 'empty', 'non-existent-file');
24+
fs.watchFile(enoentFile, common.mustCall(function(curr, prev) {
25+
fs.unwatchFile(enoentFile);
26+
}));

0 commit comments

Comments
 (0)