Skip to content
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

perf(resolve): improve file existence check #11436

Merged
merged 6 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(resolve): remove file readable check
  • Loading branch information
bluwy committed Dec 20, 2022
commit 28b6200357073a5fdb3b78e1ddff4c5899dcec12
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ function tryResolveFile(
skipPackageJson?: boolean,
): string | undefined {
const stat = fs.statSync(file, { throwIfNoEntry: false })
Copy link
Contributor

@MoustaphaDev MoustaphaDev Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know vite a lot but this not being in a try catch could be an issue as it would throw an error for virtual modules
See this error an Astro user encountered, seems like caused by this change

if (stat && (stat.mode & fs.constants.S_IRUSR) > 0) {
if (stat) {
if (!stat.isDirectory()) {
return getRealPath(file, options.preserveSymlinks) + postfix
} else if (tryIndex) {
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,12 @@ export function writeFile(
}

export function isFileReadable(filename: string): boolean {
const stat = fs.statSync(filename, { throwIfNoEntry: false })
return stat ? (stat.mode & fs.constants.S_IRUSR) > 0 : false
try {
fs.accessSync(filename, fs.constants.R_OK)
return true
} catch {
return false
}
}

const splitFirstDirRE = /(.+?)[\\/](.+)/
Expand Down