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

Thrown by writeCoverageFile (istanbul provider) when writeFile fails and existsSync reports the coverage directory it created earlier is now gone. The intended diagnosis is concurrency: another process wiped or reused the same reportsDirectory mid-run.

Solutions

  1. Give each concurrent Vitest run a distinct coverage.reportsDirectory (e.g. coverage-unit, coverage-e2e) or run them sequentially.
  2. Remove pre-run clean hooks that delete the coverage dir; clean before the run starts, not during.
  3. In CI shards, namespace the reportsDirectory per shard and merge after.

Example fix

// before: two projects share the same dir
export default defineConfig({ test: { coverage: { reportsDirectory: './coverage' } } })
// after
export default defineConfig({ test: { coverage: { reportsDirectory: './coverage/unit' } } })
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))) {
    // a concurrent run wiped the dir — re-run isolated, or merge later
    throw new Error('Coverage dir lost to a concurrent run; use a unique reportsDirectory.')
  }
  throw e
}

Prevention

When it happens

Trigger: writeCoverageFile(coverageFilesDirectory, coverage) calls writeFile, the call throws, and a subsequent existsSync(coverageFilesDirectory) returns false — meaning the directory was removed between setup and write.

Common situations: Two Vitest processes (e.g. unit and e2e) configured with the same coverage.reportsDirectory; a watch-mode rerun colliding with a clean script; CI that deletes the coverage folder between shards; an external tool (rm -rf coverage) racing the test.

Related errors


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

Appendix: source

Thrown at packages/coverage-istanbul/src/commands.ts:28

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

    return writeCoverageFile(provider.coverageFilesDirectory, coverage)
  },
}

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
}

View on GitHub (pinned to 1fa9837ec2)