{"record":{"id":"3810c369021646f9","repo":"thedotmack/claude-mem","slug":"folder-does-not-exist-folderpath","errorCode":null,"errorMessage":"Folder does not exist: ${folderPath}","messagePattern":"Folder does not exist: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cli/claude-md-commands.ts","lineNumber":248,"sourceCode":"      }\n\n      lines.push('');\n    }\n  }\n\n  return lines.join('\\n').trim();\n}\n\nfunction writeClaudeMdToFolder(folderPath: string, newContent: string): void {\n  const resolvedPath = path.resolve(folderPath);\n\n  if (resolvedPath.includes('/.git/') || resolvedPath.includes('\\\\.git\\\\') || resolvedPath.endsWith('/.git') || resolvedPath.endsWith('\\\\.git')) return;\n\n  const claudeMdPath = path.join(folderPath, 'CLAUDE.md');\n  const tempFile = `${claudeMdPath}.tmp`;\n\n  if (!existsSync(folderPath)) {\n    throw new Error(`Folder does not exist: ${folderPath}`);\n  }\n\n  let existingContent = '';\n  if (existsSync(claudeMdPath)) {\n    existingContent = readFileSync(claudeMdPath, 'utf-8');\n  }\n\n  const startTag = '<claude-mem-context>';\n  const endTag = '</claude-mem-context>';\n\n  let finalContent: string;\n  if (!existingContent) {\n    finalContent = `${startTag}\\n${newContent}\\n${endTag}`;\n  } else {\n    const startIdx = existingContent.indexOf(startTag);\n    const endIdx = existingContent.indexOf(endTag);\n\n    if (startIdx !== -1 && endIdx !== -1) {","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d768ba364302d12b76e69e4f021f0bb1d2d50ed6/src/cli/claude-md-commands.ts#L230-L266","documentation":"Thrown by writeClaudeMdToFolder() when it tries to write the managed <claude-mem-context> block into a folder that no longer exists on disk. The function resolves the path, skips any .git directory, then checks existsSync(folderPath) before attempting the write — so this fires only for genuinely absent directories. It guards against a traversal/discovery race where a directory was enumerated earlier but is gone by write time.","triggerScenarios":"A directory walk collected folderPath, then it was deleted (git checkout/branch switch, clean operation, or external process) before the write reaches it. A symlink that resolved during discovery but whose target was removed. A stale cached path from an earlier run reused after a cleanup.","commonSituations":"The user runs claude-md context injection while simultaneously switching git branches that remove directories. An editor or build tool purges output folders mid-operation. The folder is on a network mount that disconnected.","solutions":["Confirm the folder still exists at the reported path with `ls`/`Get-ChildItem`; if a branch switch removed it, switch back or re-run discovery.","If the directory should exist, create it (`mkdir -p`) and re-run, or skip the path if it is no longer relevant.","Avoid running context injection concurrently with git operations or clean tasks that mutate the tree.","Re-run the claude-md command after the filesystem is stable."],"exampleFix":"// before: write attempted on a deleted folder -> throws\n// after: ensure the directory exists before writing\nif (!existsSync(folderPath)) mkdirSync(folderPath, { recursive: true });","handlingStrategy":"validation","validationCode":"import { existsSync, mkdirSync } from 'node:fs';\n\nfunction ensureFolderForWrite(folderPath: string): void {\n  if (!existsSync(folderPath)) {\n    mkdirSync(folderPath, { recursive: true });\n  }\n}\n// call ensureFolderForWrite(folderPath) immediately before writeClaudeMdToFolder","typeGuard":"function isExistingFolder(p: string): boolean {\n  try {\n    return existsSync(p) && statSync(p).isDirectory();\n  } catch {\n    return false;\n  }\n}","tryCatchPattern":"try {\n  writeClaudeMdToFolder(folderPath, newContent);\n} catch (error) {\n  if (/Folder does not exist/.test((error as Error).message)) {\n    // directory vanished mid-walk; skip silently or recreate and retry\n    return;\n  }\n  throw error;\n}","preventionTips":["Do not run claude-md context injection concurrently with git branch switches or clean operations.","Validate directory existence immediately before writing rather than relying on stale discovery results.","Treat a vanished folder as skip-able during traversal rather than a hard failure."],"tags":["filesystem","claude-md","io","race-condition"],"backgroundTag":null,"analyzedSha":"d768ba364302d12b76e69e4f021f0bb1d2d50ed6","analyzedAt":"2026-08-12T23:52:55.241Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}