Skip to content

Commit 529b56e

Browse files
authored
fs: deprecate passing invalid types in fs.existsSync
PR-URL: #55753 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a627a99 commit 529b56e

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3779,14 +3779,17 @@ It is recommended to use the `new` qualifier instead. This applies to all REPL c
37793779

37803780
<!-- YAML
37813781
changes:
3782+
- version: REPLACEME
3783+
pr-url: https://github.com/nodejs/node/pull/55753
3784+
description: Runtime deprecation.
37823785
- version:
37833786
- v23.4.0
37843787
- v22.13.0
37853788
pr-url: https://github.com/nodejs/node/pull/55892
37863789
description: Documentation-only.
37873790
-->
37883791

3789-
Type: Documentation-only
3792+
Type: Runtime
37903793

37913794
Passing non-supported argument types is deprecated and, instead of returning `false`,
37923795
will throw an error in a future version.

lib/fs.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,7 @@ ObjectDefineProperty(exists, kCustomPromisifiedSymbol, {
273273
},
274274
});
275275

276-
// fs.existsSync never throws, it only returns true or false.
277-
// Since fs.existsSync never throws, users have established
278-
// the expectation that passing invalid arguments to it, even like
279-
// fs.existsSync(), would only get a false in return, so we cannot signal
280-
// validation errors to users properly out of compatibility concerns.
281-
// TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior
276+
let showExistsDeprecation = true;
282277
/**
283278
* Synchronously tests whether or not the given path exists.
284279
* @param {string | Buffer | URL} path
@@ -288,6 +283,12 @@ function existsSync(path) {
288283
try {
289284
path = getValidatedPath(path);
290285
} catch {
286+
if (showExistsDeprecation) {
287+
process.emitWarning(
288+
'Passing invalid argument types to fs.existsSync is deprecated', 'DeprecationWarning', 'DEP0187',
289+
);
290+
showExistsDeprecation = false;
291+
}
291292
return false;
292293
}
293294

test/parallel/test-fs-exists.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ assert(fs.existsSync(f));
5151
assert(!fs.existsSync(`${f}-NO`));
5252

5353
// fs.existsSync() never throws
54+
const msg = 'Passing invalid argument types to fs.existsSync is deprecated';
55+
common.expectWarning('DeprecationWarning', msg, 'DEP0187');
5456
assert(!fs.existsSync());
5557
assert(!fs.existsSync({}));
5658
assert(!fs.existsSync(new URL('https://foo')));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy