Skip to content

Commit 67e067e

Browse files
Eran LevinTrott
Eran Levin
authored andcommitted
fs: watch signals for recursive incompatibility
This pull request makes fs.watch throw exception, whenever it is used in an incompatible platform. For this change following changes were made to api: 1.a new error type has been introduced. 2.fs.watch has been changed accordingly. Users who use recursive on non-windows and osx platforms, will face a new exception. For this reason, it's a breaking change. Fixes: #29901 PR-URL: #29947 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 4fefd18 commit 67e067e

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

doc/api/errors.md

+6
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,12 @@ The `--entry-type=...` flag is not compatible with the Node.js REPL.
24122412
Used when an [ES Module][] loader hook specifies `format: 'dynamic'` but does
24132413
not provide a `dynamicInstantiate` hook.
24142414

2415+
<a id="ERR_FEATURE_UNAVAILABLE_ON_PLATFORM"></a>
2416+
#### `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM`
2417+
2418+
Used when a feature that is not available
2419+
to the current platform which is running Node.js is used.
2420+
24152421
<a id="ERR_STREAM_HAS_STRINGDECODER"></a>
24162422
#### `ERR_STREAM_HAS_STRINGDECODER`
24172423

doc/api/fs.md

+2
Original file line numberDiff line numberDiff line change
@@ -3766,6 +3766,8 @@ The `fs.watch` API is not 100% consistent across platforms, and is
37663766
unavailable in some situations.
37673767

37683768
The recursive option is only supported on macOS and Windows.
3769+
An `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` exception will be thrown
3770+
when the option is used on a platform that does not support it.
37693771

37703772
#### Availability
37713773

lib/fs.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ const {
6262
ERR_FS_FILE_TOO_LARGE,
6363
ERR_INVALID_ARG_VALUE,
6464
ERR_INVALID_ARG_TYPE,
65-
ERR_INVALID_CALLBACK
65+
ERR_INVALID_CALLBACK,
66+
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM
6667
},
6768
uvException
6869
} = require('internal/errors');
@@ -129,6 +130,7 @@ let FileReadStream;
129130
let FileWriteStream;
130131

131132
const isWindows = process.platform === 'win32';
133+
const isOSX = process.platform === 'darwin';
132134

133135

134136
function showTruncateDeprecation() {
@@ -1359,7 +1361,8 @@ function watch(filename, options, listener) {
13591361

13601362
if (options.persistent === undefined) options.persistent = true;
13611363
if (options.recursive === undefined) options.recursive = false;
1362-
1364+
if (options.recursive && !(isOSX || isWindows))
1365+
throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM('watch recursively');
13631366
if (!watchers)
13641367
watchers = require('internal/fs/watchers');
13651368
const watcher = new watchers.FSWatcher();

lib/internal/errors.js

+4
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,10 @@ E('ERR_FALSY_VALUE_REJECTION', function(reason) {
803803
this.reason = reason;
804804
return 'Promise was rejected with falsy value';
805805
}, Error);
806+
E('ERR_FEATURE_UNAVAILABLE_ON_PLATFORM',
807+
'The feature %s is unavailable on the current platform' +
808+
', which is being used to run Node.js',
809+
TypeError);
806810
E('ERR_FS_FILE_TOO_LARGE', 'File size (%s) is greater than 2 GB', RangeError);
807811
E('ERR_FS_INVALID_SYMLINK_TYPE',
808812
'Symlink type must be one of "dir", "file", or "junction". Received "%s"',

test/parallel/test-fs-watch-recursive.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
const common = require('../common');
44

5-
if (!(common.isOSX || common.isWindows))
6-
common.skip('recursive option is darwin/windows specific');
75

86
const assert = require('assert');
97
const path = require('path');
@@ -20,6 +18,11 @@ const testsubdir = fs.mkdtempSync(testDir + path.sep);
2018
const relativePathOne = path.join(path.basename(testsubdir), filenameOne);
2119
const filepathOne = path.join(testsubdir, filenameOne);
2220

21+
if (!common.isOSX && !common.isWindows) {
22+
assert.throws(() => { fs.watch(testDir, { recursive: true }); },
23+
{ code: 'ERR_FEATURE_UNAVAILABLE_ON_PLATFORM' });
24+
return;
25+
}
2326
const watcher = fs.watch(testDir, { recursive: true });
2427

2528
let watcherClosed = false;

0 commit comments

Comments
 (0)