Skip to content

Commit 79501f3

Browse files
authored
fix: skip fs module cache for typed import.meta.glob (#11666)
1 parent 973dc84 commit 79501f3

2 files changed

Lines changed: 190 additions & 3 deletions

File tree

‎packages/vitest/src/node/cache/fsModuleCache.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ export class FileSystemModuleCache {
219219
id: string,
220220
fileContent: string,
221221
): string | null {
222-
// bail out if file has import.meta.glob because it depends on other files
222+
// bail out if file has a glob import because it depends on other files
223223
// TODO: figure out a way to still support it
224-
if (fileContent.includes('import.meta.glob(')) {
224+
if (hasGlobImport(fileContent, environment.config.consumer)) {
225225
this.saveMemoryCache(environment, id, null)
226-
debugMemory?.(`${c.yellow('[write]')} ${id} was bailed out because it has "import.meta.glob"`)
226+
debugMemory?.(`${c.yellow('[write]')} ${id} was bailed out because it has a glob import`)
227227
return null
228228
}
229229

@@ -536,6 +536,22 @@ function lookupFile(dir: string, fileNames: string[]): string | undefined {
536536
}
537537
}
538538

539+
// Vite expands these templates into `import.meta.glob` calls
540+
const dynamicImportTemplateRE = /\bimport\s*\(\s*`[^`]*\$\{/
541+
const newUrlTemplateRE = /\bnew\s+URL\s*\(\s*`[^`]*\$\{[^`]*`\s*,\s*import\.meta\.url\b/
542+
543+
function hasGlobImport(code: string, consumer: 'client' | 'server'): boolean {
544+
if (code.includes('import.meta.glob')) {
545+
return true
546+
}
547+
if (!code.includes('${')) {
548+
return false
549+
}
550+
return (
551+
dynamicImportTemplateRE.test(code) || (consumer === 'client' && newUrlTemplateRE.test(code))
552+
)
553+
}
554+
539555
function tryReadFileSync(file: string): string {
540556
try {
541557
return readFileSync(file, 'utf-8')

‎test/e2e/test/caching.test.ts‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,176 @@ test("if file has import.meta.glob, it's not cached", async () => {
4848
`)
4949
})
5050

51+
test("if file has import.meta.glob with a type argument, it's not cached", async () => {
52+
const structure = {
53+
'src/a.js': 'export default 1',
54+
'src/b.js': 'export default 2',
55+
'glob.test.ts': /* ts */ `
56+
import { expect, inject, test } from 'vitest'
57+
58+
const files = import.meta.glob<{ default: number }>('./src/*.js', { eager: true })
59+
60+
test('glob', () => {
61+
expect(Object.keys(files)).toEqual(inject('files'))
62+
})
63+
`,
64+
}
65+
const config = {
66+
fsModuleCache: true,
67+
fsModuleCachePath: './node_modules/.vitest-fs-cache',
68+
}
69+
70+
const cold = await runInlineTests(structure, {
71+
...config,
72+
provide: { files: ['./src/a.js', './src/b.js'] },
73+
})
74+
75+
expect(cold.stderr).toBe('')
76+
expect(cold.errorTree()).toMatchInlineSnapshot(`
77+
{
78+
"glob.test.ts": {
79+
"glob": "passed",
80+
},
81+
}
82+
`)
83+
await cold.ctx?.close()
84+
85+
cold.fs.createFile('src/c.js', 'export default 3')
86+
87+
const warm = await runVitest({
88+
root: cold.root,
89+
...config,
90+
provide: { files: ['./src/a.js', './src/b.js', './src/c.js'] },
91+
})
92+
93+
expect(warm.stderr).toBe('')
94+
expect(warm.errorTree()).toMatchInlineSnapshot(`
95+
{
96+
"glob.test.ts": {
97+
"glob": "passed",
98+
},
99+
}
100+
`)
101+
})
102+
103+
test("if file has a dynamic import with a template literal, it's not cached", async () => {
104+
const structure = {
105+
'src/a.js': `export default 'a'`,
106+
'src/b.js': `export default 'b'`,
107+
'load.test.js': /* js */ `
108+
import { expect, inject, test } from 'vitest'
109+
110+
const load = (name) => import(\`./src/\${name}.js\`)
111+
112+
test('load', async () => {
113+
const names = inject('names')
114+
const modules = await Promise.all(names.map(load))
115+
expect(modules.map((m) => m.default)).toEqual(names)
116+
})
117+
`,
118+
}
119+
const config = {
120+
fsModuleCache: true,
121+
fsModuleCachePath: './node_modules/.vitest-fs-cache',
122+
}
123+
124+
const cold = await runInlineTests(structure, {
125+
...config,
126+
provide: { names: ['a', 'b'] },
127+
})
128+
129+
expect(cold.stderr).toBe('')
130+
expect(cold.errorTree()).toMatchInlineSnapshot(`
131+
{
132+
"load.test.js": {
133+
"load": "passed",
134+
},
135+
}
136+
`)
137+
await cold.ctx?.close()
138+
139+
cold.fs.createFile('src/c.js', `export default 'c'`)
140+
141+
const warm = await runVitest({
142+
root: cold.root,
143+
...config,
144+
provide: { names: ['a', 'b', 'c'] },
145+
})
146+
147+
expect(warm.stderr).toBe('')
148+
expect(warm.errorTree()).toMatchInlineSnapshot(`
149+
{
150+
"load.test.js": {
151+
"load": "passed",
152+
},
153+
}
154+
`)
155+
})
156+
157+
test("if file has new URL() with a template literal, it's not cached", async () => {
158+
const structure = {
159+
'assets/a.png': '',
160+
'assets/b.png': '',
161+
'client-env.js': /* js */ `
162+
export default {
163+
name: 'client-env',
164+
viteEnvironment: 'client',
165+
setup() {
166+
return { teardown() {} }
167+
},
168+
}
169+
`,
170+
'url.test.js': /* js */ `
171+
import { expect, inject, test } from 'vitest'
172+
173+
const getUrl = (name) => new URL(\`./assets/\${name}.png\`, import.meta.url)
174+
175+
test('url', () => {
176+
const names = inject('names')
177+
const files = names.map((name) => getUrl(name).pathname.split('/').pop())
178+
expect(files).toEqual(names.map((name) => \`\${name}.png\`))
179+
})
180+
`,
181+
}
182+
const config = {
183+
environment: './client-env.js',
184+
fsModuleCache: true,
185+
fsModuleCachePath: './node_modules/.vitest-fs-cache',
186+
}
187+
188+
const cold = await runInlineTests(structure, {
189+
...config,
190+
provide: { names: ['a', 'b'] },
191+
})
192+
193+
expect(cold.stderr).toBe('')
194+
expect(cold.errorTree()).toMatchInlineSnapshot(`
195+
{
196+
"url.test.js": {
197+
"url": "passed",
198+
},
199+
}
200+
`)
201+
await cold.ctx?.close()
202+
203+
cold.fs.createFile('assets/c.png', '')
204+
205+
const warm = await runVitest({
206+
root: cold.root,
207+
...config,
208+
provide: { names: ['a', 'b', 'c'] },
209+
})
210+
211+
expect(warm.stderr).toBe('')
212+
expect(warm.errorTree()).toMatchInlineSnapshot(`
213+
{
214+
"url.test.js": {
215+
"url": "passed",
216+
},
217+
}
218+
`)
219+
})
220+
51221
test('if no cache key generator is defined, the hash is invalid', async () => {
52222
process.env.REPLACED = 'value1'
53223

@@ -409,5 +579,6 @@ test('if cache key generator bails out, the file is not cached', async () => {
409579
declare module 'vitest' {
410580
export interface ProvidedContext {
411581
generated: string[]
582+
names: string[]
412583
}
413584
}

0 commit comments

Comments
 (0)