{"record":{"id":"5d628ac07836e262","repo":"different-ai/openwork","slug":"environment-variable-store-is-invalid-json","errorCode":null,"errorMessage":"Environment variable store is invalid JSON","messagePattern":"Environment variable store is invalid JSON","errorType":"error_code","errorClass":"EnvStoreReadError","httpStatus":null,"severity":"error","filePath":"apps/server/src/env-file.ts","lineNumber":96,"sourceCode":"): Promise<EnvStoreFile> {\n  if (!(await exists(path))) {\n    return emptyStore();\n  }\n  let raw = \"\";\n  try {\n    raw = await readFile(path, \"utf8\");\n  } catch (error) {\n    if ((error as { code?: string }).code === \"ENOENT\") return emptyStore();\n    if (options.tolerateInvalid) return emptyStore();\n    throw new EnvStoreReadError(\"Environment variable store could not be read\");\n  }\n\n  let parsed: Partial<EnvStoreFile>;\n  try {\n    parsed = JSON.parse(raw) as Partial<EnvStoreFile>;\n  } catch {\n    if (options.tolerateInvalid) return emptyStore();\n    throw new EnvStoreReadError(\"Environment variable store is invalid JSON\");\n  }\n\n  if (!parsed || typeof parsed !== \"object\" || !Array.isArray(parsed.variables)) {\n    if (options.tolerateInvalid) return emptyStore();\n    throw new EnvStoreReadError(\"Environment variable store has an invalid format\");\n  }\n\n  const variables = parsed.variables\n    .map(parseRecord)\n    .filter((entry): entry is EnvRecord => Boolean(entry));\n  return {\n    schemaVersion: typeof parsed.schemaVersion === \"number\" ? parsed.schemaVersion : 1,\n    updatedAt: typeof parsed.updatedAt === \"number\" ? parsed.updatedAt : Date.now(),\n    variables,\n  };\n}\n\nasync function writeStore(path: string, variables: EnvRecord[]): Promise<void> {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/server/src/env-file.ts#L78-L114","documentation":"Error raised when the server's environment variable store file exists but its contents are not valid JSON. Typically caused by manual edits, corrupted writes, or an incompatible serialization format in the persisted env store. Resolution is to fix or restore the JSON in the store file (or delete it so a fresh store is created); the server refuses to load the file to avoid silently dropping stored variables.","triggerScenarios":"ensureLoaded/store call readStore on a file whose contents are not valid JSON (truncated write, manual edits, wrong file configured).","commonSituations":"Crash or power loss mid-write left a truncated file; user hand-edited the file and left a syntax error; a different (non-JSON) file is at the configured path.","solutions":["Validate the file with JSON.parse (or jq) to see the syntax error and fix it by hand","Delete/rename the corrupt file and re-add the variables via the API so a clean store is written","Set tolerateInvalid if the caller treats a corrupt store as empty and should recover automatically","Back up the corrupt file before deleting to recover any variable values manually"],"exampleFix":"// before\ncat store.json  # {\"variables\":[{\"key\":\"A\",   <- truncated\n// after\nmv store.json store.json.bak   # next read creates an empty valid store\n# or: jq . store.json  to locate and fix the syntax error","handlingStrategy":"fallback","validationCode":"let parsed; try { parsed = JSON.parse(raw); } catch { return emptyStore(); }","typeGuard":"function looksLikeEnvStore(v: unknown): boolean { return typeof v === \"object\" && v !== null && Array.isArray((v as any).variables); }","tryCatchPattern":"try { return await readStore(path, { tolerateInvalid: true }); } catch { return emptyStore(); }","preventionTips":["Use tolerateInvalid for read-only/cached reads","Write atomically to avoid truncated files","Keep a backup of the last valid store"],"tags":["json","filesystem","corruption"],"backgroundTag":"invalid-json-file","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}