{"record":{"id":"5c08174428df5a8e","repo":"yamadashy/repomix","slug":"failed-to-search-for-empty-directories-error-in","errorCode":null,"errorMessage":"Failed to search for empty directories: ${error instanceof Error ? error.message : String(error)}","messagePattern":"Failed to search for empty directories: (.+?)","errorType":"exception","errorClass":"RepomixError","httpStatus":null,"severity":"error","filePath":"src/core/output/outputGenerate.ts","lineNumber":417,"sourceCode":"        error instanceof Error ? { cause: error } : undefined,\n      );\n    }\n  } else if (config.output.directoryStructure && config.output.includeEmptyDirectories) {\n    // Reuse pre-computed emptyDirPaths from the initial searchFiles call when available,\n    // avoiding a redundant full directory scan.\n    if (emptyDirPaths) {\n      directoryPathsForTree = emptyDirPaths;\n    } else {\n      try {\n        const results = await Promise.all(rootDirs.map((rootDir) => deps.searchFiles(rootDir, config)));\n        const merged = results.flatMap((result, index) => {\n          const rootDir = rootDirs[index];\n          if (!rootDir) return [];\n          return result.emptyDirPaths.map((emptyDirPath) => toOutputDisplayPath(rootDir, emptyDirPath, index));\n        });\n        directoryPathsForTree = [...new Set(merged)].sort();\n      } catch (error) {\n        throw new RepomixError(\n          `Failed to search for empty directories: ${error instanceof Error ? error.message : String(error)}`,\n          error instanceof Error ? { cause: error } : undefined,\n        );\n      }\n    }\n  }\n\n  // Generate tree string - use multi-root format if filePathsByRoot is provided\n  // generateTreeStringWithRoots handles single root case internally\n  let treeString: string;\n  if (filePathsByRoot) {\n    treeString = generateTreeStringWithRoots(filePathsByRoot, directoryPathsForTree);\n  } else {\n    // Fallback for when root info is not available\n    treeString = generateTreeString(filePathsForTree, directoryPathsForTree);\n  }\n\n  return {","sourceCodeStart":399,"sourceCodeEnd":435,"githubUrl":"https://github.com/yamadashy/repomix/blob/f465ad909315a22120636baf03fa5e28701a50cb/src/core/output/outputGenerate.ts#L399-L435","documentation":"When `directoryStructure` and `includeEmptyDirectories` are enabled, Repomix searches for empty directories per root dir and merges their display paths; any exception in that search is rethrown as a RepomixError with the original error as `cause`. It means the empty-directory scan for the tree output failed.","triggerScenarios":"Enabling `output.includeEmptyDirectories: true` with `directoryStructure: true` and the empty-dir search call (executed per rootDir, mapped through toOutputDisplayPath) throws — typically an underlying filesystem walk error.","commonSituations":"Running against a directory tree with unreadable subdirectories; errors on exotic filesystems (Windows reserved names, network shares); race where a directory disappears mid-scan.","solutions":["Inspect `error.cause` for the underlying scan failure.","Restore read permissions on directories being scanned.","Disable `includeEmptyDirectories` if the empty-dir tree is not essential.","Re-run to rule out a transient race (directory deleted during scan)."],"exampleFix":"// before (repomix.json)\n\"directoryStructure\": true,\n\"includeEmptyDirectories\": true\n// after (workaround when scan fails on unreadable dirs)\n\"directoryStructure\": true,\n\"includeEmptyDirectories\": false","handlingStrategy":"fallback","validationCode":"// Check tree readability before enabling empty-dir scan\nimport fs from 'node:fs/promises';\nawait fs.access(config.cwd, fs.constants.R_OK);","typeGuard":"null","tryCatchPattern":"try {\n  await pack({ ...config, output: { ...config.output, includeEmptyDirectories: true } });\n} catch (e) {\n  if (e instanceof RepomixError && e.message.startsWith('Failed to search for empty directories')) {\n    console.error('empty-dir scan failed:', e.cause ?? e);\n    await pack({ ...config, output: { ...config.output, includeEmptyDirectories: false } });\n  } else throw e;\n}","preventionTips":["Only enable includeEmptyDirectories when the tree display actually needs it.","Fix permissions on scanned directories beforehand.","Don't mutate the directory tree while packing is running.","Inspect e.cause for the underlying scan error before retrying."],"tags":["filesystem","empty-directories","wrapped-error"],"backgroundTag":"permission-denied","analyzedSha":"f465ad909315a22120636baf03fa5e28701a50cb","analyzedAt":"2026-08-29T01:27:42.024Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}