coverage-partitions.ts 28 KB

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