fix(ide): prevent race condition when diff accepted through CLI (#7633)

This commit is contained in:
Shreya Keshive
2025-09-02 21:24:44 -07:00
committed by GitHub
parent cb255a1619
commit c9bd3ecf6a
2 changed files with 25 additions and 13 deletions
@@ -132,7 +132,7 @@ export class DiffManager {
/** /**
* Closes an open diff view for a specific file. * Closes an open diff view for a specific file.
*/ */
async closeDiff(filePath: string) { async closeDiff(filePath: string, suppressNotification = false) {
let uriToClose: vscode.Uri | undefined; let uriToClose: vscode.Uri | undefined;
for (const [uriString, diffInfo] of this.diffDocuments.entries()) { for (const [uriString, diffInfo] of this.diffDocuments.entries()) {
if (diffInfo.originalFilePath === filePath) { if (diffInfo.originalFilePath === filePath) {
@@ -145,6 +145,7 @@ export class DiffManager {
const rightDoc = await vscode.workspace.openTextDocument(uriToClose); const rightDoc = await vscode.workspace.openTextDocument(uriToClose);
const modifiedContent = rightDoc.getText(); const modifiedContent = rightDoc.getText();
await this.closeDiffEditor(uriToClose); await this.closeDiffEditor(uriToClose);
if (!suppressNotification) {
this.onDidChangeEmitter.fire( this.onDidChangeEmitter.fire(
IdeDiffClosedNotificationSchema.parse({ IdeDiffClosedNotificationSchema.parse({
jsonrpc: '2.0', jsonrpc: '2.0',
@@ -155,6 +156,7 @@ export class DiffManager {
}, },
}), }),
); );
}
return modifiedContent; return modifiedContent;
} }
return; return;
@@ -356,10 +356,20 @@ const createMcpServer = (diffManager: DiffManager) => {
description: '(IDE Tool) Close an open diff view for a specific file.', description: '(IDE Tool) Close an open diff view for a specific file.',
inputSchema: z.object({ inputSchema: z.object({
filePath: z.string(), filePath: z.string(),
suppressNotification: z.boolean().optional(),
}).shape, }).shape,
}, },
async ({ filePath }: { filePath: string }) => { async ({
const content = await diffManager.closeDiff(filePath); filePath,
suppressNotification,
}: {
filePath: string;
suppressNotification?: boolean;
}) => {
const content = await diffManager.closeDiff(
filePath,
suppressNotification,
);
const response = { content: content ?? undefined }; const response = { content: content ?? undefined };
return { return {
content: [ content: [