{"record":{"id":"0a4926b80d6f7c06","repo":"thedotmack/claude-mem","slug":"could-not-read-configfile-error-instanceof-e","errorCode":null,"errorMessage":"Could not read ${configFile}: ${error instanceof Error ? error.message : String(error)}","messagePattern":"Could not read (.+?): (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/npx-cli/commands/install.ts","lineNumber":546,"sourceCode":"  if (!existsSync(claudeBinary)) return;\n\n  const currentPath = process.env.PATH ?? '';\n  const pathEntries = currentPath.split(':');\n  if (pathEntries.includes(claudeBinDir)) return;\n\n  const { path: configFile, shell } = detectShellConfigFile();\n  const binPathLiteral = '$HOME/.local/bin';\n  const exportLine = shell === 'fish'\n    ? `set -gx PATH ${claudeBinDir} $PATH`\n    : `export PATH=\"${binPathLiteral}:$PATH\"`;\n\n  let existing = '';\n  if (existsSync(configFile)) {\n    try {\n      existing = readFileSync(configFile, 'utf-8');\n    } catch (error: unknown) {\n      // [ANTI-PATTERN IGNORED]: the failure is already surfaced to the user via the interactive-aware log.warn wrapper below (p.log.warn in a TTY, console.warn otherwise); a raw console call here would double-print.\n      log.warn(`Could not read ${configFile}: ${error instanceof Error ? error.message : String(error)}`);\n    }\n  } else {\n    try {\n      mkdirSync(dirname(configFile), { recursive: true });\n    } catch {\n      // Best-effort directory creation.\n    }\n  }\n\n  if (existing.includes(claudeBinDir) || existing.includes(binPathLiteral)) {\n    log.info(`Claude Code PATH already configured in ${configFile}`);\n  } else {\n    try {\n      const trailing = existing.length === 0 || existing.endsWith('\\n') ? '' : '\\n';\n      const block = `${trailing}\\n# Added by claude-mem installer for Claude Code\\n${exportLine}\\n`;\n      writeFileSync(configFile, existing + block, 'utf-8');\n      log.success(`Added Claude Code to PATH in ${configFile}`);\n    } catch (error: unknown) {","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/npx-cli/commands/install.ts#L528-L564","documentation":"During PATH setup the installer reads the existing shell config file to append an export line. If readFileSync throws, this warning is logged and installation of the PATH entry continues with an empty base (potentially overwriting existing content in the write step). Marked as an accepted anti-pattern because the failure is already surfaced through the interactive-aware log.warn wrapper.","triggerScenarios":"readFileSync(configFile) throws on a file that existsSync confirmed — race with deletion, permission-denied, or the path being a directory/broken symlink.","commonSituations":"~/.zshrc or ~/.bashrc with restrictive permissions; file locked by sync tooling; config replaced by a directory; read-only filesystem (NFS/ro mount).","solutions":["Fix read permissions on the config file (chmod u+r).","Check the path is a regular file, not a directory or symlink.","Back up the file before re-running the installer if it contains valuable shell config.","Manually append the export line shown by the installer (`Run manually: echo '...' >> file`)."],"exampleFix":"// before\nexisting = readFileSync(configFile, 'utf-8');\n// after\ntry {\n  existing = readFileSync(configFile, 'utf-8');\n} catch (e) {\n  log.warn(`Could not read ${configFile}; aborting to avoid overwriting config.`);\n  log.info(`Run manually: echo '${exportLine}' >> ${configFile}`);\n  return;\n}","handlingStrategy":"try-catch","validationCode":"import { accessSync, constants, statSync } from 'fs';\nfunction isReadableFile(p: string): boolean {\n  try { return statSync(p).isFile() && (accessSync(p, constants.R_OK), true); } catch { return false; }\n}","typeGuard":"function isErrnoException(e: unknown): e is NodeJS.ErrnoException {\n  return e instanceof Error && typeof (e as NodeJS.ErrnoException).code === 'string';\n}","tryCatchPattern":"try {\n  existing = readFileSync(configFile, 'utf-8');\n} catch (error: unknown) {\n  log.warn(`Could not read ${configFile}: ${error instanceof Error ? error.message : String(error)}`);\n  // prefer aborting over continuing with existing='' to avoid clobbering\n}","preventionTips":["Back up shell config files before running installers that rewrite them.","Verify ownership/permissions of ~/.bashrc, ~/.zshrc etc. if installs run under sudo/CI.","Skip the auto-edit (print manual instructions) when the config is unreadable instead of proceeding with empty content.","Check the path is a regular file with statSync before readFileSync."],"tags":["installer","filesystem","path-setup","shell-config"],"backgroundTag":"file-read-failed","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"}