-
-
Notifications
You must be signed in to change notification settings - Fork 594
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
Fix bug where fs.exists is incorrectly promisified #835
Conversation
7e3b978
to
0b912c3
Compare
export const readFile = promisify(fs.readFile); | ||
export const realpath = promisify(fs.realpath); | ||
export { realpathSync } from 'fs'; | ||
export const stat = promisify(fs.stat); | ||
export async function exists(filePath) { |
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.
Since fs.exists
is just a wrapper around fs.access
you should use that to make sure the behaviour is the same and to not unnecessarily create the stat object just to throw it away
packages/node-resolve/src/fs.js
Outdated
export const readFile = promisify(fs.readFile); | ||
export const realpath = promisify(fs.realpath); | ||
export { realpathSync } from 'fs'; | ||
export const stat = promisify(fs.stat); | ||
export async function exists(filePath) { | ||
try { | ||
await stat(filePath); |
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 is "access" function to check file existence.
Also looks like already promisified version are available in node 10 |
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.
Using stat (or even better, access
) makes more sense anyway, as exists
is kind of deprecated in node. It's recommended to use a more informative method that tells what the thing is (file/folder/other), if it's accessible to you, etc.
0b912c3
to
4b58114
Compare
Swapped |
Rollup Plugin Name: node-resolve
This PR contains:
Are tests included?
Breaking Changes?
If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.
List any relevant issue numbers:
Description
Using the node-resolve plugin in a browser where the fs module is mocked fails to find resolve any paths. Upon debugging this issue, it appears to be because
fs.exists
is passed toutil.promisify
. The Node docs explicitly call out this function as being non-standard and not following the pattern forutil.promisify
to work correctly.As far as I can tell, this works on Node because
fs.exists
actually internally already provides a promisified version via: https://nodejs.org/api/util.html#util_custom_promisified_functionsUsing stat, which doesn't have this non-standard-ness, works around this issue.
I haven't included tests because this fixes a bug when running in an admittedly quite strange setup that's currently not replicated in tests at all as far as I know.