Use globalStorage instead of temp dir for editing files; re-order file operations to fix windows lock error

This commit is contained in:
Saoud Rizwan
2024-09-03 18:16:18 -04:00
parent c209198b23
commit b7a06f0a7e

View File

@@ -814,10 +814,20 @@ export class ClaudeDev {
// Create a temporary file with the new content // Create a temporary file with the new content
const fileName = path.basename(absolutePath) const fileName = path.basename(absolutePath)
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "claude-dev-")) const globalStoragePath = this.providerRef.deref()?.context.globalStorageUri.fsPath
if (!globalStoragePath) {
throw new Error("Global storage uri is invalid")
}
const tempDir = path.join(globalStoragePath, "temp")
await fs.mkdir(tempDir, { recursive: true })
const tempFilePath = path.join(tempDir, fileName) const tempFilePath = path.join(tempDir, fileName)
await fs.writeFile(tempFilePath, newContent) await fs.writeFile(tempFilePath, newContent)
// const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "claude-dev-"))
// const tempFilePath = path.join(tempDir, fileName)
// await fs.writeFile(tempFilePath, newContent)
// await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFilePath), Buffer.from(newContent))
vscode.commands.executeCommand( vscode.commands.executeCommand(
"vscode.diff", "vscode.diff",
vscode.Uri.parse(`claude-dev-diff:${fileName}`).with({ vscode.Uri.parse(`claude-dev-diff:${fileName}`).with({
@@ -872,12 +882,7 @@ export class ClaudeDev {
if (response !== "yesButtonTapped") { if (response !== "yesButtonTapped") {
await this.closeDiffViews() await this.closeDiffViews()
// Clean up the temporary file // Clean up the temporary file
try {
await fs.rm(tempDir, { recursive: true, force: true }) await fs.rm(tempDir, { recursive: true, force: true })
} catch (error) {
// deleting temp file failed (seems to happen on some windows machines), which is okay since system will clean it up anyways
console.error(`Error deleting temporary directory: ${error}`)
}
if (response === "messageResponse") { if (response === "messageResponse") {
await this.say("user_feedback", text, images) await this.say("user_feedback", text, images)
return this.formatIntoToolResponse(await this.formatGenericToolFeedback(text), images) return this.formatIntoToolResponse(await this.formatGenericToolFeedback(text), images)
@@ -885,6 +890,8 @@ export class ClaudeDev {
return "The user denied this operation." return "The user denied this operation."
} }
await this.closeDiffViews()
// Read the potentially edited content from the temp file // Read the potentially edited content from the temp file
const editedContent = await fs.readFile(tempFilePath, "utf-8") const editedContent = await fs.readFile(tempFilePath, "utf-8")
if (!fileExists) { if (!fileExists) {
@@ -892,16 +899,11 @@ export class ClaudeDev {
} }
await fs.writeFile(absolutePath, editedContent) await fs.writeFile(absolutePath, editedContent)
// Clean up the temporary file
try {
await fs.rm(tempDir, { recursive: true, force: true })
} catch (error) {
console.error(`Error deleting temporary directory: ${error}`)
}
// Finish by opening the edited file in the editor // Finish by opening the edited file in the editor
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false }) await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false })
await this.closeDiffViews()
// Clean up the temporary file
await fs.rm(tempDir, { recursive: true, force: true })
if (editedContent !== newContent) { if (editedContent !== newContent) {
const diffResult = diff.createPatch(relPath, originalContent, editedContent) const diffResult = diff.createPatch(relPath, originalContent, editedContent)
@@ -944,14 +946,10 @@ export class ClaudeDev {
const tabs = vscode.window.tabGroups.all const tabs = vscode.window.tabGroups.all
.map((tg) => tg.tabs) .map((tg) => tg.tabs)
.flat() .flat()
.filter((tab) => { .filter(
if (tab.input instanceof vscode.TabInputTextDiff) { (tab) =>
const originalPath = (tab.input.original as vscode.Uri).toString() tab.input instanceof vscode.TabInputTextDiff && tab.input?.original?.scheme === "claude-dev-diff"
const modifiedPath = (tab.input.modified as vscode.Uri).toString() )
return originalPath.includes("claude-dev-") || modifiedPath.includes("claude-dev-")
}
return false
})
for (const tab of tabs) { for (const tab of tabs) {
await vscode.window.tabGroups.close(tab) await vscode.window.tabGroups.close(tab)