From aed74c6ef664fa49a35c043a4dcde60b98dc334c Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 7 Nov 2022 17:48:04 +0800 Subject: [PATCH 1/3] fix: prevent cache on optional package resolve --- packages/vite/src/node/utils.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index c0bc0646f58336..dacef31504db31 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -951,12 +951,23 @@ 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 + if (!resolve.sync(id, { paths })) { + const err: any = new Error( + `Cannot find module '${root}' imported from '${root}'` + ) + err.code = 'ERR_MODULE_NOT_FOUND' + throw new err() + } + + // Use `require.resolve` again as the `resolve` package doesn't support the `exports` field + return _require.resolve(id, { paths }) } // Based on node-graceful-fs From dba6e6c2bdcda193a4bcfe4469934a2ad6a8e504 Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 7 Nov 2022 20:26:53 +0800 Subject: [PATCH 2/3] chore: fix ci --- packages/vite/src/node/utils.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index dacef31504db31..7110c5244e83a9 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -3,7 +3,7 @@ import os from 'node:os' import path from 'node:path' import { createHash } from 'node:crypto' import { promisify } from 'node:util' -import { URL, URLSearchParams } from 'node:url' +import { URL, URLSearchParams, fileURLToPath } from 'node:url' import { builtinModules, createRequire } from 'node:module' import { promises as dns } from 'node:dns' import { performance } from 'node:perf_hooks' @@ -958,13 +958,7 @@ export const requireResolveFromRootWithFallback = ( // 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 - if (!resolve.sync(id, { paths })) { - const err: any = new Error( - `Cannot find module '${root}' imported from '${root}'` - ) - err.code = 'ERR_MODULE_NOT_FOUND' - throw new err() - } + 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 }) From 14d9318628b80d1b67936a6aba9722dc0499bc44 Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 7 Nov 2022 20:31:49 +0800 Subject: [PATCH 3/3] chore: fix ci again --- packages/vite/src/node/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 7110c5244e83a9..23c7597bb77195 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -3,7 +3,7 @@ import os from 'node:os' import path from 'node:path' import { createHash } from 'node:crypto' import { promisify } from 'node:util' -import { URL, URLSearchParams, fileURLToPath } from 'node:url' +import { URL, URLSearchParams } from 'node:url' import { builtinModules, createRequire } from 'node:module' import { promises as dns } from 'node:dns' import { performance } from 'node:perf_hooks'