{"record":{"id":"417e227976a731ad","repo":"affaan-m/ECC","slug":"failed-to-create-directory-dirpath-err-mes","errorCode":null,"errorMessage":"Failed to create directory '${dirPath}': ${err.message}","messagePattern":"Failed to create directory '(.+?)': (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/lib/utils.js","lineNumber":102,"sourceCode":"function getTempDir() {\n  return os.tmpdir();\n}\n\n/**\n * Ensure a directory exists (create if not)\n * @param {string} dirPath - Directory path to create\n * @returns {string} The directory path\n * @throws {Error} If directory cannot be created (e.g., permission denied)\n */\nfunction ensureDir(dirPath) {\n  try {\n    if (!fs.existsSync(dirPath)) {\n      fs.mkdirSync(dirPath, { recursive: true });\n    }\n  } catch (err) {\n    // EEXIST is fine (race condition with another process creating it)\n    if (err.code !== 'EEXIST') {\n      throw new Error(`Failed to create directory '${dirPath}': ${err.message}`);\n    }\n  }\n  return dirPath;\n}\n\n/**\n * Get current date in YYYY-MM-DD format\n */\nfunction getDateString() {\n  const now = new Date();\n  const year = now.getFullYear();\n  const month = String(now.getMonth() + 1).padStart(2, '0');\n  const day = String(now.getDate()).padStart(2, '0');\n  return `${year}-${month}-${day}`;\n}\n\n/**\n * Get current time in HH:MM format","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/scripts/lib/utils.js#L84-L120","documentation":"ensureDir creates a directory (and parents) if it does not exist, wrapping fs.mkdirSync failures. EEXIST is tolerated as a benign race with another process; any other mkdir error (permissions, invalid path, ENOTDIR, EROFS) is rethrown with the path and the underlying OS message.","triggerScenarios":"Calling ensureDir (directly or via callers like writeSessionLease, saveAliases, appendRunRecord) when the path's parent is a file (ENOTDIR), the process lacks write permission on the parent (EACCES), the path segment is malformed, or the filesystem is read-only.","commonSituations":"A file exists where a directory component of dirPath is expected; running the script as a user without write access to ~/.claude or the project dir; pointing the CLI at a read-only mounted volume or a path with stray characters/quotes.","solutions":["Read the wrapped OS error (e.g. EACCES, ENOTDIR, EROFS) in the message to identify the cause.","If EACCES: fix permissions on the parent directory (chown/chmod) or run with appropriate rights.","If ENOTDIR: remove or rename the file that occupies a path segment where a directory is required.","If EROFS: write to a writable location instead of a read-only mount."],"exampleFix":"// before: a regular file blocks the path\nls -la ~/.claude/state   # it's a file\n// after\nrm ~/.claude/state && mkdir -p ~/.claude/state","handlingStrategy":"try-catch","validationCode":"function canCreateDir(dirPath) {\n  const parent = path.dirname(dirPath);\n  if (!fs.existsSync(parent)) return canCreateDir(parent);\n  const st = fs.statSync(parent);\n  if (!st.isDirectory()) return false;      // ENOTDIR\n  try { fs.accessSync(parent, fs.constants.W_OK); return true; }\n  catch { return false; }                    // EACCES/EROFS\n}","typeGuard":"const isDir = (p) => { try { return fs.statSync(p).isDirectory(); } catch { return false; } };","tryCatchPattern":"try {\n  ensureDir(dirPath);\n} catch (err) {\n  if (err.message.includes('EACCES')) console.error(`No write permission for ${dirPath} — chown/chmod or pick another location`);\n  else if (err.message.includes('ENOTDIR')) console.error(`A file is blocking ${dirPath} — remove/rename it`);\n  else throw err;\n}","preventionTips":["Never place a regular file at a path segment the tool uses as a directory","Run the tool as a user with write access to the project/home config directories","Avoid read-only mounts as output roots","Quote paths in shell invocations to avoid stray characters in dirPath"],"tags":["filesystem","permissions","directory"],"backgroundTag":"mkdir-permission-denied","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}