From dbcb52cda89695b47d5b0ce1b2e08401f0bb0896 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Tue, 2 Sep 2025 06:49:48 +0000 Subject: [PATCH] fix: convert recursive directory scanning to iterative approach to prevent stack overflow - Replace recursive scanDirectory function with iterative implementation using a queue - Prevents "Maximum call stack size exceeded" error when indexing large codebases (200k+ blocks) - Maintains all existing functionality and directory filtering logic - All existing tests pass without modification Fixes #7588 --- src/services/glob/list-files.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/services/glob/list-files.ts b/src/services/glob/list-files.ts index 7347515784..53e3ccee42 100644 --- a/src/services/glob/list-files.ts +++ b/src/services/glob/list-files.ts @@ -401,7 +401,20 @@ async function listFilteredDirectories( ignoreInstance, } - async function scanDirectory(currentPath: string, context: ScanContext): Promise { + // Use iterative approach with a queue to avoid stack overflow on deep directory structures + interface QueueItem { + path: string + context: ScanContext + } + + const queue: QueueItem[] = [{ path: absolutePath, context: initialContext }] + + while (queue.length > 0) { + const item = queue.shift() + if (!item) continue + + const { path: currentPath, context } = item + try { // List all entries in the current directory const entries = await fs.promises.readdir(currentPath, { withFileTypes: true }) @@ -461,7 +474,8 @@ async function listFilteredDirectories( isTargetDir: false, insideExplicitHiddenTarget: newInsideExplicitHiddenTarget, } - await scanDirectory(fullDirPath, newContext) + // Add to queue instead of recursive call + queue.push({ path: fullDirPath, context: newContext }) } } } @@ -471,9 +485,6 @@ async function listFilteredDirectories( } } - // Start scanning from the root directory - await scanDirectory(absolutePath, initialContext) - return directories }