{"record":{"id":"72bffd960713eb75","repo":"mastra-ai/mastra","slug":"failed-to-parse-snapshot-snapshotpath-err-in","errorCode":null,"errorMessage":"Failed to parse snapshot ${snapshotPath}: ${err instanceof Error ? err.message : String(err)}","messagePattern":"Failed to parse snapshot (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"observability/mastra/src/exporters/test.ts","lineNumber":1303,"sourceCode":"    }\n\n    let snapshotData: { __structure__?: string[]; spans?: unknown } | unknown[];\n    let snapshotContent: string;\n    try {\n      const { readFile } = await import('node:fs/promises');\n      snapshotContent = await readFile(snapshotPath, 'utf-8');\n    } catch (err: unknown) {\n      if (err && typeof err === 'object' && 'code' in err && (err as NodeJS.ErrnoException).code === 'ENOENT') {\n        throw new Error(\n          `Snapshot file not found: ${snapshotPath}\\n` + `Run with { updateSnapshot: true } to create it.`,\n        );\n      }\n      throw err;\n    }\n    try {\n      snapshotData = JSON.parse(snapshotContent);\n    } catch (err: unknown) {\n      throw new Error(`Failed to parse snapshot ${snapshotPath}: ${err instanceof Error ? err.message : String(err)}`);\n    }\n\n    // Handle both old format (array) and new format (object with __structure__ and spans)\n    let expectedSpans: unknown;\n    let expectedStructure: string[] | undefined;\n\n    if (Array.isArray(snapshotData)) {\n      // Old format: just the spans array\n      expectedSpans = snapshotData;\n    } else if (snapshotData && typeof snapshotData === 'object' && 'spans' in snapshotData) {\n      // New format: { __structure__, spans }\n      expectedSpans = snapshotData.spans;\n      expectedStructure = snapshotData.__structure__;\n    } else {\n      throw new Error(\n        `Invalid snapshot format in ${snapshotPath}.\\n` + `Expected an array or object with 'spans' property.`,\n      );\n    }","sourceCodeStart":1285,"sourceCodeEnd":1321,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/observability/mastra/src/exporters/test.ts#L1285-L1321","documentation":"After reading the snapshot file, TestExporter JSON.parse()s it. If the content is not valid JSON (truncated write, manual edit, merge-conflict markers, encoding issues), the library wraps the parse error with the snapshot path and underlying message so you know which file is corrupt.","triggerScenarios":"The file at snapshotPath exists but contains invalid JSON — partially written output from a crashed update run, hand-edited snapshots, git conflict markers (<<<<<<<) in the file, or non-JSON content saved under the snapshot name.","commonSituations":"Merge conflicts in committed snapshots resolved textually but never re-validated; interrupted test runs leaving truncated files; editor saves with non-UTF8 or trailing garbage.","solutions":["Open the reported snapshot file and inspect the underlying JSON syntax error in the wrapped message","Delete the corrupt file and regenerate with { updateSnapshot: true }","If caused by a merge conflict, resolve by regenerating the snapshot rather than hand-merging","Ensure no build step writes non-JSON content to snapshot-named files"],"exampleFix":"// before (snapshot.json contains '<<<<<<< HEAD')\n$ rm .snapshots/test.json\n// after\nconst exporter = new TestExporter({ updateSnapshot: true }); // regenerates valid JSON","handlingStrategy":"validation","validationCode":"const content = await readFile(path, 'utf-8');\ntry { JSON.parse(content); } catch (e) {\n  throw new Error(`Corrupt snapshot ${path}; regenerate with updateSnapshot: true`);\n}","typeGuard":"function isParseableJson(text: string): boolean {\n  try { JSON.parse(text); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  await exporter.export(spans, callback);\n} catch (err) {\n  if ((err as Error).message.startsWith('Failed to parse snapshot')) {\n    await rm(snapshotPath);\n    await new TestExporter({ updateSnapshot: true }).export(spans, callback);\n  } else throw err;\n}","preventionTips":["Never hand-edit snapshot files; regenerate instead","Resolve merge conflicts by regenerating snapshots, not textual merges","Check for git conflict markers (<<<<<<<) before running comparisons","Avoid interrupting updateSnapshot runs mid-write"],"tags":["snapshots","json-parse","corrupt-file","filesystem"],"backgroundTag":"json-parse-error","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}