Skip to content

Commit c722999

Browse files
committed
fIx(amazon q): Serve original content in memory instead of using temp files
1 parent 8ebc3be commit c722999

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

packages/amazonq/src/lsp/chat/messages.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ import {
7070
import { v4 as uuidv4 } from 'uuid'
7171
import * as vscode from 'vscode'
7272
import * as path from 'path'
73-
import * as os from 'os'
7473
import { Disposable, LanguageClient, Position, TextDocumentIdentifier } from 'vscode-languageclient'
7574
import { AmazonQChatViewProvider } from './webviewProvider'
7675
import {
@@ -85,7 +84,6 @@ import {
8584
} from 'aws-core-vscode/codewhisperer'
8685
import { AmazonQPromptSettings, messages, openUrl, isTextEditor, globals, setContext } from 'aws-core-vscode/shared'
8786
import { DefaultAmazonQAppInitContext, messageDispatcher, referenceLogText } from 'aws-core-vscode/amazonq'
88-
import { fs } from 'aws-core-vscode/shared'
8987
import { telemetry } from 'aws-core-vscode/telemetry'
9088
import { isValidResponseError } from './error'
9189
import { decryptResponse, encryptRequest } from '../encryption'
@@ -150,7 +148,6 @@ export function registerMessageListeners(
150148
encryptionKey: Buffer
151149
) {
152150
const chatStreamTokens = new Map<string, CancellationTokenSource>() // tab id -> token
153-
const tempDiffFiles = new Set<string>() // track temp files for cleanup
154151

155152
// Keep track of pending chat options to send when webview UI is ready
156153
const pendingChatOptions = languageClient.initializeResult?.awsServerCapabilities?.chatOptions
@@ -656,47 +653,39 @@ export function registerMessageListeners(
656653
languageClient.onNotification(openFileDiffNotificationType.method, async (params: OpenFileDiffParams) => {
657654
const currentFileUri = vscode.Uri.parse(params.originalFileUri)
658655
const originalContent = params.originalFileContent ?? ''
656+
const fileName = path.basename(currentFileUri.fsPath)
659657

660-
// Clean up any existing temp files first
661-
for (const tempFile of tempDiffFiles) {
662-
try {
663-
await fs.delete(tempFile)
664-
} catch {}
665-
}
666-
tempDiffFiles.clear()
658+
// Use custom scheme to avoid adding to recent files
659+
const originalFileUri = vscode.Uri.parse(`amazonq-diff:${fileName}_original_${Date.now()}`)
667660

668-
// Create a temporary file with original content
669-
const fileName = path.basename(currentFileUri.fsPath)
670-
const tempFileName = `${path.parse(fileName).name}_original_${Date.now()}${path.extname(fileName)}`
671-
const tempFilePath = path.join(os.tmpdir(), tempFileName)
661+
// Register content provider for the custom scheme
662+
const disposable = vscode.workspace.registerTextDocumentContentProvider('amazonq-diff', {
663+
provideTextDocumentContent: () => originalContent,
664+
})
672665

673666
try {
674-
await fs.writeFile(tempFilePath, originalContent)
675-
const tempFileUri = vscode.Uri.file(tempFilePath)
676-
tempDiffFiles.add(tempFilePath)
677-
678-
// Open diff view with temp file (left) vs current file (right)
667+
// Open diff view with custom scheme URI (left) vs current file (right)
679668
await vscode.commands.executeCommand(
680669
'vscode.diff',
681-
tempFileUri,
670+
originalFileUri,
682671
currentFileUri,
683672
`${vscode.workspace.asRelativePath(currentFileUri)} (Original ↔ Current, Editable)`,
684673
{ preview: false }
685674
)
686675

687-
// Clean up temp file when diff view is closed
688-
const disposable = vscode.window.onDidChangeVisibleTextEditors(() => {
676+
// Clean up content provider when diff view is closed
677+
const cleanupDisposable = vscode.window.onDidChangeVisibleTextEditors(() => {
689678
const isDiffViewOpen = vscode.window.visibleTextEditors.some(
690-
(editor) => editor.document.uri.toString() === tempFileUri.toString()
679+
(editor) => editor.document.uri.toString() === originalFileUri.toString()
691680
)
692-
if (!isDiffViewOpen && tempDiffFiles.has(tempFilePath)) {
693-
fs.delete(tempFilePath).catch(() => {})
694-
tempDiffFiles.delete(tempFilePath)
681+
if (!isDiffViewOpen) {
695682
disposable.dispose()
683+
cleanupDisposable.dispose()
696684
}
697685
})
698686
} catch (error) {
699-
languageClient.error(`[VSCode Client] Failed to create temp file for diff: ${error}`)
687+
disposable.dispose()
688+
languageClient.error(`[VSCode Client] Failed to open diff view: ${error}`)
700689
}
701690
})
702691

0 commit comments

Comments
 (0)