vitest-dev/vitest · error · Error

Something removed the coverage directory

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

Same guard as the istanbul provider but emitted by the v8 coverage provider's writeCoverageFile. writeFile to the coverage JSON fails and the coverage directory it created earlier no longer exists, pointing at a concurrent remover of reportsDirectory.

Solutions

  1. Use a unique coverage.reportsDirectory per concurrent run/shard, then merge coverage outputs.
  2. Avoid deleting the coverage directory while a run is in progress; clean only before the run.
  3. Run coverage-producing suites sequentially if sharding/merging is not set up.

Example fix

// before
export default defineConfig({ test: { coverage: { provider: 'v8', reportsDirectory: './coverage' } } })
// after
export default defineConfig({ test: { coverage: { provider: 'v8', reportsDirectory: './coverage/shard-1' } } })
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try {
  await vitestRun()
} catch (e) {
  if (/removed the coverage directory/.test(String(e?.message))) {
    throw new Error('Coverage dir lost to a concurrent run; use a unique reportsDirectory.')
  }
  throw e
}

Prevention

When it happens

Trigger: writeCoverageFile(coverageFilesDirectory, coverage) in packages/coverage-v8 calls writeFile, it throws, and existsSync(coverageFilesDirectory) is false.

Common situations: Parallel Vitest workers/projects sharing one v8 coverage.reportsDirectory; a watcher rerun colliding with coverage cleanup; CI cleanup between parallel shards overwriting the same directory.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/ad60f6385d09b711. Report an issue: GitHub.

Appendix: 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 1fa9837ec2)