{"record":{"id":"6620b2666e93de6d","repo":"thedotmack/claude-mem","slug":"corrupt-json-file-refusing-to-overwrite-filepa","errorCode":null,"errorMessage":"Corrupt JSON file, refusing to overwrite: ${filePath}: ${error instanceof Error ? error.message : String(error)}","messagePattern":"Corrupt JSON file, refusing to overwrite: (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/utils/json-utils.ts","lineNumber":13,"sourceCode":"import { existsSync, readFileSync } from 'fs';\nimport { parseJsonWithBom } from '../shared/atomic-json.js';\n\nexport function readJsonSafe<T>(filePath: string, defaultValue: T): T {\n  if (!existsSync(filePath)) return defaultValue;\n  try {\n    // Windows tooling (PowerShell 5.1, some editors) may rewrite JSON with a\n    // UTF-8 BOM. Node/Bun decode that to U+FEFF at offset 0, which JSON.parse\n    // rejects. Strip on read so install/uninstall and other callers survive.\n    // See #3013.\n    return parseJsonWithBom<T>(readFileSync(filePath, 'utf-8'));\n  } catch (error: unknown) {\n    throw new Error(`Corrupt JSON file, refusing to overwrite: ${filePath}: ${error instanceof Error ? error.message : String(error)}`);\n  }\n}\n","sourceCodeStart":1,"sourceCodeEnd":16,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/utils/json-utils.ts#L1-L16","documentation":"readJsonSafe reads a JSON file and parses it (with BOM tolerance for Windows tooling); if reading or parsing fails, it throws with the file path and underlying message, explicitly refusing to let callers overwrite a file it could not understand. The intent is data protection: an unparseable config file may still hold user edits, so blindly overwriting it would destroy data.","triggerScenarios":"Calling any consumer (knownMarketplaces, installedPlugins, settings, readMcpConfigTolerantly, etc.) when the target file contains invalid JSON (truncated write, manual edit typo, BOM handled elsewhere, empty file).","commonSituations":"A crashed process left a half-written settings.json; user hand-edited JSON and introduced a syntax error; PowerShell 5.1 or an editor rewrote the file with a UTF-8 BOM (#3013); file exists but is empty.","solutions":["Open the file, fix the JSON syntax error (or the empty/truncated content), and save","Back up the corrupt file, then restore it from a known-good version and rerun","If truly unrecoverable, manually move/delete the file so a fresh valid one can be written — after confirming nothing valuable is lost","Re-save the file as UTF-8 without BOM if Windows tooling rewrote it"],"exampleFix":"// before\nconst config = readJsonSafe<Config>(path); // throws on corrupt file\n// after\nlet config: Config;\ntry {\n  config = readJsonSafe<Config>(path);\n} catch {\n  backupCorruptFile(path);\n  config = defaultConfig;\n}","handlingStrategy":"try-catch","validationCode":"import { existsSync, readFileSync } from 'fs';\nif (existsSync(filePath)) { try { JSON.parse(readFileSync(filePath, 'utf-8').replace(/^\\uFEFF/, '')); } catch (e) { console.error('corrupt JSON:', filePath, e.message); } }","typeGuard":null,"tryCatchPattern":"try { const cfg = readJsonSafe<Config>(path); }\ncatch (e) { if (e.message.startsWith('Corrupt JSON file')) { backupAndReset(path); const cfg = readJsonSafe<Config>(path); } else throw e; }","preventionTips":["Write JSON atomically (temp file + rename) to avoid truncated files","Validate JSON after manual edits with a linter/formatter","Save config files as UTF-8 without BOM when using Windows editors","Back up config files before uninstall/reinstall operations"],"tags":["json","filesystem","config"],"backgroundTag":"json-parse-error","analyzedSha":"d8bc9755e74915e5c3b999181e10a67c889bce2a","analyzedAt":"2026-09-17T16:40:26.182Z","contentChangedAt":"2026-09-17T16:40:26.182Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}