diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 8912015bb14818..ba0676f1b84da5 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3779,6 +3779,9 @@ It is recommended to use the `new` qualifier instead. This applies to all REPL c -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. diff --git a/lib/fs.js b/lib/fs.js index af72ac36144c55..e2996ba9ca4ef6 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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 @@ -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; } diff --git a/test/parallel/test-fs-exists.js b/test/parallel/test-fs-exists.js index 857f3f26174549..3be2197660ce8c 100644 --- a/test/parallel/test-fs-exists.js +++ b/test/parallel/test-fs-exists.js @@ -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')));