{"record":{"id":"dd07bac34a7603cd","repo":"davila7/claude-code-templates","slug":"warning-could-not-read-agents-directory-agentsd","errorCode":null,"errorMessage":"Warning: Could not read agents directory ${agentsDir}:","messagePattern":"Warning: Could not read agents directory (.+?):","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"cli-tool/src/analytics.js","lineNumber":1769,"sourceCode":"   * @returns {Promise<Array>} Array of agent objects\n   */\n  async loadAgentsFromDirectory(agentsDir, level, projectName = null) {\n    const agents = [];\n    \n    try {\n      const files = await fs.readdir(agentsDir);\n      \n      for (const file of files) {\n        if (file.endsWith('.md')) {\n          const filePath = path.join(agentsDir, file);\n          const agentData = await this.parseAgentFile(filePath, level, projectName);\n          if (agentData) {\n            agents.push(agentData);\n          }\n        }\n      }\n    } catch (error) {\n      console.warn(chalk.yellow(`Warning: Could not read agents directory ${agentsDir}:`, error.message));\n    }\n    \n    return agents;\n  }\n\n  /**\n   * Parse agent markdown file\n   * @param {string} filePath - Path to agent file\n   * @param {string} level - 'user' or 'project'\n   * @param {string} projectName - Name of project (if project level)\n   * @returns {Promise<Object|null>} Agent object or null if parsing failed\n   */\n  async parseAgentFile(filePath, level, projectName = null) {\n    try {\n      const content = await fs.readFile(filePath, 'utf8');\n      const stats = await fs.stat(filePath);\n      \n      // Parse YAML frontmatter","sourceCodeStart":1751,"sourceCodeEnd":1787,"githubUrl":"https://github.com/davila7/claude-code-templates/blob/a0851ed10c7c60463dac8cfaaca124cf32d5804d/cli-tool/src/analytics.js#L1751-L1787","documentation":"A warning from agent discovery: fs operations over the .claude/agents directory threw (permissions, missing dir edge case not previously guarded, EMFILE). The method returns whatever agents it collected so far.","triggerScenarios":"readdir/readFile over ~/.claude/agents (or project agents dir) failing with EACCES/EMFILE/ENOENT racing directory deletion while iterating agent files.","commonSituations":"Agents directory owned by another user; too many open files during a large scan; directory removed mid-scan by the CLI itself.","solutions":["Check permissions on the agents directory shown in the message","Raise ulimit -n if you see EMFILE during large scans","Re-run the scan — transient races self-heal","Ignore; partial agent list is returned"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Guard before scanning\nconst st = await fs.stat(agentsDir).catch(() => null);\nif (!st?.isDirectory()) return agents; // nothing to scan","typeGuard":null,"tryCatchPattern":"try { /* scan agents dir */ }\ncatch (error) {\n  console.warn(`Could not read agents directory ${agentsDir}:`, error.message);\n  return agents; // partial results\n}","preventionTips":["Stat the directory before iterating","Run as the owning user; avoid sudo-launched scans","Raise ulimit -n for large-scale scans"],"tags":["file-io","agents","analytics","permissions"],"backgroundTag":"directory-read-failed","analyzedSha":"a0851ed10c7c60463dac8cfaaca124cf32d5804d","analyzedAt":"2026-08-28T14:11:56.058Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}