Skip to content

Commit 97cbf9b

Browse files
refactor: remove 'glob' from CW (#4406)
## Problem: We cannot use 'glob' in browser mode since it breaks if not in nodejs ## Solution: Use `vscode.workspace.findFiles` which simply searches the entire workspace (or current folder if no workspace was selected) Note the previous comments about it not working, which was due to us needing to use a relative path since it searches the workspace/root folder only <!--- REMINDER: - Read CONTRIBUTING.md first. - Add test coverage for your changes. - Update the changelog using `npm run newChange`. - Link to related issues/commits. - Testing: how did you test your changes? - Screenshots --> ## License By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Signed-off-by: nkomonen <[email protected]>
1 parent e842ba7 commit 97cbf9b

File tree

1 file changed

+3
-16
lines changed
  • packages/toolkit/src/codewhisperer/util/supplementalContext

1 file changed

+3
-16
lines changed

packages/toolkit/src/codewhisperer/util/supplementalContext/utgUtils.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { glob } from 'glob'
76
import * as fs from 'fs-extra'
87
import * as path from 'path'
98
import * as vscode from 'vscode'
@@ -115,7 +114,7 @@ function generateSupplementalContextFromFocalFile(
115114
strategy: UtgStrategy,
116115
cancellationToken: vscode.CancellationToken
117116
): CodeWhispererSupplementalContextItem[] {
118-
const fileContent = fs.readFileSync(vscode.Uri.file(filePath!).fsPath, 'utf-8')
117+
const fileContent = fs.readFileSync(vscode.Uri.parse(filePath!).fsPath, 'utf-8')
119118

120119
// DO NOT send code chunk with empty content
121120
if (fileContent.trim().length === 0) {
@@ -190,9 +189,6 @@ async function findSourceFileByName(
190189
languageConfig: utgLanguageConfig,
191190
cancellationToken: vscode.CancellationToken
192191
): Promise<string | undefined> {
193-
const uri = editor.document.uri
194-
const workspaceFolder = vscode.workspace.getWorkspaceFolder(uri)
195-
const projectPath = workspaceFolder ? workspaceFolder.uri.fsPath : path.dirname(uri.fsPath)
196192
const testFileName = path.basename(editor.document.fileName)
197193

198194
let basenameSuffix = testFileName
@@ -222,25 +218,16 @@ async function findSourceFileByName(
222218

223219
throwIfCancelled(cancellationToken)
224220

225-
// TODO: vscode.workspace.findFiles is preferred but doesn't seems to be working for now.
226-
// TODO: Enable this later.
227-
//const sourceFiles =
228-
// await vscode.workspace.findFiles(`${projectPath}/**/${basenameSuffix}${languageConfig.extension}`);
229-
const sourceFiles = await globPromise(`${projectPath}/**/${basenameSuffix}${languageConfig.extension}`)
221+
const sourceFiles = await vscode.workspace.findFiles(`**/${basenameSuffix}${languageConfig.extension}`)
230222

231223
throwIfCancelled(cancellationToken)
232224

233225
if (sourceFiles.length > 0) {
234-
return sourceFiles[0]
226+
return sourceFiles[0].toString()
235227
}
236228
return undefined
237229
}
238230

239-
// TODO: Replace this by vscode.workspace.findFiles
240-
function globPromise(pattern: string): Promise<string[]> {
241-
return glob(pattern)
242-
}
243-
244231
function throwIfCancelled(token: vscode.CancellationToken): void | never {
245232
if (token.isCancellationRequested) {
246233
throw new ToolkitError(supplemetalContextFetchingTimeoutMsg, { cause: new CancellationError('timeout') })

0 commit comments

Comments
 (0)