Skip to content

Commit d030e04

Browse files
author
s3-1395
committed
fix(diff-view): reopen the preview tabs a discarded stream evicted
restorePreviewTabs() is defined at DiffViewProvider.ts:1141 and reached from saveChanges() (:403) and revertChanges() (:917), but not from discardUnapprovedStream(), so a denied stream left every preview tab VS Code evicted to open the diff closed out: reset() clears snapshotPreviewTabs right after the method returns and nothing reopened what the snapshot held. Call it from the discard path once the artifact sweep succeeds and before the outcome is reported, including on the path where the diff editor is already gone, and skip it when cleanupFailure is set - the placeholder is then still on disk and still owned, and the snapshot is the only record the retry path has of what is left to remove. restorePreviewTabs() reports its own per-tab failures (fs.access miss -> continue, showText Document rejection -> console.error), so awaiting it here cannot mask editorFailure. Coverage: the reopened-tab test fails before this commit with "Number of calls: 0" and passes after it; the sweep-failure test pins the skip and passed before it as well. 135 passed (135 tests) after, 134 passed / 1 failed before.
1 parent d65ab55 commit d030e04

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,17 @@ export class DiffViewProvider {
786786
console.error("Error removing abandoned write_to_file artifacts:", error)
787787
}
788788

789+
// Restore any preview tabs the diff evicted, reconstructing the user's prior
790+
// not-yet-edited tab state, the way saveChanges() and revertChanges() do at their own ends.
791+
// The discard path never reached this: reset() clears snapshotPreviewTabs right after, so a
792+
// denied stream left every tab VS Code evicted to open the diff gone for the session.
793+
// Skipped when the sweep failed - the placeholder is then still on disk and still owned, the
794+
// snapshot is the only record the retry path has of what is left to remove, and reopening
795+
// tabs on top of a half-removed artifact would report a workspace that is not restored.
796+
// restorePreviewTabs() reports its own per-tab failures, so it cannot mask editorFailure.
797+
if (!cleanupFailure) {
798+
await this.restorePreviewTabs()
799+
}
789800
if (editorFailure) {
790801
throw editorFailure instanceof Error ? editorFailure : new Error(String(editorFailure))
791802
}

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,6 +2899,75 @@ describe("DiffViewProvider", () => {
28992899
vi.useRealTimers()
29002900
}
29012901
})
2902+
it("reopens a preview tab the diff evicted before reset() drops the snapshot", async () => {
2903+
// Review row wM62emT7. restorePreviewTabs() is defined at DiffViewProvider.ts:1135 and
2904+
// called from saveChanges() and revertChanges(), but the discard path never reached it, so a
2905+
// denied stream left every preview tab VS Code evicted to open the diff gone: reset() clears
2906+
// snapshotPreviewTabs and nothing reopens what it held.
2907+
const revealRange = vi.fn()
2908+
vi.mocked(vscode.window.showTextDocument).mockResolvedValue(makeTextEditor({ revealRange }))
2909+
// No tab in any group: the captured file is one the diff evicted.
2910+
Object.defineProperty(vscode.window.tabGroups, "all", {
2911+
get: (): vscode.TabGroup[] => [],
2912+
configurable: true,
2913+
})
2914+
Object.assign(diffViewProvider, {
2915+
relPath: "mock-target-file.ts",
2916+
activeDiffEditor: undefined,
2917+
editType: "modify",
2918+
createdDirs: [],
2919+
placeholderPath: undefined,
2920+
snapshotPreviewTabs: [
2921+
{
2922+
uri: makeSnapshotUri(`${mockCwd}/mock-preview-tab.ts`),
2923+
scrollLine: 7,
2924+
viewColumn: vscode.ViewColumn.One,
2925+
},
2926+
],
2927+
})
2928+
await diffViewProvider.discardUnapprovedStream()
2929+
await diffViewProvider.reset()
2930+
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
2931+
{ fsPath: `${mockCwd}/mock-preview-tab.ts`, scheme: "file" },
2932+
{ preview: true, preserveFocus: true, viewColumn: vscode.ViewColumn.One },
2933+
)
2934+
expect(revealRange).toHaveBeenCalledTimes(1)
2935+
expect(revealRange).toHaveBeenCalledWith(
2936+
expect.objectContaining({ start: { line: 7, character: 0 } }),
2937+
vscode.TextEditorRevealType.AtTop,
2938+
)
2939+
expect(diffViewProvider["snapshotPreviewTabs"]).toEqual([])
2940+
})
2941+
2942+
it("leaves the snapshot in place when the artifact sweep fails so the retry can finish it", async () => {
2943+
// The restoration is skipped, not merely ordered late, when the sweep failed: the placeholder
2944+
// is still on disk and owned, and reopening the evicted tabs would report a restored
2945+
// workspace on top of a half-removed artifact while the snapshot is the only record the
2946+
// retry path has of what is left to remove.
2947+
Object.defineProperty(vscode.window.tabGroups, "all", {
2948+
get: (): vscode.TabGroup[] => [],
2949+
configurable: true,
2950+
})
2951+
vi.mocked(vscode.window.showTextDocument).mockResolvedValue(makeTextEditor())
2952+
Object.assign(diffViewProvider, {
2953+
relPath: "mock-target-file.ts",
2954+
activeDiffEditor: undefined,
2955+
editType: "create",
2956+
createdDirs: [],
2957+
placeholderPath: mockTargetPath,
2958+
snapshotPreviewTabs: [
2959+
{
2960+
uri: makeSnapshotUri(`${mockCwd}/mock-preview-tab.ts`),
2961+
scrollLine: 7,
2962+
viewColumn: vscode.ViewColumn.One,
2963+
},
2964+
],
2965+
})
2966+
vi.mocked(fs.unlink).mockRejectedValueOnce(new Error("EPERM: operation not permitted"))
2967+
await expect(diffViewProvider.discardUnapprovedStream()).rejects.toThrow("EPERM")
2968+
expect(vscode.window.showTextDocument).not.toHaveBeenCalled()
2969+
expect(diffViewProvider["snapshotPreviewTabs"]).toHaveLength(1)
2970+
})
29022971
it("removes the placeholder and created directories even when open() failed before activeDiffEditor was assigned", async () => {
29032972
// open() creates the directories (line 130) and the empty placeholder (line 134)
29042973
// BEFORE openDiffEditor() assigns activeDiffEditor (line 173). If that call rejects

0 commit comments

Comments
 (0)