vitest-dev/vitest · error · Error

Something removed the coverage directory "${coverageFilesDir

Error message

Something removed the coverage directory "${coverageFilesDirectory}" Vitest created earlier. Make sure you are not running multiple Vitests with the same "coverage.reportsDirectory" at the same time.

What it means

Identical guard to the Istanbul one but in the V8 coverage provider's writeCoverageFile. After CDP coverage is collected and filtered, it is written to coverageFilesDirectory; if the write fails and that directory is gone, the provider blames a concurrent Vitest that deleted it.

Source

Thrown at packages/coverage-v8/src/commands.ts:54

  const provider = context.project.vitest.coverageProvider as V8CoverageProvider

  return await writeCoverageFile(provider.coverageFilesDirectory, { result })
}

export async function writeCoverageFile(coverageFilesDirectory: string, coverage: unknown): Promise<string> {
  // Write results on file system directly and transfer only the filename over RPC
  const filename = resolve(
    coverageFilesDirectory,
    `coverage-${randomUUID()}.json`,
  )

  try {
    await writeFile(filename, JSON.stringify(coverage), 'utf-8')
  }
  catch (error) {
    if (!existsSync(coverageFilesDirectory)) {
      throw new Error(
        `Something removed the coverage directory "${coverageFilesDirectory}" Vitest created earlier. Make sure you are not running multiple Vitests with the same "coverage.reportsDirectory" at the same time.`,
        { cause: error },
      )
    }

    throw error
  }

  return filename
}

function filterResult(url: string, origin: string, pageUrl: string): boolean {
  if (!url.startsWith(origin)) {
    return false
  }

  if (url.includes('/node_modules/')) {
    return false

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Set a unique coverage.reportsDirectory per concurrent Vitest process.
  2. Serialize coverage runs in CI.
  3. Ensure nothing cleans the reports directory while a run is in progress.

Example fix

// before
// vitest --coverage --coverage.provider=v8  (run twice in parallel)
// after
// run 1: --coverage.reportsDirectory=coverage/v8-a
// run 2: --coverage.reportsDirectory=coverage/v8-b
Defensive patterns

Strategy: validation

Validate before calling

import { existsSync, mkdirSync } from 'node:fs'
if (!existsSync(coverageFilesDirectory)) {
  mkdirSync(coverageFilesDirectory, { recursive: true })
}

Try / catch

try {
  await writeCoverageFile(dir, { result })
} catch (e) {
  if (/removed the coverage directory/i.test(String((e as Error).message))) {
    mkdirSync(dir, { recursive: true })
    return writeCoverageFile(dir, { result })
  }
  throw e
}

Prevention

When it happens

Trigger: takeV8Coverage (browser CDP) calls writeCoverageFile; the JSON write throws and existsSync(coverageFilesDirectory) returns false, producing this error with the underlying FS error as cause.

Common situations: Parallel CI jobs with the same coverage.reportsDirectory; coverage directory on a tmpfs that gets cleaned; a post-run hook deleting coverage/ before the browser provider flushes its file.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/ad60f6385d09b711.json. Report an issue: GitHub.