Skip to content

Commit eaa3d2f

Browse files
committed
Guarantee patch application reliability by explicitly syncing the temporary patch file to disk before invoking git apply
1 parent f72eb3f commit eaa3d2f

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/handlers/diff-handler.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export const store_original_file_states = async (
146146
return original_states
147147
}
148148

149-
// Helper function to extract content from patch for preview
150149
export const extract_content_from_patch = (
151150
patch_content: string,
152151
original_content: string
@@ -166,7 +165,6 @@ export const extract_content_from_patch = (
166165
}
167166
}
168167

169-
// Ensures all target files are closed before applying patches
170168
async function close_files_in_all_editor_groups(
171169
file_paths: string[],
172170
workspace_path: string
@@ -209,7 +207,6 @@ async function close_files_in_all_editor_groups(
209207
return [...new Map(closed_files.map((item) => [item.fsPath, item])).values()]
210208
}
211209

212-
// Reopens files that were closed before patch application
213210
async function reopen_closed_files(closed_files: vscode.Uri[]): Promise<void> {
214211
for (const uri of closed_files) {
215212
try {
@@ -225,7 +222,6 @@ async function reopen_closed_files(closed_files: vscode.Uri[]): Promise<void> {
225222
}
226223
}
227224

228-
// Opens, formats, and saves a list of files.
229225
async function process_modified_files(
230226
file_paths: string[],
231227
workspace_path: string
@@ -464,11 +460,15 @@ export const apply_git_patch = async (
464460
workspace_path
465461
)
466462

467-
await vscode.workspace.fs.writeFile(
468-
vscode.Uri.file(temp_file),
469-
Buffer.from(patch_content)
470-
)
471-
await new Promise((r) => setTimeout(r, 100))
463+
// Write patch file and ensure it's synced to disk before git apply reads it
464+
const patch_buffer = Buffer.from(patch_content)
465+
const fd = await fs.promises.open(temp_file, 'w')
466+
try {
467+
await fd.write(patch_buffer)
468+
await fd.sync() // Ensure data is flushed to disk
469+
} finally {
470+
await fd.close()
471+
}
472472

473473
let last_error: any = null
474474
let success = false
@@ -547,7 +547,6 @@ export const apply_git_patch = async (
547547
}
548548
}
549549

550-
// Cleanup and return
551550
if (success) {
552551
await process_modified_files(file_paths, workspace_path)
553552
await reopen_closed_files(closed_files)

‎packages/vscode/src/commands/apply-chat-response-command/response-processor.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ export const process_chat_response = async (
153153
]
154154
}
155155

156-
console.log('xxx', clipboard_items)
157-
158156
if (clipboard_items.some((item) => item.type == 'diff')) {
159157
const patches = clipboard_items.filter(
160158
(item): item is DiffItem => item.type == 'diff'

0 commit comments

Comments
 (0)