Skip to content

Commit c599a2e

Browse files
authoredNov 7, 2022
fix: prevent cache on optional package resolve (#10812)
1 parent 7a18b2e commit c599a2e

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed
 

‎packages/vite/src/node/utils.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -951,12 +951,17 @@ export const requireResolveFromRootWithFallback = (
951951
root: string,
952952
id: string
953953
): string => {
954+
const paths = _require.resolve.paths?.(id) || []
954955
// Search in the root directory first, and fallback to the default require paths.
955-
const fallbackPaths = _require.resolve.paths?.(id) || []
956-
const path = _require.resolve(id, {
957-
paths: [root, ...fallbackPaths]
958-
})
959-
return path
956+
paths.unshift(root)
957+
958+
// Use `resolve` package to check existence first, so if the package is not found,
959+
// it won't be cached by nodejs, since there isn't a way to invalidate them:
960+
// https://github.com/nodejs/node/issues/44663
961+
resolve.sync(id, { basedir: root, paths })
962+
963+
// Use `require.resolve` again as the `resolve` package doesn't support the `exports` field
964+
return _require.resolve(id, { paths })
960965
}
961966

962967
// Based on node-graceful-fs

0 commit comments

Comments
 (0)
Please sign in to comment.