-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs: add exists
to fsPromises
#53737
fs: add exists
to fsPromises
#53737
Conversation
For context, sync /**
* Tests whether or not the given path exists.
* @param {string | Buffer | URL} path
* @param {(exists?: boolean) => any} callback
* @returns {void}
*/
function exists(path, callback) {
validateFunction(callback, 'cb');
function suppressedCallback(err) {
callback(!err);
}
try {
fs.access(path, F_OK, suppressedCallback);
} catch {
return callback(false);
}
} |
CC @nodejs/fs |
This has already been discussed several times, and no consensus. I'm +1 for this PR, but unfortunately, it is unlikely to be merged. |
#39968 (from back in 2021) didn't reach consensus, but it also staled out (as in, there hasn't been activity in multiple years). Therefor, I'm not confident in this, up-to-date PR for a good discussion. The main argument against this is that it's easy to do in userland, but if you look at |
If you look closer, you'll see it's deprecated. |
🤣 apparently it is, but |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are unaddressed concerns from #39960:
- The name is misleading: Somehow support checking if a path exists while using fs.promises #39960 (comment)
- Checking a file status asynchronously means you're by design getting an outdated info, and that would make it easier for users to fall into the trap of introducing race conditions in their code. Somehow support checking if a path exists while using fs.promises #39960 (comment)
|
||
* `path` {string|URL} The filepath to check. | ||
|
||
Checks whether the given filepath exists. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not correct, the current implementation checks if the current process can access a file at that path.
Sorry for the noise. I should've checked multiple issues and past attempts before considering this change. I now see that excluding this function was a deliberate decision, and maybe it's best if the promises api doesn't have it. |
@redyetidev I don't think anyone reading through #39960 would conclude that the exclusion of
The races in issue are about the Linux kernel and POSIX process concurrency semantics, and therefore cannot be resolved by Node. I.e., an Neither shortcoming negates the utility of the This API is clearly very widely used in the wild. The Node.js maintainers will continue to face inbound requests about this functionality until the stdlib includes it. We wouldn't be having this discussion years later otherwise. This NPM module should not exist: https://www.npmjs.com/package/fs.promises.exists. Continuing to omit this API is at best an act of indifference to the Node.js user base. Moreso it feels like paternalism and casual disregard: "you don't know what's best for you." Why the stiff resistance to doc'ing the limitations and adding the API? |
Hi! If you feel strongly about this, I suggest opening an issue with these key ideas. Maybe it'll gain some good traction :-). Thanks for your input! |
You're welcome! And thank you for trying! |
This pull request introduces an
exists
method tofs.promises
.Rationale: The synchronous
fs
module includes bothexists
andexistsSync
methods, which operate simply by callingaccess
and returning true or false based on success (althoughexistsSync
does it a bit differently). Given this straightforward approach, it seems logical to also provide this functionality infs.promises
. There are no technical limitations preventing this, so adding this method enhances consistency and convenience.