vitest-dev/vitest · error · Error

cannot read ${file} when saving inline snapshot

Error message

cannot read ${file} when saving inline snapshot

What it means

Thrown by saveInlineSnapshots() when environment.readSnapshotFile(file) returns null while writing back inline snapshots during --update or first write. readSnapshotFile returns null when the file does not exist or is not readable on the host; saving an inline snapshot requires reading the source to rewrite it in place, so a missing source file aborts the operation.

Source

Thrown at packages/snapshot/src/port/inlineSnapshot.ts:34

  column: number
  // it maybe possible to accurately extract this from `ParsedStack.method`,
  // but for now, we ask higher level assertion to pass it explicitly
  // since this is useful for certain error messages before we extract stack.
  assertionName?: string
}

export async function saveInlineSnapshots(
  environment: SnapshotEnvironment,
  snapshots: Array<InlineSnapshot>,
): Promise<void> {
  const MagicString = (await import('magic-string')).default
  const files = new Set(snapshots.map(i => i.file))
  await Promise.all(
    Array.from(files).map(async (file) => {
      const snaps = snapshots.filter(i => i.file === file)
      const code = await environment.readSnapshotFile(file)
      if (code == null) {
        throw new Error(`cannot read ${file} when saving inline snapshot`)
      }

      const s = new MagicString(code)

      for (const snap of snaps) {
        const index = positionToOffset(code, snap.line, snap.column)
        replaceInlineSnap(code, s, index, snap.snapshot, snap.assertionName)
      }

      const transformed = s.toString()
      if (transformed !== code) {
        await environment.saveSnapshotFile(file, transformed)
      }
    }),
  )
}

const defaultStartObjectRegex

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Verify the test file exists on disk at the path shown in the error before re-running vitest -u.
  2. If using a custom SnapshotEnvironment, ensure readSnapshotFile returns the file's source string (not null) for existing files.
  3. Avoid deleting or moving spec files during a running vitest watch/update session.
  4. Re-run the update once the filesystem is stable.
Defensive patterns

Strategy: validation

Validate before calling

import { existsSync } from 'node:fs'
if (!existsSync(file)) {
  throw new Error(`source file missing before inline snapshot update: ${file}`)
}

Try / catch

try {
  await vitest.runInlineTests({...})
} catch (err) {
  if (err.message.includes('cannot read') && err.message.includes('saving inline snapshot')) {
    // file removed mid-run; recreate it and re-run vitest -u
  } else throw err
}

Prevention

When it happens

Trigger: Running vitest -u on a test file that was deleted between collection and write; custom SnapshotEnvironment.readSnapshotFile returning null; filesystem permissions or a race where the file is removed mid-run; running inline snapshot updates through a virtual filesystem that doesn't expose the source.

Common situations: IDE or watcher deleting/renaming a file while vitest is updating; misconfigured roots causing the resolved file path to point at a non-existent location; in-memory test setups (runInlineTests) that forget to register the source file with the environment.

Related errors


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