Skip to content

Commit 9c044db

Browse files
authored
fix(cache): watch cached files outside the root (#11860)
1 parent 5f72c3d commit 9c044db

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ export class FileSystemModuleCache {
241241
this.vitest.coverageProvider?.restoreModuleCacheData?.(environment, id, data)
242242
}
243243

244+
// Vite watches the files outside of the root when it loads them, a cached module skips that
245+
watchFile(file: string): void {
246+
const server = this.vitest.vite
247+
if (!file.startsWith(`${server.config.root}/`) && existsSync(file)) {
248+
server.watcher.add(file)
249+
}
250+
}
251+
244252
private toBase64(obj: unknown) {
245253
const json = stringify(obj)
246254
return Buffer.from(json).toString('base64')

‎packages/vitest/src/node/environments/transformService.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ export class ModuleTransformService {
375375
return
376376
}
377377

378+
if (cachedModule.file) {
379+
this.fsCache.watchFile(cachedModule.file)
380+
}
381+
378382
// Notify coverage provider that cached results were read
379383
if (cachedModule.hasCoverageData) {
380384
this.fsCache.restoreCoverageData(environment, cachedModule.id, cachedModule.coverageData)

‎test/e2e/test/watch/file-watching.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,48 @@ export default {
179179
await vitest.waitForStdout('1 passed')
180180
})
181181

182+
test('editing a cached source file outside the root triggers re-run', async () => {
183+
const fs = testUtils.useTmpFS(
184+
{
185+
'app/package.json': JSON.stringify({ name: 'app', type: 'module' }),
186+
// keeps the workspace root at app/
187+
'app/pnpm-workspace.yaml': '',
188+
'app/vitest.config.mjs': /* js */ `
189+
export default {
190+
test: {
191+
fsModuleCache: true,
192+
fsModuleCachePath: './node_modules/.vitest-fs-cache',
193+
},
194+
}
195+
`,
196+
'app/test/uses-lib.test.ts': /* ts */ `
197+
import { expect, test } from 'vitest'
198+
import { helper } from '../../lib/src/helper.ts'
199+
200+
test('uses lib', () => {
201+
expect(helper()).toBe(1)
202+
})
203+
`,
204+
'lib/src/helper.ts': `export const helper = () => 1\n`,
205+
},
206+
false,
207+
)
208+
const root = resolve(fs.root, 'app')
209+
210+
// the first run writes the cache, so the watched run reads the helper from it
211+
const cold = await testUtils.runVitest({ root })
212+
expect(cold.errorTree()).toEqual({ 'test/uses-lib.test.ts': { 'uses lib': 'passed' } })
213+
await cold.ctx?.close()
214+
215+
const { vitest } = await testUtils.runVitest({ root, watch: true })
216+
217+
fs.editFile('lib/src/helper.ts', modifyContent)
218+
219+
await vitest.waitForStdout('New code running')
220+
await vitest.waitForStdout('RERUN ../../../lib/src/helper.ts')
221+
await vitest.waitForStdout('1 passed')
222+
})
223+
182224
test('editing config file reloads new changes', async () => {
183225
const { vitest, fs } = await testUtils.runInlineTests(
184226
{

0 commit comments

Comments
 (0)