Skip to content

Commit

Permalink
fix(amazonq): increase fuzz factor for diff patch (#5048)
Browse files Browse the repository at this point in the history
* fix(amazonq): increase fuzz factor for diff patch

* fix tests
  • Loading branch information
ctlai95 committed May 24, 2024
1 parent be72449 commit 04bbdd2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
11 changes: 1 addition & 10 deletions packages/core/src/codewhisperer/commands/basicCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,9 @@ export const installAmazonQExtension = Commands.declare(
}
)

// Temporary workaround to avoid errors when pressing "Fix" multiple times from hover.
// There doesn't seem to be a way to close the hover or update the hover after interacting inside it.
// Keep track of which findingIds have already been fixed and exit early.
const fixedFindingIds = new Set()

export const applySecurityFix = Commands.declare(
'aws.amazonq.applySecurityFix',
() => async (issue: CodeScanIssue, filePath: string, source: Component) => {
if (fixedFindingIds.has(issue.findingId)) {
return
}
const [suggestedFix] = issue.suggestedFixes
if (!suggestedFix || !filePath) {
return
Expand All @@ -355,7 +347,7 @@ export const applySecurityFix = Commands.declare(
const document = await vscode.workspace.openTextDocument(filePath)
const fileContent = document.getText()
languageId = document.languageId
const updatedContent = applyPatch(fileContent, patch)
const updatedContent = applyPatch(fileContent, patch, { fuzzFactor: 4 })
if (!updatedContent) {
void vscode.window.showErrorMessage(CodeWhispererConstants.codeFixAppliedFailedMessage)
throw Error('Failed to get updated content from applying diff patch')
Expand All @@ -379,7 +371,6 @@ export const applySecurityFix = Commands.declare(
}

await closeSecurityIssueWebview(issue.findingId)
fixedFindingIds.add(issue.findingId)
} catch (err) {
getLogger().error(`Apply fix command failed. ${err}`)
applyFixTelemetryEntry.result = 'Failed'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ describe('CodeWhisperer-basicCommands', function () {
})

it('handles patch failure', async function () {
const textDocumentMock = createMockDocument()
const textDocumentMock = createMockDocument('first line\nsecond line\nthird line\nfourth line\nfifth line')

openTextDocumentMock.resolves(textDocumentMock)

Expand All @@ -619,7 +619,7 @@ describe('CodeWhisperer-basicCommands', function () {
targetCommand = testCommand(applySecurityFix)
codeScanIssue.suggestedFixes = [
{
code: '@@ -1,1 -1,1 @@\n-mock\n+line5',
code: "@@ -3,1 +3,1 @@\n fix\n that\n-doesn't\n+match\n the\n document",
description: 'dummy',
},
]
Expand All @@ -635,6 +635,30 @@ describe('CodeWhisperer-basicCommands', function () {
})
})

it('should allow up to 4 differing lines', async function () {
const textDocumentMock = createMockDocument('first line\nsecond line\nthird line\nfourth line\nfifth line')
openTextDocumentMock.resolves(textDocumentMock)
sandbox.stub(vscode.workspace, 'openTextDocument').value(openTextDocumentMock)
sandbox.stub(vscode.WorkspaceEdit.prototype, 'replace').value(replaceMock)
applyEditMock.resolves(true)
sandbox.stub(vscode.workspace, 'applyEdit').value(applyEditMock)
sandbox.stub(diagnosticsProvider, 'removeDiagnostic').value(removeDiagnosticMock)
sandbox.stub(SecurityIssueHoverProvider.instance, 'removeIssue').value(removeIssueMock)
sandbox.stub(SecurityIssueCodeActionProvider.instance, 'removeIssue').value(removeIssueMock)

targetCommand = testCommand(applySecurityFix)
codeScanIssue.suggestedFixes = [
{
code: '@@ -1,1 +1,1 @@\n first line\n changed\n-third line\n+changed\n foobar\n fifth line',
description: 'dummy',
},
]
await targetCommand.execute(codeScanIssue, 'test.py', 'webview')
assertTelemetry('codewhisperer_codeScanIssueApplyFix', {
result: 'Succeeded',
})
})

it('handles apply edit failure', async function () {
const fileName = 'sample.py'
const textDocumentMock = createMockDocument('first line\n second line\n fourth line', fileName)
Expand Down

0 comments on commit 04bbdd2

Please sign in to comment.