Skip to content

Commit ca76f45

Browse files
authoredJan 11, 2024
fix(vitest): use file instead of id for HMR (#4938)
1 parent 7fd44dc commit ca76f45

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed
 

‎packages/vitest/src/node/core.ts

+22-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { basename, dirname, join, normalize, relative, resolve } from 'pathe'
55
import fg from 'fast-glob'
66
import mm from 'micromatch'
77
import c from 'picocolors'
8-
import { normalizeRequestId } from 'vite-node/utils'
98
import { ViteNodeRunner } from 'vite-node/client'
109
import { SnapshotManager } from '@vitest/snapshot/manager'
1110
import type { CancelReason, File } from '@vitest/runner'
@@ -646,23 +645,23 @@ export class Vitest {
646645
}, WATCHER_DEBOUNCE)
647646
}
648647

649-
public getModuleProjects(id: string) {
648+
public getModuleProjects(filepath: string) {
650649
return this.projects.filter((project) => {
651-
return project.getModulesByFilepath(id).size
650+
return project.getModulesByFilepath(filepath).size
652651
// TODO: reevaluate || project.browser?.moduleGraph.getModulesByFile(id)?.size
653652
})
654653
}
655654

656655
private unregisterWatcher = noop
657656
private registerWatcher() {
658-
const updateLastChanged = (id: string) => {
659-
const projects = this.getModuleProjects(id)
657+
const updateLastChanged = (filepath: string) => {
658+
const projects = this.getModuleProjects(filepath)
660659
projects.forEach(({ server, browser }) => {
661-
const serverMods = server.moduleGraph.getModulesByFile(id)
660+
const serverMods = server.moduleGraph.getModulesByFile(filepath)
662661
serverMods?.forEach(mod => server.moduleGraph.invalidateModule(mod))
663662

664663
if (browser) {
665-
const browserMods = browser.moduleGraph.getModulesByFile(id)
664+
const browserMods = browser.moduleGraph.getModulesByFile(filepath)
666665
browserMods?.forEach(mod => browser.moduleGraph.invalidateModule(mod))
667666
}
668667
})
@@ -725,62 +724,56 @@ export class Vitest {
725724
/**
726725
* @returns A value indicating whether rerun is needed (changedTests was mutated)
727726
*/
728-
private handleFileChanged(id: string): string[] {
729-
if (this.changedTests.has(id) || this.invalidates.has(id))
727+
private handleFileChanged(filepath: string): string[] {
728+
if (this.changedTests.has(filepath) || this.invalidates.has(filepath))
730729
return []
731730

732-
if (mm.isMatch(id, this.config.forceRerunTriggers)) {
731+
if (mm.isMatch(filepath, this.config.forceRerunTriggers)) {
733732
this.state.getFilepaths().forEach(file => this.changedTests.add(file))
734-
return [id]
733+
return [filepath]
735734
}
736735

737-
const projects = this.getModuleProjects(id)
736+
const projects = this.getModuleProjects(filepath)
738737
if (!projects.length) {
739738
// if there are no modules it's possible that server was restarted
740739
// we don't have information about importers anymore, so let's check if the file is a test file at least
741-
if (this.state.filesMap.has(id) || this.projects.some(project => project.isTestFile(id))) {
742-
this.changedTests.add(id)
743-
return [id]
740+
if (this.state.filesMap.has(filepath) || this.projects.some(project => project.isTestFile(filepath))) {
741+
this.changedTests.add(filepath)
742+
return [filepath]
744743
}
745744
return []
746745
}
747746

748747
const files: string[] = []
749748

750749
for (const project of projects) {
751-
const { server } = project
752-
const mods = project.getModulesByFilepath(id)
750+
const mods = project.getModulesByFilepath(filepath)
753751
if (!mods.size)
754752
continue
755753

756-
// remove queries from id
757-
id = normalizeRequestId(id, server.config.base)
758-
759-
this.invalidates.add(id)
754+
this.invalidates.add(filepath)
760755

761756
// one of test files that we already run, or one of test files that we can run
762-
if (this.state.filesMap.has(id) || project.isTestFile(id)) {
763-
this.changedTests.add(id)
764-
files.push(id)
757+
if (this.state.filesMap.has(filepath) || project.isTestFile(filepath)) {
758+
this.changedTests.add(filepath)
759+
files.push(filepath)
765760
continue
766761
}
767762

768763
let rerun = false
769764
for (const mod of mods) {
770-
if (!mod.id)
771-
continue
772765
mod.importers.forEach((i) => {
773-
if (!i.id)
766+
if (!i.file)
774767
return
775768

776-
const heedsRerun = this.handleFileChanged(i.id)
769+
const heedsRerun = this.handleFileChanged(i.file)
777770
if (heedsRerun)
778771
rerun = true
779772
})
780773
}
781774

782775
if (rerun)
783-
files.push(id)
776+
files.push(filepath)
784777
}
785778

786779
return files

0 commit comments

Comments
 (0)
Please sign in to comment.