Skip to content

Commit dbcb52c

Browse files
committed
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
1 parent 2e59347 commit dbcb52c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/services/glob/list-files.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,20 @@ async function listFilteredDirectories(
401401
ignoreInstance,
402402
}
403403

404-
async function scanDirectory(currentPath: string, context: ScanContext): Promise<void> {
404+
// Use iterative approach with a queue to avoid stack overflow on deep directory structures
405+
interface QueueItem {
406+
path: string
407+
context: ScanContext
408+
}
409+
410+
const queue: QueueItem[] = [{ path: absolutePath, context: initialContext }]
411+
412+
while (queue.length > 0) {
413+
const item = queue.shift()
414+
if (!item) continue
415+
416+
const { path: currentPath, context } = item
417+
405418
try {
406419
// List all entries in the current directory
407420
const entries = await fs.promises.readdir(currentPath, { withFileTypes: true })
@@ -461,7 +474,8 @@ async function listFilteredDirectories(
461474
isTargetDir: false,
462475
insideExplicitHiddenTarget: newInsideExplicitHiddenTarget,
463476
}
464-
await scanDirectory(fullDirPath, newContext)
477+
// Add to queue instead of recursive call
478+
queue.push({ path: fullDirPath, context: newContext })
465479
}
466480
}
467481
}
@@ -471,9 +485,6 @@ async function listFilteredDirectories(
471485
}
472486
}
473487

474-
// Start scanning from the root directory
475-
await scanDirectory(absolutePath, initialContext)
476-
477488
return directories
478489
}
479490

0 commit comments

Comments
 (0)