Skip to content

Commit

Permalink
fs: deprecate passing invalid types in fs.existsSync
Browse files Browse the repository at this point in the history
PR-URL: nodejs#55753
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
Ceres6 authored Jan 10, 2025
1 parent a627a99 commit 529b56e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3779,14 +3779,17 @@ It is recommended to use the `new` qualifier instead. This applies to all REPL c

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/55753

Check warning on line 3783 in doc/api/deprecations.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: Runtime deprecation.
- version:
- v23.4.0
- v22.13.0
pr-url: https://github.com/nodejs/node/pull/55892
description: Documentation-only.
-->

Type: Documentation-only
Type: Runtime

Passing non-supported argument types is deprecated and, instead of returning `false`,
will throw an error in a future version.
Expand Down
13 changes: 7 additions & 6 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,7 @@ ObjectDefineProperty(exists, kCustomPromisifiedSymbol, {
},
});

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

Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-fs-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ assert(fs.existsSync(f));
assert(!fs.existsSync(`${f}-NO`));

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

0 comments on commit 529b56e

Please sign in to comment.