Skip to content

Commit 3b503b7

Browse files
authored
codewhisperer: remove build requirement (#4114)
* fix(codewhisperer): remove build requirement for java * add change log
1 parent f5971b8 commit 3b503b7

File tree

3 files changed

+26
-44
lines changed

3 files changed

+26
-44
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": "CodeWhisperer: Security scans for Java no longer require build artifacts"
4+
}

src/codewhisperer/util/dependencyGraph/javaDependencyGraph.ts

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,6 @@ export class JavaDependencyGraph extends DependencyGraph {
152152
})
153153
}
154154

155-
private async getFileLevelBuildFilePaths(uri: vscode.Uri, projectPath: string) {
156-
const filePath = uri.fsPath
157-
this._pickedSourceFiles.clear()
158-
this._pickedSourceFiles.add(filePath)
159-
this._totalSize = statSync(filePath).size
160-
const content = await readFileAsString(uri.fsPath)
161-
this._totalLines = content.split(DependencyGraphConstants.newlineRegex).length
162-
const javaStatement = this.extractStatement(content)
163-
this._buildFileRelativePaths.clear()
164-
const buildFileRelativePath = this.generateBuildFileRelativePath(uri, projectPath, javaStatement.packages)
165-
return this.generateOneBuildFilePaths(buildFileRelativePath)
166-
}
167-
168155
async searchDependency(uri: vscode.Uri): Promise<Set<string>> {
169156
const filePath = uri.fsPath
170157
const q: string[] = []
@@ -229,26 +216,6 @@ export class JavaDependencyGraph extends DependencyGraph {
229216
this._fetchedDirs.add(dirPath)
230217
}
231218

232-
private generateOneBuildFilePaths(buildFileRelativePath: string) {
233-
const oneBuildFilePaths: string[] = []
234-
this._outputDirs.forEach(dir => {
235-
const builFilePath = path.join(dir, buildFileRelativePath)
236-
if (existsSync(builFilePath) && oneBuildFilePaths.length === 0) {
237-
oneBuildFilePaths.push(builFilePath)
238-
}
239-
})
240-
this._outputNonStrictDirs.forEach(dir => {
241-
const builFilePath = path.join(dir, buildFileRelativePath)
242-
if (existsSync(builFilePath) && oneBuildFilePaths.length === 0) {
243-
oneBuildFilePaths.push(builFilePath)
244-
}
245-
})
246-
if (oneBuildFilePaths.length === 0) {
247-
throw new Error(`${buildFileRelativePath} is not found.`)
248-
}
249-
return oneBuildFilePaths
250-
}
251-
252219
private generateBuildFilePaths() {
253220
const buildFiles: Set<string> = new Set<string>()
254221
this._buildFileRelativePaths.forEach(relativePath => {
@@ -372,17 +339,13 @@ export class JavaDependencyGraph extends DependencyGraph {
372339
await sleep(1000)
373340
this.autoDetectClasspath(projectPath, projectName, DependencyGraphConstants.javaBuildExt)
374341
if (this._outputDirs.size === 0 && this._outputNonStrictDirs.size === 0) {
375-
throw new Error(`Classpath auto-detection failed.`)
376-
}
377-
let buildFiles: string[] = this.generateBuildFilePaths()
378-
if (buildFiles.length === 0) {
379-
getLogger().debug('Project level compile error:')
380-
buildFiles = await this.getFileLevelBuildFilePaths(uri, projectPath)
342+
getLogger().debug(`Classpath auto-detection failed.`)
381343
}
344+
const buildFiles: string[] = this.generateBuildFilePaths()
382345
const truncDirPath = this.getTruncDirPath(uri)
383-
getLogger().debug(`Picked source files:`)
346+
getLogger().debug(`Picked source files: [${[...this._pickedSourceFiles].join(', ')}]`)
384347
this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
385-
getLogger().debug(`Picked build artifacts:`)
348+
getLogger().debug(`Picked build artifacts: [${buildFiles}]`)
386349
this.copyFilesToTmpDir(buildFiles, truncDirPath)
387350
const totalBuildSize = this.getFilesTotalSize(Array.from(buildFiles.values()))
388351
const zipFilePath = this.zipDir(truncDirPath, CodeWhispererConstants.codeScanZipExt)

src/test/codewhisperer/util/dependencyGraph/javaDependencyGraph.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,37 @@ describe('javaDependencyGraph', function () {
5959
})
6060

6161
describe('generateTruncation', function () {
62+
let javaDependencyGraph: JavaDependencyGraph
6263
beforeEach(function () {
63-
sinon.stub(JavaDependencyGraph.prototype, <any>'generateBuildFilePaths').returns([appCodePath])
64+
javaDependencyGraph = new JavaDependencyGraph(languageId)
6465
})
6566
afterEach(function () {
6667
sinon.restore()
6768
})
6869
it('Should generate and return expected truncation', async function () {
69-
const javaDependencyGraph = new JavaDependencyGraph(languageId)
70-
sinon.stub(javaDependencyGraph, <any>'_outputDirs').value(new Set<string>('build'))
70+
sinon.stub(JavaDependencyGraph.prototype, <any>'generateBuildFilePaths').returns([appCodePath])
71+
sinon.stub(javaDependencyGraph, <any>'_outputDirs').value(new Set<string>(['build']))
7172
const truncation = await javaDependencyGraph.generateTruncation(vscode.Uri.parse(appCodePath))
7273
assert.ok(truncation.lines > 0)
7374
assert.ok(truncation.rootDir.includes(CodeWhispererConstants.codeScanTruncDirPrefix))
7475
assert.ok(truncation.srcPayloadSizeInBytes > 0)
7576
assert.ok(truncation.scannedFiles.size > 0)
7677
assert.ok(truncation.zipFilePath.includes(CodeWhispererConstants.codeScanTruncDirPrefix))
7778
})
79+
it('should not throw error if java compilation output path is not set', async () => {
80+
sinon.stub(JavaDependencyGraph.prototype, <any>'generateBuildFilePaths').returns([appCodePath])
81+
sinon.stub(javaDependencyGraph, <any>'_outputDirs').value(new Set<string>([]))
82+
await assert.doesNotReject(async () => {
83+
await javaDependencyGraph.generateTruncation(vscode.Uri.parse(appCodePath))
84+
})
85+
})
86+
it('should not throw error no build files are found', async () => {
87+
sinon.stub(JavaDependencyGraph.prototype, <any>'generateBuildFilePaths').returns([])
88+
sinon.stub(javaDependencyGraph, <any>'_outputDirs').value(new Set<string>(['build']))
89+
await assert.doesNotReject(async () => {
90+
await javaDependencyGraph.generateTruncation(vscode.Uri.parse(appCodePath))
91+
})
92+
})
7893
})
7994

8095
describe('isTestFile', () => {

0 commit comments

Comments
 (0)