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

fix: prevent cache on optional package resolve #10812

Merged
merged 3 commits into from Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/vite/src/node/utils.ts
Expand Up @@ -951,12 +951,17 @@ export const requireResolveFromRootWithFallback = (
root: string,
id: string
): string => {
const paths = _require.resolve.paths?.(id) || []
// Search in the root directory first, and fallback to the default require paths.
const fallbackPaths = _require.resolve.paths?.(id) || []
const path = _require.resolve(id, {
paths: [root, ...fallbackPaths]
})
return path
paths.unshift(root)

// Use `resolve` package to check existence first, so if the package is not found,
// it won't be cached by nodejs, since there isn't a way to invalidate them:
// https://github.com/nodejs/node/issues/44663
resolve.sync(id, { basedir: root, paths })

// Use `require.resolve` again as the `resolve` package doesn't support the `exports` field
return _require.resolve(id, { paths })
}

// Based on node-graceful-fs
Expand Down