Skip to content

Commit 35452cd

Browse files
authored
fix(amazonq): Updating logic for getWorkspaceRelativePath in shared src for /test (#6628)
## Problem - This will fix `Target file doesn't exist or is a directory.` issue in unit test generation. - If user opens project in workspace mode and try `/test` then in shared workspaceUtils: https://github.com/aws/aws-toolkit-vscode/blob/35502be238b1bf7c3ff73e44df8d077a6d32aa85/packages/core/src/shared/utilities/workspaceUtils.ts#L220-L236 returns the files starting from src but UTG does need values from child level. ## Solution Given above function with workspaceFolders as ``` [ { uri: { scheme: "file", authority: "", path: "/Users/Q/Downloads/TestingUTG/Sample/src", query: "", fragment: "", _formatted: "file:///Users/Q/Downloads/TestingUTG/Sample/src", _fsPath: "/Users/Q/Downloads/TestingUTG/Sample/src", }, name: "src", index: 0, }, { uri: { scheme: "file", authority: "", path: "/Users/laileni/Downloads/TestingUTG/Sample/src/Sample", query: "", fragment: "", _formatted: "file:///Users/Q/Downloads/TestingUTG/Sample/src/Sample", _fsPath: "/Users/Q/Downloads/TestingUTG/Sample/src/Sample", }, name: "📦 Sample", index: 1, }, ] ``` User requires the project/folder with path: "/Users/Q/Downloads/TestingUTG/Sample/src/Sample" but right now this gives the path from "/Users/Q/Downloads/TestingUTG/Sample/src" which incorrectly collects the files and this is causing failures in Unit test generation. Example: Expected Payload is ``` Sample - File A - Folder B etc... ``` but actual payload is ``` Sample - src -- Sample --- File A --- Folder B ``` - Sort workspace folders by path length (descending) to prioritize deeper paths ### TODO: - Need to enable this for entire Q --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent d6be742 commit 35452cd

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Amazon Q /test: Fixing the issue of target file does not exist."
4+
}

packages/core/src/codewhisperer/util/zipUtil.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,14 @@ export class ZipUtil {
410410
return
411411
}
412412

413-
const sourceFiles = await collectFiles(projectPaths, vscode.workspace.workspaceFolders as CurrentWsFolders, {
414-
maxSizeBytes: this.getProjectScanPayloadSizeLimitInBytes(),
415-
})
413+
const sourceFiles = await collectFiles(
414+
projectPaths,
415+
vscode.workspace.workspaceFolders as CurrentWsFolders,
416+
{
417+
maxSizeBytes: this.getProjectScanPayloadSizeLimitInBytes(),
418+
},
419+
useCase
420+
)
416421
for (const file of sourceFiles) {
417422
const projectName = path.basename(file.workspaceFolder.uri.fsPath)
418423
const zipEntryPath = this.getZipEntryPath(projectName, file.relativeFilePath)

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import fs from '../fs/fs'
2020
import { ChildProcess } from './processUtils'
2121
import { isWin } from '../vscode/env'
2222
import { maxRepoSizeBytes } from '../../amazonqFeatureDev/constants'
23+
import { FeatureUseCase } from '../../codewhisperer/models/constants'
2324

2425
type GitIgnoreRelativeAcceptor = {
2526
folderPath: string
@@ -223,12 +224,20 @@ export function getWorkspaceRelativePath(
223224
workspaceFolders?: readonly vscode.WorkspaceFolder[]
224225
} = {
225226
workspaceFolders: vscode.workspace.workspaceFolders,
226-
}
227+
},
228+
useCase?: FeatureUseCase
227229
): { relativePath: string; workspaceFolder: vscode.WorkspaceFolder } | undefined {
228230
if (!override.workspaceFolders) {
229231
return
230232
}
231-
for (const folder of override.workspaceFolders) {
233+
let folders = override.workspaceFolders
234+
if (useCase && useCase === FeatureUseCase.TEST_GENERATION) {
235+
// Sort workspace folders by path length (descending) to prioritize deeper paths
236+
// TODO: Need to enable this for entire Q
237+
folders = [...override.workspaceFolders].sort((a, b) => b.uri.fsPath.length - a.uri.fsPath.length)
238+
}
239+
240+
for (const folder of folders) {
232241
if (isInDirectory(folder.uri.fsPath, childPath)) {
233242
return { relativePath: path.relative(folder.uri.fsPath, childPath), workspaceFolder: folder }
234243
}
@@ -352,7 +361,8 @@ export async function collectFiles(
352361
excludeByGitIgnore?: boolean // default true
353362
excludePatterns?: string[] // default defaultExcludePatterns
354363
filterFn?: CollectFilesFilter
355-
}
364+
},
365+
useCase?: FeatureUseCase
356366
): Promise<CollectFilesResultItem[]> {
357367
const storage: Awaited<CollectFilesResultItem[]> = []
358368

@@ -398,7 +408,7 @@ export async function collectFiles(
398408
const files = excludeByGitIgnore ? await filterOutGitignoredFiles(rootPath, allFiles, false) : allFiles
399409

400410
for (const file of files) {
401-
const relativePath = getWorkspaceRelativePath(file.fsPath, { workspaceFolders })
411+
const relativePath = getWorkspaceRelativePath(file.fsPath, { workspaceFolders }, useCase)
402412
if (!relativePath) {
403413
continue
404414
}

0 commit comments

Comments
 (0)