|
| 1 | +import type { Vitest } from '../core' |
| 2 | +import type { TestProject } from '../project' |
| 3 | +import type { TestSpecification } from '../test-specification' |
| 4 | +import { existsSync } from 'node:fs' |
| 5 | +import { mkdir, readFile, rm } from 'node:fs/promises' |
| 6 | +import { cleanUrl } from '@vitest/utils/helpers' |
| 7 | +import { dirname, isAbsolute, resolve } from 'pathe' |
| 8 | +import { createDebugger } from '../../utils/debugger' |
| 9 | +import { tagCacheDir } from './cachedirTag' |
| 10 | +import { atomicWriteFile } from './fsModuleCache' |
| 11 | +import { getFileStamp, getRootRelativePath, isPartialRun } from './results' |
| 12 | + |
| 13 | +const debug = createDebugger('vitest:cache:dependencies') |
| 14 | + |
| 15 | +const DEPENDENCIES_VERSION = 1 |
| 16 | + |
| 17 | +export interface ProjectDependencies { |
| 18 | + /** Absolute paths without a query, referenced by index. */ |
| 19 | + modules: string[] |
| 20 | + moduleIndex: Map<string, number> |
| 21 | + globalSetup?: number[] |
| 22 | + /** Root-relative test file path to the indices of every module it loaded. */ |
| 23 | + files: Map<string, number[]> |
| 24 | +} |
| 25 | + |
| 26 | +interface SerializedDependencies { |
| 27 | + version: number |
| 28 | + projects: Record< |
| 29 | + string, |
| 30 | + { |
| 31 | + modules: string[] |
| 32 | + globalSetup?: number[] |
| 33 | + files: Record<string, number[]> |
| 34 | + } |
| 35 | + > |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Stores the modules that every test file loaded during its last run. |
| 40 | + */ |
| 41 | +export class DependenciesCache { |
| 42 | + private projects = new Map<string, ProjectDependencies>() |
| 43 | + // recorded during the current run, merged into `projects` by `update` |
| 44 | + private recordedFiles = new Map<string, Map<string, string[]>>() |
| 45 | + private recordedGlobalSetup = new Map<string, string[]>() |
| 46 | + private path: string | null |
| 47 | + private fileStamp: string | undefined |
| 48 | + |
| 49 | + constructor(private vitest: Vitest) { |
| 50 | + this.path = |
| 51 | + vitest.config.cache && vitest.config.experimental.recordDependencies |
| 52 | + ? resolve(vitest.viteConfig.cacheDir, 'dependencies.json') |
| 53 | + : null |
| 54 | + } |
| 55 | + |
| 56 | + get(project: TestProject): ProjectDependencies | undefined { |
| 57 | + return this.projects.get(project.name) |
| 58 | + } |
| 59 | + |
| 60 | + record(project: TestProject, filepath: string, dependencies: string[]): void { |
| 61 | + if (!this.path) { |
| 62 | + return |
| 63 | + } |
| 64 | + let files = this.recordedFiles.get(project.name) |
| 65 | + if (!files) { |
| 66 | + this.recordedFiles.set(project.name, (files = new Map())) |
| 67 | + } |
| 68 | + files.set(filepath, this.resolveFiles(project, dependencies)) |
| 69 | + } |
| 70 | + |
| 71 | + recordGlobalSetup(project: TestProject, dependencies: string[]): void { |
| 72 | + if (this.path && dependencies.length) { |
| 73 | + this.recordedGlobalSetup.set(project.name, this.resolveFiles(project, dependencies)) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + async read(): Promise<void> { |
| 78 | + const path = this.path |
| 79 | + const stamp = path ? await getFileStamp(path) : undefined |
| 80 | + if (!path || !stamp || stamp === this.fileStamp) { |
| 81 | + return |
| 82 | + } |
| 83 | + this.fileStamp = stamp |
| 84 | + try { |
| 85 | + const { version, projects }: SerializedDependencies = JSON.parse( |
| 86 | + await readFile(path, 'utf-8'), |
| 87 | + ) |
| 88 | + if (version !== DEPENDENCIES_VERSION) { |
| 89 | + debug?.(`ignored ${path}, the version ${version} is not supported`) |
| 90 | + return |
| 91 | + } |
| 92 | + const root = this.vitest.config.root |
| 93 | + const result = new Map<string, ProjectDependencies>() |
| 94 | + for (const name in projects) { |
| 95 | + const { modules, globalSetup, files } = projects[name] |
| 96 | + const absoluteModules = modules.map((file) => resolve(root, file)) |
| 97 | + result.set(name, { |
| 98 | + modules: absoluteModules, |
| 99 | + moduleIndex: new Map(absoluteModules.map((file, index) => [file, index])), |
| 100 | + globalSetup, |
| 101 | + files: new Map(Object.entries(files)), |
| 102 | + }) |
| 103 | + } |
| 104 | + this.projects = result |
| 105 | + } catch (error) { |
| 106 | + debug?.(`failed to read ${path}: ${error}`) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + async update(specifications: TestSpecification[]): Promise<void> { |
| 111 | + if (!this.recordedFiles.size && !this.recordedGlobalSetup.size) { |
| 112 | + return |
| 113 | + } |
| 114 | + await this.read() |
| 115 | + |
| 116 | + for (const [name, files] of this.recordedGlobalSetup) { |
| 117 | + const project = this.getProjectDependencies(name) |
| 118 | + project.globalSetup = this.intern(project, files) |
| 119 | + } |
| 120 | + |
| 121 | + for (const specification of specifications) { |
| 122 | + const files = this.recordedFiles.get(specification.project.name)?.get(specification.moduleId) |
| 123 | + const task = specification.testModule?.task |
| 124 | + if (!files || !task) { |
| 125 | + continue |
| 126 | + } |
| 127 | + const project = this.getProjectDependencies(specification.project.name) |
| 128 | + const file = getRootRelativePath(this.vitest.config.root, specification.moduleId) |
| 129 | + const previous = project.files.get(file) |
| 130 | + // a partial run skips tests, so it can miss their dynamic imports, |
| 131 | + // without a previous record the file keeps having none and always runs |
| 132 | + if (!isPartialRun(this.vitest, specification, task)) { |
| 133 | + project.files.set(file, this.intern(project, files)) |
| 134 | + } else if (previous) { |
| 135 | + project.files.set(file, Array.from(new Set([...previous, ...this.intern(project, files)]))) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + this.recordedFiles.clear() |
| 140 | + this.recordedGlobalSetup.clear() |
| 141 | + await this.write() |
| 142 | + } |
| 143 | + |
| 144 | + async clear(): Promise<void> { |
| 145 | + this.projects.clear() |
| 146 | + this.fileStamp = undefined |
| 147 | + if (this.path && existsSync(this.path)) { |
| 148 | + await rm(this.path, { force: true }) |
| 149 | + this.vitest.logger.log('[cache] cleared dependencies cache at', this.path) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // the files of the modules, with the files that plugins watch for them |
| 154 | + private resolveFiles(project: TestProject, dependencies: string[]): string[] { |
| 155 | + const files = new Set<string>() |
| 156 | + const add = (file: string) => { |
| 157 | + if (isAbsolute(file) && !file.includes('node_modules')) { |
| 158 | + files.add(file) |
| 159 | + } |
| 160 | + } |
| 161 | + // without the module runner, plugins do not transform the modules and cannot watch files |
| 162 | + const watchedByPlugins = project.config.experimental.viteModuleRunner !== false |
| 163 | + for (const id of dependencies) { |
| 164 | + add(cleanUrl(id)) |
| 165 | + if (!watchedByPlugins) { |
| 166 | + continue |
| 167 | + } |
| 168 | + for (const environment of Object.values(project.vite.environments)) { |
| 169 | + const node = environment.moduleGraph.getModuleById(id) |
| 170 | + const transformed = node?.transformResult |
| 171 | + if (!transformed) { |
| 172 | + continue |
| 173 | + } |
| 174 | + const imports = new Set([...(transformed.deps || []), ...(transformed.dynamicDeps || [])]) |
| 175 | + node.importedModules.forEach((imported) => { |
| 176 | + if (imported.file && !imports.has(imported.url)) { |
| 177 | + add(imported.file) |
| 178 | + } |
| 179 | + }) |
| 180 | + break |
| 181 | + } |
| 182 | + } |
| 183 | + return Array.from(files) |
| 184 | + } |
| 185 | + |
| 186 | + private intern(project: ProjectDependencies, files: string[]): number[] { |
| 187 | + return files.map((file) => { |
| 188 | + let index = project.moduleIndex.get(file) |
| 189 | + if (index === undefined) { |
| 190 | + index = project.modules.push(file) - 1 |
| 191 | + project.moduleIndex.set(file, index) |
| 192 | + } |
| 193 | + return index |
| 194 | + }) |
| 195 | + } |
| 196 | + |
| 197 | + private getProjectDependencies(name: string): ProjectDependencies { |
| 198 | + let project = this.projects.get(name) |
| 199 | + if (!project) { |
| 200 | + project = { modules: [], moduleIndex: new Map(), files: new Map() } |
| 201 | + this.projects.set(name, project) |
| 202 | + } |
| 203 | + return project |
| 204 | + } |
| 205 | + |
| 206 | + private async write(): Promise<void> { |
| 207 | + if (!this.path) { |
| 208 | + return |
| 209 | + } |
| 210 | + const root = this.vitest.config.root |
| 211 | + const projects: SerializedDependencies['projects'] = Object.create(null) |
| 212 | + for (const [name, { modules, globalSetup, files }] of this.projects) { |
| 213 | + projects[name] = { |
| 214 | + modules: modules.map((file) => getRootRelativePath(root, file)), |
| 215 | + globalSetup, |
| 216 | + files: Object.fromEntries(files), |
| 217 | + } |
| 218 | + } |
| 219 | + const dependencies: SerializedDependencies = { version: DEPENDENCIES_VERSION, projects } |
| 220 | + try { |
| 221 | + const cacheDir = dirname(this.path) |
| 222 | + await mkdir(cacheDir, { recursive: true }) |
| 223 | + tagCacheDir(dirname(cacheDir)) |
| 224 | + await atomicWriteFile(this.path, JSON.stringify(dependencies)) |
| 225 | + this.fileStamp = await getFileStamp(this.path) |
| 226 | + } catch (error) { |
| 227 | + debug?.(`failed to write ${this.path}: ${error}`) |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments