From 4fe971fda39ab1a323461c09b35108cc7a271484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Tue, 20 Feb 2024 22:05:07 +0900 Subject: [PATCH] fix: resolve directory correctly when `fs.cachedChecks: true` (#15983) --- packages/vite/src/node/fsUtils.ts | 4 ++++ playground/resolve/__tests__/resolve.spec.ts | 4 ++++ playground/resolve/index.html | 6 ++++++ playground/resolve/non-normalized.js | 1 + playground/resolve/vite.config.js | 7 +++++++ 5 files changed, 22 insertions(+) create mode 100644 playground/resolve/non-normalized.js diff --git a/packages/vite/src/node/fsUtils.ts b/packages/vite/src/node/fsUtils.ts index ae1892e989677c..9536218c8f8ce4 100644 --- a/packages/vite/src/node/fsUtils.ts +++ b/packages/vite/src/node/fsUtils.ts @@ -189,6 +189,10 @@ export function createCachedFsUtils(config: ResolvedConfig): FsUtils { function getDirentCacheFromPath( normalizedFile: string, ): DirentCache | false | undefined { + // path.posix.normalize may return a path either with / or without / + if (normalizedFile[normalizedFile.length - 1] === '/') { + normalizedFile = normalizedFile.slice(0, -1) + } if (normalizedFile === root) { return rootCache } diff --git a/playground/resolve/__tests__/resolve.spec.ts b/playground/resolve/__tests__/resolve.spec.ts index 45b87c23e4ab10..d930ac1e49a2df 100644 --- a/playground/resolve/__tests__/resolve.spec.ts +++ b/playground/resolve/__tests__/resolve.spec.ts @@ -215,6 +215,10 @@ test('Resolve doesnt interrupt page request with trailing query and .css', async expect(await page.textContent('h1')).toBe('Resolve') }) +test('resolve non-normalized absolute path', async () => { + expect(await page.textContent('.non-normalized')).toMatch('[success]') +}) + test.runIf(!isWindows)( 'Resolve doesnt interrupt page request that clashes with local project package.json', async () => { diff --git a/playground/resolve/index.html b/playground/resolve/index.html index 89eced2e7149e9..fa6b5afbb84cd6 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -167,6 +167,9 @@

resolve.conditions

resolve package that contains # in path

+

resolve non normalized absolute path

+

+