{"record":{"id":"51eb5606b8c9f2c3","repo":"JuliusBrussee/caveman","slug":"mcp-config-path-contains-dangling-symlink-ancest","errorCode":null,"errorMessage":"MCP config path contains dangling symlink ${ancestor}; refusing mutation","messagePattern":"MCP config path contains dangling symlink (.+?); refusing mutation","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":11237,"sourceCode":"  agent: \"kilo\" | \"qwen\";\n  path: string;\n  before: Buffer | null;\n  beforeMode: number;\n  after: Buffer | null;\n  changed: boolean;\n};\n\nfunction canonicalMcpConfigPath(path: string): string {\n  const absolute = normalize(resolve(path));\n  const missing: string[] = [];\n  let ancestor = absolute;\n  while (true) {\n    try {\n      return normalize(join(realpathSync(ancestor), ...missing));\n    } catch {\n      try {\n        if (lstatSync(ancestor).isSymbolicLink()) {\n          throw new Error(`MCP config path contains dangling symlink ${ancestor}; refusing mutation`);\n        }\n      } catch (error) {\n        if ((error as NodeJS.ErrnoException).code !== \"ENOENT\") throw error;\n      }\n      const parent = dirname(ancestor);\n      if (parent === ancestor) return absolute;\n      missing.unshift(basename(ancestor));\n      ancestor = parent;\n    }\n  }\n}\n\nfunction ownedMcpConfigPlan(\n  agent: \"kilo\" | \"qwen\",\n  path: string,\n  loaded: { bytes: Buffer | null; exists: boolean },\n  after: Buffer | null,\n): OwnedMcpConfigPlan {","sourceCodeStart":11219,"sourceCodeEnd":11255,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L11219-L11255","documentation":"When resolving the real path of an MCP config file whose ancestors may not exist yet, the CLI walks up the directory chain with realpathSync. If an existing ancestor is a dangling symlink (realpath fails and lstat says it is a symlink), the CLI refuses to mutate the config to avoid creating files in an unintended location or following a broken link.","triggerScenarios":"Adding/removing an MCP server whose config path contains a symlinked ancestor whose target no longer exists — realpathSync throws ENOENT, lstat confirms a symlink, and this error is thrown. Also rethrown for non-ENOENT lstat errors at that ancestor.","commonSituations":"Dotfile managers symlinking ~/.config/agent to a target that was deleted or on an unmounted volume; restoring a home directory where link targets moved; deleted external drives holding symlinked config trees.","solutions":["Find the dangling link: `find <parent-dir> -xtype l` and either remove it or re-point it to an existing target","Recreate the symlink's target directory if it should exist, then re-run the MCP command","Replace the symlink with a real directory (copy contents, remove link) if the indirection is no longer needed"],"exampleFix":"# before\n$ ls -l ~/.config/kilo\nkilo -> /mnt/old-drive/kilo  # target gone\n# after\n$ rm ~/.config/kilo && mkdir -p ~/.config/kilo","handlingStrategy":"validation","validationCode":"import { realpathSync, lstatSync } from \"node:fs\";\nconst dir = dirname(configPath);\ntry { realpathSync(dir); } catch {\n  let a = dir;\n  while (a !== dirname(a)) {\n    try { if (lstatSync(a).isSymbolicLink()) throw new Error(`dangling symlink at ${a}`); } catch (e) { if ((e as any).code !== \"ENOENT\") throw e; }\n    a = dirname(a);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await addMcpServer(name, config);\n} catch (e) {\n  if ((e as Error).message.includes(\"dangling symlink\")) {\n    const link = (e as Error).message.match(/symlink (\\S+);/)?.[1];\n    console.error(`Remove or repair the link at ${link}: find $(dirname ${link}) -xtype l`);\n  } else throw e;\n}","preventionTips":["Periodically run `find ~/.config -xtype l` to detect dangling symlinks","Ensure symlink targets (external drives, other volumes) are mounted before CLI use","Prefer real directories over symlinked config roots for agent configs","Restore backups with links re-pointed to valid targets"],"tags":["symlink","filesystem","mcp"],"backgroundTag":"file-not-found","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}