From ca76f4576d1fbfb82f9131fbefc0e3cabaea4902 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 11 Jan 2024 18:27:01 +0100 Subject: [PATCH] fix(vitest): use file instead of id for HMR (#4938) --- packages/vitest/src/node/core.ts | 51 ++++++++++++++------------------ 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index a5a82926346f..8b498b3a61ab 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -5,7 +5,6 @@ import { basename, dirname, join, normalize, relative, resolve } from 'pathe' import fg from 'fast-glob' import mm from 'micromatch' import c from 'picocolors' -import { normalizeRequestId } from 'vite-node/utils' import { ViteNodeRunner } from 'vite-node/client' import { SnapshotManager } from '@vitest/snapshot/manager' import type { CancelReason, File } from '@vitest/runner' @@ -646,23 +645,23 @@ export class Vitest { }, WATCHER_DEBOUNCE) } - public getModuleProjects(id: string) { + public getModuleProjects(filepath: string) { return this.projects.filter((project) => { - return project.getModulesByFilepath(id).size + return project.getModulesByFilepath(filepath).size // TODO: reevaluate || project.browser?.moduleGraph.getModulesByFile(id)?.size }) } private unregisterWatcher = noop private registerWatcher() { - const updateLastChanged = (id: string) => { - const projects = this.getModuleProjects(id) + const updateLastChanged = (filepath: string) => { + const projects = this.getModuleProjects(filepath) projects.forEach(({ server, browser }) => { - const serverMods = server.moduleGraph.getModulesByFile(id) + const serverMods = server.moduleGraph.getModulesByFile(filepath) serverMods?.forEach(mod => server.moduleGraph.invalidateModule(mod)) if (browser) { - const browserMods = browser.moduleGraph.getModulesByFile(id) + const browserMods = browser.moduleGraph.getModulesByFile(filepath) browserMods?.forEach(mod => browser.moduleGraph.invalidateModule(mod)) } }) @@ -725,22 +724,22 @@ export class Vitest { /** * @returns A value indicating whether rerun is needed (changedTests was mutated) */ - private handleFileChanged(id: string): string[] { - if (this.changedTests.has(id) || this.invalidates.has(id)) + private handleFileChanged(filepath: string): string[] { + if (this.changedTests.has(filepath) || this.invalidates.has(filepath)) return [] - if (mm.isMatch(id, this.config.forceRerunTriggers)) { + if (mm.isMatch(filepath, this.config.forceRerunTriggers)) { this.state.getFilepaths().forEach(file => this.changedTests.add(file)) - return [id] + return [filepath] } - const projects = this.getModuleProjects(id) + const projects = this.getModuleProjects(filepath) if (!projects.length) { // if there are no modules it's possible that server was restarted // we don't have information about importers anymore, so let's check if the file is a test file at least - if (this.state.filesMap.has(id) || this.projects.some(project => project.isTestFile(id))) { - this.changedTests.add(id) - return [id] + if (this.state.filesMap.has(filepath) || this.projects.some(project => project.isTestFile(filepath))) { + this.changedTests.add(filepath) + return [filepath] } return [] } @@ -748,39 +747,33 @@ export class Vitest { const files: string[] = [] for (const project of projects) { - const { server } = project - const mods = project.getModulesByFilepath(id) + const mods = project.getModulesByFilepath(filepath) if (!mods.size) continue - // remove queries from id - id = normalizeRequestId(id, server.config.base) - - this.invalidates.add(id) + this.invalidates.add(filepath) // one of test files that we already run, or one of test files that we can run - if (this.state.filesMap.has(id) || project.isTestFile(id)) { - this.changedTests.add(id) - files.push(id) + if (this.state.filesMap.has(filepath) || project.isTestFile(filepath)) { + this.changedTests.add(filepath) + files.push(filepath) continue } let rerun = false for (const mod of mods) { - if (!mod.id) - continue mod.importers.forEach((i) => { - if (!i.id) + if (!i.file) return - const heedsRerun = this.handleFileChanged(i.id) + const heedsRerun = this.handleFileChanged(i.file) if (heedsRerun) rerun = true }) } if (rerun) - files.push(id) + files.push(filepath) } return files