{"record":{"id":"f86468bc07d99a64","repo":"vitest-dev/vitest","slug":"cannot-read-file-when-saving-inline-snapshot","errorCode":null,"errorMessage":"cannot read ${file} when saving inline snapshot","messagePattern":"cannot read (.+?) when saving inline snapshot","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/snapshot/src/port/inlineSnapshot.ts","lineNumber":34,"sourceCode":"  column: number\n  // it maybe possible to accurately extract this from `ParsedStack.method`,\n  // but for now, we ask higher level assertion to pass it explicitly\n  // since this is useful for certain error messages before we extract stack.\n  assertionName?: string\n}\n\nexport async function saveInlineSnapshots(\n  environment: SnapshotEnvironment,\n  snapshots: Array<InlineSnapshot>,\n): Promise<void> {\n  const MagicString = (await import('magic-string')).default\n  const files = new Set(snapshots.map(i => i.file))\n  await Promise.all(\n    Array.from(files).map(async (file) => {\n      const snaps = snapshots.filter(i => i.file === file)\n      const code = await environment.readSnapshotFile(file)\n      if (code == null) {\n        throw new Error(`cannot read ${file} when saving inline snapshot`)\n      }\n\n      const s = new MagicString(code)\n\n      for (const snap of snaps) {\n        const index = positionToOffset(code, snap.line, snap.column)\n        replaceInlineSnap(code, s, index, snap.snapshot, snap.assertionName)\n      }\n\n      const transformed = s.toString()\n      if (transformed !== code) {\n        await environment.saveSnapshotFile(file, transformed)\n      }\n    }),\n  )\n}\n\nconst defaultStartObjectRegex","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/snapshot/src/port/inlineSnapshot.ts#L16-L52","documentation":"While saving inline snapshots, saveInlineSnapshots reads each affected source file via environment.readSnapshotFile; if that returns null (file missing, unreadable, or the environment cannot read it), the writer cannot compute offsets to insert/replace snapshots and aborts.","triggerScenarios":"An inline snapshot assertion whose source file was deleted, moved, or made unreadable between the test run and the save phase; a custom SnapshotEnvironment whose readSnapshotFile returns null; permission error on the source file.","commonSituations":"File deleted/renamed mid-run; running tests in a read-only or sandboxed FS where the environment cannot read the source; custom environment with a broken readSnapshotFile; race where another process moved the file.","solutions":[],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-check the source file is readable before running inline snapshots\nconst code = await environment.readSnapshotFile(file)\nif (code == null) throw new Error(`source file ${file} is not readable; cannot save inline snapshot`)","typeGuard":null,"tryCatchPattern":"try {\n  await saveInlineSnapshots(environment, snapshots)\n} catch (e) {\n  if (e instanceof Error && /cannot read .* when saving inline snapshot/.test(e.message)) {\n    // restore/recreate the file, or write snapshots manually\n  } else throw e\n}","preventionTips":["Ensure source files exist and are readable for the whole test run","Implement readSnapshotFile to return string contents in custom environments","Avoid moving/deleting test files mid-run"],"tags":["snapshot","inline","filesystem","environment"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}