Skip to content

Commit 4fe971f

Browse files
authored
fix: resolve directory correctly when fs.cachedChecks: true (#15983)
1 parent 7b9e927 commit 4fe971f

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

packages/vite/src/node/fsUtils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ export function createCachedFsUtils(config: ResolvedConfig): FsUtils {
189189
function getDirentCacheFromPath(
190190
normalizedFile: string,
191191
): DirentCache | false | undefined {
192+
// path.posix.normalize may return a path either with / or without /
193+
if (normalizedFile[normalizedFile.length - 1] === '/') {
194+
normalizedFile = normalizedFile.slice(0, -1)
195+
}
192196
if (normalizedFile === root) {
193197
return rootCache
194198
}

playground/resolve/__tests__/resolve.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ test('Resolve doesnt interrupt page request with trailing query and .css', async
215215
expect(await page.textContent('h1')).toBe('Resolve')
216216
})
217217

218+
test('resolve non-normalized absolute path', async () => {
219+
expect(await page.textContent('.non-normalized')).toMatch('[success]')
220+
})
221+
218222
test.runIf(!isWindows)(
219223
'Resolve doesnt interrupt page request that clashes with local project package.json',
220224
async () => {

playground/resolve/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ <h2>resolve.conditions</h2>
167167
<h2>resolve package that contains # in path</h2>
168168
<p class="path-contains-sharp-symbol"></p>
169169

170+
<h2>resolve non normalized absolute path</h2>
171+
<p class="non-normalized"></p>
172+
170173
<script type="module">
171174
import '@generated-content-virtual-file'
172175
function text(selector, text) {
@@ -384,6 +387,9 @@ <h2>resolve package that contains # in path</h2>
384387
'.path-contains-sharp-symbol',
385388
`[success] ${contains.call('#', '#')} ${last.call('#')}`,
386389
)
390+
391+
import nonNormalizedAbsolute from '@non-normalized'
392+
text('.non-normalized', nonNormalizedAbsolute)
387393
</script>
388394

389395
<style>

playground/resolve/non-normalized.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default '[success] non normalized absolute path'

playground/resolve/vite.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ export default defineConfig({
9999
}
100100
},
101101
},
102+
{
103+
name: 'resolve to non normalized absolute',
104+
async resolveId(id) {
105+
if (id !== '@non-normalized') return
106+
return this.resolve(__dirname + '//non-normalized')
107+
},
108+
},
102109
],
103110
optimizeDeps: {
104111
include: [

0 commit comments

Comments
 (0)