coverage-partitions.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /** Coordinate single-worker Vitest coverage partitions and one merged report. */
  2. import { spawn } from 'node:child_process'
  3. import { globSync, readFileSync, writeFileSync } from 'node:fs'
  4. import { lstat, mkdir, readdir, rm, unlink, writeFile } from 'node:fs/promises'
  5. import { join, relative, sep } from 'node:path'
  6. import { coverageExemptHeavySuites } from './coverage-exempt.ts'
  7. import { pnpmInvocation } from './pnpm-invocation.ts'
  8. /** Environment variable selecting the number of instrumented coverage processes. */
  9. export const COVERAGE_PARTITIONS_ENV = 'DSH_COVERAGE_PARTITIONS'
  10. /** Internal marker that suppresses reports and thresholds inside a partition process. */
  11. export const COVERAGE_PARTITION_MODE_ENV = 'DSH_COVERAGE_PARTITION_MODE'
  12. /** Environment variable overriding instrumented test and polling timeouts. */
  13. export const COVERAGE_TEST_TIMEOUT_ENV = 'DSH_COVERAGE_TEST_TIMEOUT_MS'
  14. /** One child command owned by the coverage coordinator. */
  15. export interface CoverageCommand {
  16. /** Diagnostic identity. */
  17. label: string
  18. /** Executable launched without a platform shell. */
  19. command: string
  20. /** Arguments passed to the executable. */
  21. args: string[]
  22. /** Environment additions for the child. */
  23. env: Record<string, string | undefined>
  24. /** Working directory for the child. */
  25. cwd: string
  26. /** Blob the partition must produce; absent for the merge command. */
  27. blobPath?: string
  28. }
  29. /** Observable child-process completion. */
  30. export interface CoverageCommandResult {
  31. /** Numeric process status, or `null` when a signal ended the child. */
  32. exitCode: number | null
  33. /** Terminating signal, or `null` after an ordinary exit. */
  34. signalCode: NodeJS.Signals | null
  35. /** Spawn failure recorded independently from process completion. */
  36. error?: string
  37. /** Bounded combined stdout/stderr tail repeated when the command fails. */
  38. outputTail?: string
  39. }
  40. /** Execute one coordinator command with inherited output. */
  41. export type CoverageCommandRunner = (command: CoverageCommand) => Promise<CoverageCommandResult>
  42. /** Construction inputs for {@link CoveragePartitionCoordinator}. */
  43. export interface CoveragePartitionCoordinatorOptions {
  44. /** Repository root that owns coverage output. */
  45. root: string
  46. /** Number of concurrent single-worker Vitest processes. */
  47. partitions: number
  48. /** pnpm JavaScript or executable entrypoint from `npm_execpath`. */
  49. pnpmEntrypoint: string
  50. /** Additional arguments shared by every partition. */
  51. vitestArgs?: string[]
  52. /** Child executor, injectable for scheduler tests. */
  53. runCommand?: CoverageCommandRunner
  54. /** Instrumented inventory; collected from the workspace when absent or empty. */
  55. files?: readonly string[]
  56. /** Recorded durations paired with `files`; read from persistence when absent. */
  57. weights?: ReadonlyMap<string, number>
  58. /** Project ownership paired with `files`; collected from `vitest list` when absent. */
  59. projectOf?: ReadonlyMap<string, string>
  60. }
  61. /** Parse an optional coverage partition count. */
  62. export function parseCoveragePartitionCount(raw: string | undefined): number | undefined {
  63. if (raw === undefined || raw === '') return undefined
  64. const parsed = Number.parseInt(raw, 10)
  65. if (!Number.isSafeInteger(parsed) || parsed < 2 || String(parsed) !== raw) {
  66. throw new Error(`${COVERAGE_PARTITIONS_ENV} must be an integer greater than 1, got ${JSON.stringify(raw)}.`)
  67. }
  68. return parsed
  69. }
  70. /** Resolve the paired Vitest timeout arguments used by coverage partitions. */
  71. export function coverageTestTimeoutArgs(raw: string | undefined): string[] {
  72. if (raw === undefined || raw === '') return []
  73. const parsed = Number.parseInt(raw, 10)
  74. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  75. throw new Error(`${COVERAGE_TEST_TIMEOUT_ENV} must be a positive integer, got ${JSON.stringify(raw)}.`)
  76. }
  77. return [`--testTimeout=${raw}`, `--expect.poll.timeout=${raw}`]
  78. }
  79. /** Remove pnpm's package-script separator before forwarding Vitest arguments. */
  80. export function forwardedCoverageArgs(args: readonly string[]): string[] {
  81. return [...args.slice(args[0] === '--' ? 1 : 0)]
  82. }
  83. /**
  84. * Weight assigned to a file with no recorded duration. One millisecond keeps
  85. * the LPT assignment purely duration-driven once history exists, while a
  86. * first run (no cache at all) degrades to an even file-count split.
  87. */
  88. const UNKNOWN_FILE_WEIGHT = 1
  89. /**
  90. * Coordinator-maintained duration history. CI removes `node_modules/.vite`
  91. * on every checkout, so Vitest's own cache never survives there; this
  92. * gitignored file at the repository root carries recorded durations across
  93. * runs on a persistent checkout (self-hosted runners).
  94. */
  95. const FILE_TIMES_NAME = '.coverage-times.json'
  96. /**
  97. * The instrumented inventory: every file plus the Vitest project it belongs
  98. * to (`thread-safe` or `process-bound`). Preserving the per-project split
  99. * matters because the projects are mutually exclusive: a file's own project
  100. * must run it exactly once, so a partition config cannot hand the whole
  101. * partition list to every project.
  102. */
  103. export interface InstrumentedInventory {
  104. files: string[]
  105. /** Project name per file; the pool prefix of the `vitest list` line. */
  106. projectOf: Map<string, string>
  107. }
  108. /**
  109. * Parse `vitest list --filesOnly` output into the instrumented inventory:
  110. * one `[pool] path` line per file, deduplicated, minus the exempt heavy
  111. * suites that `vitest list` itself does not exclude.
  112. */
  113. export function parseListOutput(output: string, root: string): InstrumentedInventory {
  114. const files = new Set<string>()
  115. const projectOf = new Map<string, string>()
  116. for (const line of output.split(/\r?\n/)) {
  117. const match = /^\[([^\]]+)\]\s+(\S+\.spec\.(?:ts|tsx))$/.exec(line)
  118. if (match !== null && match[1] !== undefined && match[2] !== undefined) {
  119. files.add(match[2])
  120. projectOf.set(match[2], match[1])
  121. }
  122. }
  123. for (const suite of coverageExemptHeavySuites) {
  124. for (const file of globSync(suite.exclude, { cwd: root })) {
  125. // globSync returns platform separators on Windows; the parsed inventory
  126. // and Vitest include patterns both use forward slashes.
  127. const normalized = file.split('\\').join('/')
  128. files.delete(normalized)
  129. projectOf.delete(normalized)
  130. }
  131. }
  132. return { files: [...files].sort(), projectOf }
  133. }
  134. /**
  135. * Collect the instrumented coverage inventory from a `vitest list --filesOnly`
  136. * run: no test collection and no worker pool, just the file list. Caller
  137. * filters (positional args after `--`) narrow the list before the exempt
  138. * heavy suites are removed here because `vitest list` does not apply the
  139. * `COVERAGE_EXEMPT_ENV` exclusion.
  140. */
  141. async function collectInstrumentedFiles(
  142. root: string,
  143. pnpmEntrypoint: string,
  144. filters: readonly string[] = [],
  145. ): Promise<InstrumentedInventory> {
  146. const invocation = pnpmInvocation(['exec', 'vitest', 'list', '--filesOnly', ...filters], { npm_execpath: pnpmEntrypoint })
  147. const output = await runListCommand(invocation.command, invocation.args, root)
  148. return parseListOutput(output, root)
  149. }
  150. /** Run `vitest list` and return its stdout, or throw with exit code and stderr. */
  151. function runListCommand(command: string, args: string[], root: string): Promise<string> {
  152. return new Promise((resolveList, rejectList) => {
  153. const child = spawn(command, args, { cwd: root, env: process.env, stdio: ['ignore', 'pipe', 'pipe'] })
  154. let output = ''
  155. let errorOutput = ''
  156. child.stdout.setEncoding('utf8')
  157. child.stderr.setEncoding('utf8')
  158. child.stdout.on('data', (chunk: string) => { output += chunk })
  159. child.stderr.on('data', (chunk: string) => { errorOutput += chunk })
  160. child.once('error', (error: Error) => { rejectList(error) })
  161. child.once('close', (code) => {
  162. if (code === 0) resolveList(output)
  163. else rejectList(new Error(`vitest list exited with ${String(code ?? 'signal')}${errorOutput === '' ? '' : `: ${errorOutput.trim().slice(0, 300)}`}`))
  164. })
  165. })
  166. }
  167. /**
  168. * Read recorded per-file durations: the coordinator's persisted file first
  169. * (survives CI checkouts), falling back to the Vitest results cache for local
  170. * development. Cache entries are `[projectName:relativePath, {duration}]`;
  171. * a file appearing several times keeps the average duration.
  172. */
  173. export function readFileDurations(root: string): Map<string, number> {
  174. const persisted = readPersistedDurations(root)
  175. if (persisted.size > 0) return persisted
  176. const totals = new Map<string, { sum: number; count: number }>()
  177. for (const file of globSync('node_modules/.vite/vitest/*/results.json', { cwd: root })) {
  178. let cache: { results?: Array<[string, { duration?: number }]> }
  179. try {
  180. cache = JSON.parse(readFileSync(join(root, file), 'utf8')) as { results?: Array<[string, { duration?: number }]> }
  181. } catch {
  182. continue
  183. }
  184. for (const [key, entry] of cache.results ?? []) {
  185. const separator = key.indexOf(':')
  186. if (separator < 0) continue
  187. const path = key.slice(separator + 1)
  188. const duration = entry.duration
  189. if (typeof duration !== 'number') continue
  190. const total = totals.get(path)
  191. if (total === undefined) totals.set(path, { sum: duration, count: 1 })
  192. else {
  193. total.sum += duration
  194. total.count++
  195. }
  196. }
  197. }
  198. return new Map([...totals].map(([path, { sum, count }]) => [path, sum / count]))
  199. }
  200. /** Read the coordinator's persisted duration map; empty when absent or corrupt. */
  201. function readPersistedDurations(root: string): Map<string, number> {
  202. let raw: Record<string, unknown>
  203. try {
  204. raw = JSON.parse(readFileSync(join(root, FILE_TIMES_NAME), 'utf8')) as Record<string, unknown>
  205. } catch {
  206. return new Map()
  207. }
  208. const durations = new Map<string, number>()
  209. for (const [file, duration] of Object.entries(raw)) {
  210. if (typeof duration === 'number' && Number.isFinite(duration)) durations.set(file, duration)
  211. }
  212. return durations
  213. }
  214. /**
  215. * Merge new durations into the persisted file and rewrite it. A fresh run's
  216. * measurements overwrite earlier ones, so the history tracks the latest
  217. * checkout's behavior; entries whose file no longer exists in the current
  218. * inventory are dropped, so deleted or renamed specs never linger with stale
  219. * weights.
  220. */
  221. export function writeFileDurations(
  222. root: string,
  223. durations: ReadonlyMap<string, number>,
  224. currentFiles?: readonly string[],
  225. ): void {
  226. if (durations.size === 0) return
  227. const merged = new Map(readPersistedDurations(root))
  228. for (const [file, duration] of durations) merged.set(file, duration)
  229. if (currentFiles !== undefined) {
  230. const present = new Set(currentFiles)
  231. for (const file of [...merged.keys()]) {
  232. if (!present.has(file)) merged.delete(file)
  233. }
  234. }
  235. writeFileSync(join(root, FILE_TIMES_NAME), `${JSON.stringify(Object.fromEntries(merged), null, 1)}\n`, 'utf8')
  236. }
  237. /**
  238. * Extract per-file durations from Vitest JSON reporter outputs (one per
  239. * partition). Each `testResults` entry names an absolute spec path and carries
  240. * `startTime`/`endTime`; the difference is the file's recorded duration.
  241. */
  242. export function collectPartitionDurations(reportFiles: readonly string[], root: string): Map<string, number> {
  243. const durations = new Map<string, number>()
  244. for (const file of reportFiles) {
  245. let report: { testResults?: Array<{ name?: unknown; startTime?: number; endTime?: number }> }
  246. try {
  247. report = JSON.parse(readFileSync(file, 'utf8')) as { testResults?: Array<{ name?: unknown; startTime?: number; endTime?: number }> }
  248. } catch {
  249. continue
  250. }
  251. for (const result of report.testResults ?? []) {
  252. if (typeof result.name !== 'string' || typeof result.startTime !== 'number' || typeof result.endTime !== 'number') continue
  253. const relativePath = relative(root, result.name).split(sep).join('/')
  254. durations.set(relativePath, Math.max(0, result.endTime - result.startTime))
  255. }
  256. }
  257. return durations
  258. }
  259. /**
  260. * Assign files to partitions by longest-processing-time: heavier files are
  261. * seeded first into the currently lightest partition, so recorded durations
  262. * (and the import/environment cost that scales with a partition's file set)
  263. * spread instead of piling into whichever shard the hash lands them in. A
  264. * min-heap over the buckets keeps each placement at O(log partitions).
  265. * @returns one file list per partition, every partition non-empty.
  266. */
  267. export function assignWeightedPartitions(
  268. files: readonly string[],
  269. weights: ReadonlyMap<string, number>,
  270. partitions: number,
  271. ): string[][] {
  272. if (files.length === 0) return Array.from({ length: partitions }, () => [])
  273. const weighted = files
  274. .map(file => ({ file, weight: weights.get(file) ?? UNKNOWN_FILE_WEIGHT }))
  275. .sort((a, b) => b.weight - a.weight || a.file.localeCompare(b.file))
  276. const buckets = Array.from({ length: partitions }, () => ({ sum: 0, files: [] as string[] }))
  277. // Min-heap of bucket indices ordered by (sum, file count); equal sums pick
  278. // the leaner bucket so a duration-sparse inventory still balances file count.
  279. const heap = buckets.map((_, index) => index)
  280. for (const { file, weight } of weighted) {
  281. const top = heap[0]
  282. if (top === undefined) throw new Error('coverage partitions: partition heap index is out of bounds.')
  283. const bucket = buckets[top]
  284. if (bucket === undefined) throw new Error('coverage partitions: partition bucket is missing.')
  285. bucket.sum += weight
  286. bucket.files.push(file)
  287. siftDown(buckets, heap, 0)
  288. }
  289. return buckets.map(bucket => bucket.files)
  290. }
  291. /** Restore the min-heap property after the root bucket grew heavier. */
  292. function siftDown(
  293. buckets: Array<{ sum: number; files: string[] }>,
  294. heap: number[],
  295. index: number,
  296. ): void {
  297. const size = heap.length
  298. for (;;) {
  299. const left = 2 * index + 1
  300. const right = 2 * index + 2
  301. let smallest = index
  302. let smallestBucket = bucketAt(buckets, heap, index)
  303. const leftBucket = left < size ? bucketAt(buckets, heap, left) : undefined
  304. const rightBucket = right < size ? bucketAt(buckets, heap, right) : undefined
  305. if (leftBucket !== undefined && smallestBucket !== undefined && bucketLess(leftBucket, smallestBucket)) {
  306. smallest = left
  307. smallestBucket = leftBucket
  308. }
  309. if (rightBucket !== undefined && smallestBucket !== undefined && bucketLess(rightBucket, smallestBucket)) {
  310. smallest = right
  311. }
  312. if (smallest === index) return
  313. const moved = heap[index]
  314. const replacement = heap[smallest]
  315. if (moved === undefined || replacement === undefined) return
  316. heap[index] = replacement
  317. heap[smallest] = moved
  318. index = smallest
  319. }
  320. }
  321. /** Read the bucket at a heap position, or undefined when the index is absent. */
  322. function bucketAt(
  323. buckets: Array<{ sum: number; files: string[] }>,
  324. heap: number[],
  325. index: number,
  326. ): { sum: number; files: string[] } | undefined {
  327. const bucketIndex = heap[index]
  328. return bucketIndex === undefined ? undefined : buckets[bucketIndex]
  329. }
  330. /** Order buckets by total weight, then by file count, then by nothing (stable). */
  331. function bucketLess(
  332. left: { sum: number; files: string[] } | undefined,
  333. right: { sum: number; files: string[] } | undefined,
  334. ): boolean {
  335. if (left === undefined || right === undefined) return left !== undefined
  336. if (left.sum !== right.sum) return left.sum < right.sum
  337. return left.files.length < right.files.length
  338. }
  339. /** Sum of a partition's file weights; unknown weights count as one. */
  340. function partitionWeight(files: readonly string[], weights: ReadonlyMap<string, number>): number {
  341. return files.reduce((sum, file) => sum + (weights.get(file) ?? UNKNOWN_FILE_WEIGHT), 0)
  342. }
  343. /**
  344. * Source of one partition's temporary Vitest config: the workspace config
  345. * with `test.include` narrowed to the partition's file list, per project.
  346. * The config sits under `coverage/.partitioned/`, so the workspace config is
  347. * two directories up, and the partition processes run with cwd at the
  348. * repository root (Vite resolves the relative include patterns against it).
  349. * Each project keeps only the files that belong to it: the projects are
  350. * mutually exclusive, so handing the whole partition list to every project
  351. * would run plain files twice (once per project).
  352. */
  353. function partitionConfigSource(
  354. files: readonly string[],
  355. projectOf: ReadonlyMap<string, string>,
  356. ): string {
  357. const threadSafe = JSON.stringify(files.filter(file => projectOf.get(file) !== 'process-bound').map(file => file.split('\\').join('/')))
  358. const processBound = JSON.stringify(files.filter(file => projectOf.get(file) === 'process-bound').map(file => file.split('\\').join('/')))
  359. return [
  360. "import base from '../../vitest.config.ts'",
  361. 'export default {',
  362. ' ...base,',
  363. ' test: {',
  364. ' ...base.test,',
  365. ' projects: (base.test.projects ?? []).map(project => ({',
  366. ' ...project,',
  367. ' test: {',
  368. ' ...project.test,',
  369. ' include: project.test.name === \'process-bound\' ? ' + processBound + ' : ' + threadSafe + ',',
  370. ' },',
  371. ' })),',
  372. ' },',
  373. '}',
  374. '',
  375. ].join('\n')
  376. }
  377. /** Run instrumented partitions, validate their blobs, and merge once. */
  378. export class CoveragePartitionCoordinator {
  379. private readonly root: string
  380. private readonly partitions: number
  381. private readonly pnpmEntrypoint: string
  382. private readonly vitestArgs: string[]
  383. private readonly runCommand: CoverageCommandRunner
  384. private readonly files: readonly string[]
  385. private readonly weights: ReadonlyMap<string, number> | undefined
  386. private projectOf = new Map<string, string>()
  387. private readonly temporaryRoot: string
  388. private readonly blobsRoot: string
  389. /** Create a coordinator from validated process-independent inputs. */
  390. public constructor(options: CoveragePartitionCoordinatorOptions) {
  391. if (!Number.isSafeInteger(options.partitions) || options.partitions < 2) {
  392. throw new Error(`coverage partitions must be an integer greater than 1, got ${String(options.partitions)}.`)
  393. }
  394. this.root = options.root
  395. this.partitions = options.partitions
  396. this.pnpmEntrypoint = options.pnpmEntrypoint
  397. this.vitestArgs = options.vitestArgs ?? []
  398. this.runCommand = options.runCommand ?? runCoverageCommand
  399. this.files = options.files ?? []
  400. this.weights = options.weights
  401. this.projectOf = new Map(options.projectOf ?? [])
  402. this.temporaryRoot = join(this.root, 'coverage', '.partitioned')
  403. this.blobsRoot = join(this.temporaryRoot, 'blobs')
  404. }
  405. /**
  406. * Run every partition before one merged threshold check.
  407. * @returns zero only when every partition and the merge command succeed.
  408. */
  409. public async run(): Promise<number> {
  410. await removeOwnedTree(join(this.root, 'coverage'))
  411. await mkdir(this.blobsRoot, { recursive: true })
  412. try {
  413. const assignments = await this.assignFiles()
  414. this.assertNonEmptyAssignments(assignments)
  415. const configPaths = await this.writePartitionConfigs(assignments)
  416. const commands = assignments.map((_, index) => this.partitionCommand(index + 1, configPaths[index] ?? ''))
  417. const results = await Promise.all(commands.map(async (command) => {
  418. console.log(`coverage-partitions: start ${command.label}`)
  419. const result = await this.runCommand(command)
  420. if (commandFailed(result)) {
  421. console.error(`coverage-partitions: FAIL ${command.label} (${commandFailureReason(result)})`)
  422. if (result.outputTail !== undefined && result.outputTail !== '') {
  423. console.error(`coverage-partitions: output tail for ${command.label}:\n${result.outputTail}`)
  424. }
  425. }
  426. return result
  427. }))
  428. // Persist durations before blob validation: a missing blob aborts the
  429. // run, but the completed partitions' timings are still worth keeping.
  430. this.persistDurations(this.partitions)
  431. await this.assertCompleteBlobSet(commands)
  432. const mergeCommand = this.mergeCommand()
  433. console.log(`coverage-partitions: start ${mergeCommand.label}`)
  434. const mergeResult = await this.runCommand(mergeCommand)
  435. return results.some(commandFailed) || commandFailed(mergeResult) ? 1 : 0
  436. } finally {
  437. await removeOwnedTree(this.temporaryRoot)
  438. }
  439. }
  440. /**
  441. * Refuse an empty partition: Vitest treats a config with no matching files
  442. * as "run everything", so an empty bucket would silently execute the whole
  443. * suite once per empty partition.
  444. */
  445. private assertNonEmptyAssignments(assignments: readonly (readonly string[])[]): void {
  446. const empty = assignments.findIndex(files => files.length === 0)
  447. if (empty >= 0) {
  448. throw new Error(
  449. `coverage partitions: partition ${empty + 1}/${this.partitions} has no files; `
  450. + 'the instrumented inventory is empty or smaller than the partition count.',
  451. )
  452. }
  453. }
  454. /** Persist measured per-file durations so the next run can weight by them. */
  455. private persistDurations(partitionCount: number): void {
  456. const reportFiles = Array.from(
  457. { length: partitionCount },
  458. (_, index) => join(this.temporaryRoot, `partition-${index + 1}.report.json`),
  459. )
  460. const currentFiles = this.projectOf.size > 0 ? [...this.projectOf.keys()] : undefined
  461. writeFileDurations(this.root, collectPartitionDurations(reportFiles, this.root), currentFiles)
  462. }
  463. /**
  464. * Distribute the instrumented inventory across partitions by recorded
  465. * duration, heaviest partition first so the longest child starts earliest
  466. * (fail-fast: its verdict, success or failure, lands before the light
  467. * children settle). An injected file list skips workspace collection and
  468. * cache reads (scheduler tests); production collection always runs.
  469. */
  470. private async assignFiles(): Promise<string[][]> {
  471. let files: readonly string[]
  472. let weights: ReadonlyMap<string, number>
  473. if (this.files.length > 0) {
  474. files = this.files
  475. weights = this.weights ?? new Map()
  476. } else {
  477. // Positional filters live after the `--` separator; options and their
  478. // values (`--testTimeout 5000`) must never be mistaken for filters.
  479. const separator = this.vitestArgs.indexOf('--')
  480. const filters = separator >= 0 ? this.vitestArgs.slice(separator + 1) : []
  481. const inventory = await collectInstrumentedFiles(this.root, this.pnpmEntrypoint, filters)
  482. files = inventory.files
  483. this.projectOf = inventory.projectOf
  484. weights = readFileDurations(this.root)
  485. }
  486. const buckets = assignWeightedPartitions(files, weights, this.partitions)
  487. buckets.sort((left, right) => partitionWeight(right, weights) - partitionWeight(left, weights))
  488. return buckets
  489. }
  490. /**
  491. * Write one temporary Vitest config per partition whose `include` (top level
  492. * and per project) is the partition's file list. Passing files on the
  493. * command line exceeded the Windows CreateProcess limit once a partition
  494. * held a few hundred paths, so each partition instead points Vitest at a
  495. * short `--config` path.
  496. */
  497. private async writePartitionConfigs(assignments: readonly (readonly string[])[]): Promise<string[]> {
  498. return await Promise.all(assignments.map(async (files, index) => {
  499. const configPath = join(this.temporaryRoot, `vitest-partition-${index + 1}.config.ts`)
  500. await writeFile(configPath, partitionConfigSource(files, this.projectOf), 'utf8')
  501. return configPath
  502. }))
  503. }
  504. private partitionCommand(index: number, configPath: string): CoverageCommand {
  505. const blobPath = join(this.blobsRoot, `partition-${index}.json`)
  506. const reportsDirectory = join(this.temporaryRoot, `coverage-${index}`)
  507. const jsonReportPath = join(this.temporaryRoot, `partition-${index}.report.json`)
  508. const invocation = pnpmInvocation([
  509. 'exec',
  510. 'vitest',
  511. 'run',
  512. '--coverage',
  513. '--coverage.reportOnFailure',
  514. '--maxWorkers=1',
  515. `--config=${this.relativePath(configPath)}`,
  516. '--reporter=default',
  517. '--reporter=blob',
  518. '--reporter=json',
  519. `--outputFile.blob=${this.relativePath(blobPath)}`,
  520. `--outputFile.json=${this.relativePath(jsonReportPath)}`,
  521. `--coverage.reportsDirectory=${this.relativePath(reportsDirectory)}`,
  522. ...this.vitestArgs,
  523. ], { npm_execpath: this.pnpmEntrypoint })
  524. return {
  525. label: `partition ${index}/${this.partitions}`,
  526. ...invocation,
  527. env: {
  528. [COVERAGE_PARTITIONS_ENV]: undefined,
  529. [COVERAGE_PARTITION_MODE_ENV]: '1',
  530. },
  531. cwd: this.root,
  532. blobPath,
  533. }
  534. }
  535. private mergeCommand(): CoverageCommand {
  536. const invocation = pnpmInvocation([
  537. 'exec',
  538. 'vitest',
  539. `--merge-reports=${this.relativePath(this.blobsRoot)}`,
  540. '--coverage',
  541. ], { npm_execpath: this.pnpmEntrypoint })
  542. return {
  543. label: 'merged coverage report',
  544. ...invocation,
  545. env: {
  546. [COVERAGE_PARTITIONS_ENV]: undefined,
  547. [COVERAGE_PARTITION_MODE_ENV]: undefined,
  548. },
  549. cwd: this.root,
  550. }
  551. }
  552. private relativePath(path: string): string {
  553. return relative(this.root, path).split(sep).join('/')
  554. }
  555. private async assertCompleteBlobSet(commands: CoverageCommand[]): Promise<void> {
  556. const expected = commands.map((command) => {
  557. if (command.blobPath === undefined) throw new Error(`${command.label} has no blob path.`)
  558. return this.relativePath(command.blobPath)
  559. }).sort()
  560. const actual = (await readdir(this.blobsRoot))
  561. .map(name => this.relativePath(join(this.blobsRoot, name)))
  562. .sort()
  563. if (actual.length !== expected.length || actual.some((name, index) => name !== expected[index])) {
  564. throw new Error(`coverage partitions produced ${JSON.stringify(actual)}; expected ${JSON.stringify(expected)}.`)
  565. }
  566. }
  567. }
  568. /** Spawn one pnpm-backed command without a platform shell. */
  569. function runCoverageCommand(command: CoverageCommand): Promise<CoverageCommandResult> {
  570. return new Promise((resolveCommand) => {
  571. let outputTail = ''
  572. const env = { ...process.env }
  573. for (const [name, value] of Object.entries(command.env)) {
  574. if (value === undefined) Reflect.deleteProperty(env, name)
  575. else env[name] = value
  576. }
  577. const child = spawn(command.command, command.args, {
  578. cwd: command.cwd,
  579. env,
  580. stdio: ['ignore', 'pipe', 'pipe'],
  581. })
  582. child.stdout.setEncoding('utf8')
  583. child.stderr.setEncoding('utf8')
  584. child.stdout.on('data', (chunk: string) => {
  585. process.stdout.write(chunk)
  586. outputTail = appendOutputTail(outputTail, chunk)
  587. })
  588. child.stderr.on('data', (chunk: string) => {
  589. process.stderr.write(chunk)
  590. outputTail = appendOutputTail(outputTail, chunk)
  591. })
  592. child.once('error', (error: Error) => {
  593. resolveCommand({ exitCode: null, signalCode: null, error: error.message, outputTail })
  594. })
  595. child.once('close', (exitCode, signalCode) => {
  596. resolveCommand({ exitCode, signalCode, outputTail })
  597. })
  598. })
  599. }
  600. function appendOutputTail(previous: string, chunk: string): string {
  601. const combined = previous + chunk
  602. return combined.length <= 65_536 ? combined : combined.slice(-65_536)
  603. }
  604. function commandFailed(result: CoverageCommandResult): boolean {
  605. return result.exitCode !== 0 || result.signalCode !== null || result.error !== undefined
  606. }
  607. function commandFailureReason(result: CoverageCommandResult): string {
  608. const facts = [
  609. result.error,
  610. result.exitCode === null ? undefined : `exit ${result.exitCode}`,
  611. result.signalCode === null ? undefined : `signal ${result.signalCode}`,
  612. ].filter((fact): fact is string => fact !== undefined)
  613. return facts.join(', ') || 'no exit code or signal'
  614. }
  615. async function removeOwnedTree(path: string): Promise<void> {
  616. const metadata = await lstat(path).catch((error: unknown) => {
  617. if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return undefined
  618. throw error
  619. })
  620. if (metadata === undefined) return
  621. if (metadata.isSymbolicLink() || !metadata.isDirectory()) {
  622. await unlink(path)
  623. return
  624. }
  625. await rm(path, { recursive: true, force: true })
  626. }