Skip to content

Commit 5f72ec9

Browse files
authored
fix(amazonq): always use projectName in zipEntryPath #6182
## Problem Projects with a nested folder of the same name does not scan properly. For example if the project name is `foo`, and there exists a folder `foo/foo/` then the zip artifact only contains the inner `foo/` folder. ## Solution Always include the project name in the zip artifact instead of conditionally adding it.
1 parent ef926e4 commit 5f72ec9

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Code Review: Fixed a bug where projects with repeated path names did not scan properly."
4+
}

packages/amazonq/test/unit/codewhisperer/util/zipUtil.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import { ToolkitError } from 'aws-core-vscode/shared'
1414
import { LspClient } from 'aws-core-vscode/amazonq'
1515
import { fs } from 'aws-core-vscode/shared'
1616
import path from 'path'
17+
import JSZip from 'jszip'
1718

1819
describe('zipUtil', function () {
1920
const workspaceFolder = getTestWorkspaceFolder()
2021
const appRoot = join(workspaceFolder, 'java11-plain-maven-sam-app')
2122
const appCodePath = join(appRoot, 'HelloWorldFunction', 'src', 'main', 'java', 'helloworld', 'App.java')
23+
const appCodePathWithRepeatedProjectName = join(workspaceFolder, 'workspaceFolder', 'App.java')
2224

2325
describe('getProjectPaths', function () {
2426
it('Should return the correct project paths', function () {
@@ -112,6 +114,30 @@ describe('zipUtil', function () {
112114
)
113115
assert.equal(zipMetadata2.lines, zipMetadata.lines + 1)
114116
})
117+
118+
it('should handle path with repeated project name for file scan', async function () {
119+
const zipMetadata = await zipUtil.generateZip(
120+
vscode.Uri.file(appCodePathWithRepeatedProjectName),
121+
CodeAnalysisScope.FILE_ON_DEMAND
122+
)
123+
124+
const zipFileData = await fs.readFileBytes(zipMetadata.zipFilePath)
125+
const zip = await JSZip.loadAsync(zipFileData)
126+
const files = Object.keys(zip.files)
127+
assert.ok(files.includes(join('workspaceFolder', 'workspaceFolder', 'App.java')))
128+
})
129+
130+
it('should handle path with repeated project name for project scan', async function () {
131+
const zipMetadata = await zipUtil.generateZip(
132+
vscode.Uri.file(appCodePathWithRepeatedProjectName),
133+
CodeAnalysisScope.PROJECT
134+
)
135+
136+
const zipFileData = await fs.readFileBytes(zipMetadata.zipFilePath)
137+
const zip = await JSZip.loadAsync(zipFileData)
138+
const files = Object.keys(zip.files)
139+
assert.ok(files.includes(join('workspaceFolder', 'workspaceFolder', 'App.java')))
140+
})
115141
})
116142

117143
describe('generateZipTestGen', function () {

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,8 @@ export class ZipUtil {
140140
return zipFilePath
141141
}
142142

143-
protected getZipEntryPath(projectName: string, relativePath: string, useCase?: ZipUseCase) {
144-
// Workspaces with multiple folders have the folder names as the root folder,
145-
// but workspaces with only a single folder don't. So prepend the workspace folder name
146-
// if it is not present.
147-
if (useCase === ZipUseCase.TEST_GENERATION) {
148-
return path.join(projectName, relativePath)
149-
}
150-
return relativePath.split('/').shift() === projectName ? relativePath : path.join(projectName, relativePath)
143+
protected getZipEntryPath(projectName: string, relativePath: string) {
144+
return path.join(projectName, relativePath)
151145
}
152146

153147
/**
@@ -422,7 +416,8 @@ export class ZipUtil {
422416
this.getProjectScanPayloadSizeLimitInBytes()
423417
)
424418
for (const file of sourceFiles) {
425-
const zipEntryPath = this.getZipEntryPath(file.workspaceFolder.name, file.relativeFilePath, useCase)
419+
const projectName = path.basename(file.workspaceFolder.uri.fsPath)
420+
const zipEntryPath = this.getZipEntryPath(projectName, file.relativeFilePath)
426421

427422
if (ZipConstants.knownBinaryFileExts.includes(path.extname(file.fileUri.fsPath))) {
428423
if (useCase === ZipUseCase.TEST_GENERATION) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package helloworld;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.URL;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.stream.Collectors;
10+
11+
import com.amazonaws.services.lambda.runtime.Context;
12+
import com.amazonaws.services.lambda.runtime.RequestHandler;
13+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
14+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
15+
16+
/**
17+
* Handler for requests to Lambda function.
18+
*/
19+
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
20+
21+
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
22+
Map<String, String> headers = new HashMap<>();
23+
headers.put("Content-Type", "application/json");
24+
headers.put("X-Custom-Header", "application/json");
25+
26+
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
27+
.withHeaders(headers);
28+
try {
29+
final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
30+
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);
31+
32+
return response
33+
.withStatusCode(200)
34+
.withBody(output);
35+
} catch (IOException e) {
36+
return response
37+
.withBody("{}")
38+
.withStatusCode(500);
39+
}
40+
}
41+
42+
private String getPageContents(String address) throws IOException{
43+
URL url = new URL(address);
44+
try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
45+
return br.lines().collect(Collectors.joining(System.lineSeparator()));
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)