Skip to content

Commit b97f68e

Browse files
author
easonLiangWorldedtech
committed
fix(editor): fail the approved save when the document save reports false
saveChanges() awaited the document save and dropped the returned boolean, so a save that reported failure still ran the success bookkeeping and the caller was told the approved content persisted. Capture the awaited result and throw a typed DocumentSaveFailedError before the commit point and before any success bookkeeping, so the caller keeps its rollback and the existing failure handling runs. A true result behaves exactly as before; a save that rejects still propagates as before. This branch gates an on-commit callback after the save rather than a placeholder release, so the ordering promise is derived for this branch: the callback must not run when the save reported false. The statement is inherited from upstream code that predates the chain; this branch is the earliest carrier that is still open, so the shape is fixed here and the later branches port it forward re-derived. The regression doubles resolve an explicit false, so a double resolving undefined cannot pass by accident. Negative controls: reverting to a bare await reddens exactly the two save-false tests (2/88, the same two as the pre-fix production run); moving the throw after the callback reddens exactly the ordering test (1/88). Restored byte for byte, hash verified. See tracking item 6106182216.
1 parent 5ff7bea commit b97f68e

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ function isEnoent(error: unknown): boolean {
2626
return typeof error === "object" && error !== null && (error as { code?: unknown }).code === "ENOENT"
2727
}
2828

29+
/**
30+
* The approved document save reported failure: the content the user accepted never reached disk.
31+
* Typed so the caller's failure handling can tell a failed write from an unrelated teardown error,
32+
* and so a discarded boolean can never let the success bookkeeping run on a write that did not land.
33+
*/
34+
export class DocumentSaveFailedError extends Error {
35+
constructor(absolutePath: string) {
36+
super(
37+
`Failed to save ${absolutePath}: the document save reported failure; the approved content is not on disk.`,
38+
)
39+
this.name = "DocumentSaveFailedError"
40+
}
41+
}
42+
2943
export const DIFF_VIEW_URI_SCHEME = "cline-diff"
3044
export const DIFF_VIEW_LABEL_CHANGES = "Original ↔ Zoo's Changes"
3145

@@ -359,7 +373,14 @@ export class DiffViewProvider {
359373
const editedContent = updatedDocument.getText()
360374

361375
if (updatedDocument.isDirty) {
362-
await updatedDocument.save()
376+
const saved = await updatedDocument.save()
377+
// A save that reports false never landed: the buffer is still dirty and the file holds
378+
// the previous content (or nothing for a new file). Throw before the commit point and
379+
// before any success bookkeeping, so the caller keeps its rollback and the existing
380+
// failure handling runs. A save that rejects still propagates as before.
381+
if (!saved) {
382+
throw new DocumentSaveFailedError(absolutePath)
383+
}
363384
}
364385

365386
// The write is irreversible from here: a clean document already matches disk, and a saved

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as fs from "fs/promises"
2-
import { DiffViewProvider, DIFF_VIEW_URI_SCHEME, DIFF_VIEW_LABEL_CHANGES } from "../DiffViewProvider"
2+
import {
3+
DocumentSaveFailedError,
4+
DiffViewProvider,
5+
DIFF_VIEW_URI_SCHEME,
6+
DIFF_VIEW_LABEL_CHANGES,
7+
} from "../DiffViewProvider"
38
import * as vscode from "vscode"
49
import * as path from "path"
510
import delay from "delay"
@@ -996,6 +1001,64 @@ describe("DiffViewProvider", () => {
9961001
// to keep the rollback available, so signalling here would lose the write's debris.
9971002
expect(onCommit).not.toHaveBeenCalled()
9981003
})
1004+
1005+
it("throws a typed write failure before any success bookkeeping when the save reports false", async () => {
1006+
// The double resolves an explicit false, not undefined: a bare await would let a
1007+
// double resolving undefined pass by accident, so the reported-failure contract is
1008+
// pinned with a real false.
1009+
const save = vi.fn().mockResolvedValue(false)
1010+
// Structural double carrying only the members saveChanges() reaches at the save; the
1011+
// full TextEditor shape needs a live editor host - hence the assertion.
1012+
diffViewProvider["activeDiffEditor"] = {
1013+
document: { getText: vi.fn().mockReturnValue("new content"), isDirty: true, save },
1014+
} as unknown as vscode.TextEditor
1015+
const closeAllDiffViews = vi.fn().mockResolvedValue(undefined)
1016+
diffViewProvider["closeAllDiffViews"] = closeAllDiffViews
1017+
1018+
await expect(diffViewProvider.saveChanges(false, 0)).rejects.toThrow(DocumentSaveFailedError)
1019+
1020+
expect(save).toHaveBeenCalledTimes(1)
1021+
// Success bookkeeping must not run: the caller is told the write failed, so the diff
1022+
// views stay open and no result is stored for formatFileWriteResponse().
1023+
expect(closeAllDiffViews).not.toHaveBeenCalled()
1024+
expect(diffViewProvider.newProblemsMessage).toBeUndefined()
1025+
expect(diffViewProvider.userEdits).toBeUndefined()
1026+
})
1027+
1028+
it("does not signal the commit point when the save reports false", async () => {
1029+
const save = vi.fn().mockResolvedValue(false)
1030+
// Structural double, as in the rejected-save test above.
1031+
diffViewProvider["activeDiffEditor"] = {
1032+
document: { getText: vi.fn().mockReturnValue("new content"), isDirty: true, save },
1033+
} as unknown as vscode.TextEditor
1034+
diffViewProvider["closeAllDiffViews"] = vi.fn().mockResolvedValue(undefined)
1035+
const onCommit = vi.fn()
1036+
1037+
await expect(diffViewProvider.saveChanges(false, 0, onCommit)).rejects.toThrow(DocumentSaveFailedError)
1038+
1039+
// Ordering for this branch: the commit point stands in for the placeholder release the
1040+
// sibling branches gate on. A callback that ran here would set the caller's
1041+
// writeCommitted flag and stand the rollback down on a write that never landed.
1042+
expect(onCommit).not.toHaveBeenCalled()
1043+
})
1044+
1045+
it("behaves exactly as before when the save reports true", async () => {
1046+
const save = vi.fn().mockResolvedValue(true)
1047+
// Structural double, as in the rejected-save test above.
1048+
diffViewProvider["activeDiffEditor"] = {
1049+
document: { getText: vi.fn().mockReturnValue("new content"), isDirty: true, save },
1050+
} as unknown as vscode.TextEditor
1051+
const closeAllDiffViews = vi.fn().mockResolvedValue(undefined)
1052+
diffViewProvider["closeAllDiffViews"] = closeAllDiffViews
1053+
const onCommit = vi.fn()
1054+
1055+
const result = await diffViewProvider.saveChanges(false, 0, onCommit)
1056+
1057+
expect(save).toHaveBeenCalledTimes(1)
1058+
expect(onCommit).toHaveBeenCalledTimes(1)
1059+
expect(closeAllDiffViews).toHaveBeenCalledTimes(1)
1060+
expect(result.finalContent).toBe("new content")
1061+
})
9991062
})
10001063

10011064
describe("preEditScrollLine capture and restore", () => {

0 commit comments

Comments
 (0)