Skip to content

Commit 5012922

Browse files
authored
Fix recursive directory scanning in @ mention "Add Folder" functionality (#4863)
1 parent 8370591 commit 5012922

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

src/services/glob/list-files.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,42 @@ async function listFilteredDirectories(
190190
gitignorePatterns: string[],
191191
): Promise<string[]> {
192192
const absolutePath = path.resolve(dirPath)
193-
194-
try {
195-
// List all entries in the directory
196-
const entries = await fs.promises.readdir(absolutePath, { withFileTypes: true })
197-
198-
// Filter for directories only
199-
const directories = entries
200-
.filter((entry) => entry.isDirectory())
201-
.filter((entry) => {
202-
return shouldIncludeDirectory(entry.name, recursive, gitignorePatterns)
203-
})
204-
.map((entry) => path.join(absolutePath, entry.name))
205-
206-
// Format directory paths with trailing slash
207-
return directories.map((dir) => (dir.endsWith("/") ? dir : `${dir}/`))
208-
} catch (err) {
209-
console.error(`Error listing directories: ${err}`)
210-
return [] // Return empty array on error
193+
const directories: string[] = []
194+
195+
async function scanDirectory(currentPath: string): Promise<void> {
196+
try {
197+
// List all entries in the current directory
198+
const entries = await fs.promises.readdir(currentPath, { withFileTypes: true })
199+
200+
// Filter for directories only, excluding symbolic links to prevent circular traversal
201+
for (const entry of entries) {
202+
if (entry.isDirectory() && !entry.isSymbolicLink()) {
203+
const dirName = entry.name
204+
const fullDirPath = path.join(currentPath, dirName)
205+
206+
// Check if this directory should be included
207+
if (shouldIncludeDirectory(dirName, recursive, gitignorePatterns)) {
208+
// Add the directory to our results (with trailing slash)
209+
const formattedPath = fullDirPath.endsWith("/") ? fullDirPath : `${fullDirPath}/`
210+
directories.push(formattedPath)
211+
212+
// If recursive mode and not a ignored directory, scan subdirectories
213+
if (recursive && !isDirectoryExplicitlyIgnored(dirName)) {
214+
await scanDirectory(fullDirPath)
215+
}
216+
}
217+
}
218+
}
219+
} catch (err) {
220+
// Silently continue if we can't read a directory
221+
console.warn(`Could not read directory ${currentPath}: ${err}`)
222+
}
211223
}
224+
225+
// Start scanning from the root directory
226+
await scanDirectory(absolutePath)
227+
228+
return directories
212229
}
213230

214231
/**
@@ -295,7 +312,8 @@ function formatAndCombineResults(files: string[], directories: string[], limit:
295312
const allPaths = [...directories, ...files]
296313

297314
// Deduplicate paths (a directory might appear in both lists)
298-
const uniquePaths = [...new Set(allPaths)]
315+
const uniquePathsSet = new Set(allPaths)
316+
const uniquePaths = Array.from(uniquePathsSet)
299317

300318
// Sort to ensure directories come first, followed by files
301319
uniquePaths.sort((a: string, b: string) => {

0 commit comments

Comments
 (0)