Add fallback for if temp file can't be saved

This commit is contained in:
Saoud Rizwan
2024-09-03 10:16:44 -04:00
parent 8222808871
commit f1713e4b99

View File

@@ -813,17 +813,18 @@ export class ClaudeDev {
}
// Create a temporary file with the new content
const fileName = path.basename(absolutePath)
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "claude-dev-"))
const tempFilePath = path.join(tempDir, path.basename(absolutePath))
const tempFilePath = path.join(tempDir, fileName)
await fs.writeFile(tempFilePath, newContent)
vscode.commands.executeCommand(
"vscode.diff",
vscode.Uri.parse(`claude-dev-diff:${path.basename(absolutePath)}`).with({
vscode.Uri.parse(`claude-dev-diff:${fileName}`).with({
query: Buffer.from(originalContent).toString("base64"),
}),
vscode.Uri.file(tempFilePath),
`${path.basename(absolutePath)}: ${fileExists ? "Original ↔ Claude's Changes" : "New File"} (Editable)`
`${fileName}: ${fileExists ? "Original ↔ Claude's Changes" : "New File"} (Editable)`
)
let userResponse: {
@@ -858,6 +859,15 @@ export class ClaudeDev {
console.log("saving diff document")
await diffDocument.save()
}
// some users report a bug where the diff editor doesnt save the file automatically, so this is a workaround
if (!diffDocument) {
const savedResult = await vscode.workspace.save(vscode.Uri.file(tempFilePath))
// savedResult will be undefined if file was not found or couldn't be saved
if (!savedResult) {
// last resort is saving everything
await vscode.workspace.saveAll(false)
}
}
if (response !== "yesButtonTapped") {
await this.closeDiffViews()