Skip to content

Commit cbb6024

Browse files
committed
minor fix
1 parent d12bb5f commit cbb6024

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

packages/core/src/codewhispererChat/tools/listDirectory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export class ListDirectory {
2929
if (!this.fsPath || this.fsPath.trim().length === 0) {
3030
throw new Error('Path cannot be empty.')
3131
}
32+
if (this.maxDepth !== undefined && this.maxDepth < 0) {
33+
throw new Error('MaxDepth cannot be negative.')
34+
}
3235

3336
const sanitized = sanitizePath(this.fsPath)
3437
this.fsPath = sanitized

packages/core/src/shared/utilities/workspaceUtils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ export function formatListing(name: string, fileType: vscode.FileType, fullPath:
693693
* You can either pass a custom callback or rely on the default `formatListing`.
694694
*
695695
* @param dirUri The folder to begin traversing
696-
* @param maxDepth Maximum depth to descend (0 => just this folder, if it's missing -> recursively)
696+
* @param maxDepth Maximum depth to descend (0 => just this folder, if it's missing => recursively)
697697
* @param customFormatCallback Optional. If given, it will override the default line-formatting
698698
*/
699699
export async function readDirectoryRecursively(
@@ -702,8 +702,7 @@ export async function readDirectoryRecursively(
702702
customFormatCallback?: (name: string, fileType: vscode.FileType, fullPath: string) => string
703703
): Promise<string[]> {
704704
const logger = getLogger()
705-
maxDepth = maxDepth ?? -1
706-
const depthDescription = maxDepth < 0 ? 'unlimited' : maxDepth
705+
const depthDescription = maxDepth === undefined ? 'unlimited' : maxDepth
707706
logger.info(`Reading directory: ${dirUri.fsPath} to max depth: ${depthDescription}`)
708707

709708
const queue: Array<{ uri: vscode.Uri; depth: number }> = [{ uri: dirUri, depth: 0 }]
@@ -713,7 +712,7 @@ export async function readDirectoryRecursively(
713712

714713
while (queue.length > 0) {
715714
const { uri, depth } = queue.shift()!
716-
if (maxDepth >= 0 && depth > maxDepth) {
715+
if (maxDepth !== undefined && depth > maxDepth) {
717716
logger.info(`Skipping directory: ${uri.fsPath} (depth ${depth} > max ${maxDepth})`)
718717
continue
719718
}
@@ -731,7 +730,7 @@ export async function readDirectoryRecursively(
731730
const childUri = vscode.Uri.joinPath(uri, name)
732731
results.push(formatter(name, fileType, childUri.fsPath))
733732

734-
if (fileType === vscode.FileType.Directory && (maxDepth < 0 || depth < maxDepth)) {
733+
if (fileType === vscode.FileType.Directory && (maxDepth === undefined || depth < maxDepth)) {
735734
queue.push({ uri: childUri, depth: depth + 1 })
736735
}
737736
}

packages/core/src/test/codewhispererChat/tools/listDirectory.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ describe('ListDirectory Tool', () => {
1919
await assert.rejects(listDirectory.validate(), /Path cannot be empty/i, 'Expected an error about empty path')
2020
})
2121

22+
it('throws if maxDepth is negative', async () => {
23+
const listDirectory = new ListDirectory({ path: '~', maxDepth: -1 })
24+
await assert.rejects(
25+
listDirectory.validate(),
26+
/MaxDepth cannot be negative/i,
27+
'Expected an error about negative maxDepth'
28+
)
29+
})
30+
2231
it('lists directory contents', async () => {
2332
await testFolder.mkdir('subfolder')
2433
await testFolder.write('fileA.txt', 'fileA content')

0 commit comments

Comments
 (0)