Skip to content

Commit 25cf13b

Browse files
author
easonLiangWorldedtech
committed
fix(diff-view): identify this provider's tab by URI, not by basename
Two tasks can edit files with the same name in different directories, so a label match on the basename could close the other task's clean tab. A label-identified tab now counts only when its own URI points at this provider's target.
1 parent 1b74c27 commit 25cf13b

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -912,16 +912,17 @@ export class DiffViewProvider {
912912
if (tab.isDirty) {
913913
return false
914914
}
915-
if (
916-
tab.input instanceof vscode.TabInputTextDiff &&
917-
tab.input.original.scheme === DIFF_VIEW_URI_SCHEME &&
918-
path.resolve(tab.input.modified.fsPath) === target
919-
) {
920-
return true
915+
if (tab.input instanceof vscode.TabInputTextDiff) {
916+
return path.resolve(tab.input.modified.fsPath) === target
921917
}
922918
// A diff tab for a file that was already open is identified by its label
923-
// rather than by the URI scheme, so the label has to be matched too.
919+
// rather than by the URI scheme. A basename alone cannot tell two tasks in
920+
// different directories apart, so the label only counts when the tab's own
921+
// URI points at this provider's target.
922+
const uri = (tab.input as { uri?: { fsPath?: string } })?.uri
924923
return (
924+
typeof uri?.fsPath === "string" &&
925+
path.resolve(uri.fsPath) === target &&
925926
typeof tab.label === "string" &&
926927
tab.label.startsWith(`${path.basename(target)}: ${DIFF_VIEW_LABEL_CHANGES}`)
927928
)

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ vi.mock("../../../utils/fs", () => ({
6464

6565
// Mock path
6666
vi.mock("path", () => ({
67-
resolve: vi.fn((cwd, relPath) => `${cwd}/${relPath}`),
67+
resolve: vi.fn((cwd: string, relPath?: string) => (relPath === undefined ? cwd : `${cwd}/${relPath}`)),
6868
isAbsolute: vi.fn((p: string) => p.startsWith("/")),
6969
basename: vi.fn((path) => path.split("/").pop()),
7070
dirname: vi.fn((path) => path.split("/").slice(0, -1).join("/") || "/"),
@@ -883,6 +883,57 @@ describe("DiffViewProvider", () => {
883883

884884
expect(closedTabs).toEqual([ownTab])
885885
})
886+
887+
it("leaves another task's tab when the same basename sits in another directory", async () => {
888+
// Two tasks can edit files with the same name. Matching by basename alone
889+
// would close the other task's clean tab, so the tab's own URI has to decide.
890+
const ownTab = {
891+
input: {
892+
constructor: { name: "TabInputTextDiff" },
893+
original: { scheme: DIFF_VIEW_URI_SCHEME },
894+
modified: { fsPath: `${mockCwd}/test.ts` },
895+
},
896+
label: `test.ts: ${DIFF_VIEW_LABEL_CHANGES} (Editable)`,
897+
isDirty: false,
898+
}
899+
const sameNameOtherDir = {
900+
input: {
901+
constructor: { name: "TabInputTextDiff" },
902+
original: { scheme: DIFF_VIEW_URI_SCHEME },
903+
modified: { fsPath: "/other-cwd/test.ts" },
904+
},
905+
label: `test.ts: ${DIFF_VIEW_LABEL_CHANGES} (Editable)`,
906+
isDirty: false,
907+
}
908+
// A pre-opened file's tab is identified by its label, so the URI check is the
909+
// only thing that can tell the two apart here.
910+
const labelOnlyOtherDir = {
911+
input: { uri: { fsPath: "/other-cwd/test.ts" } },
912+
label: `test.ts: ${DIFF_VIEW_LABEL_CHANGES} (Editable)`,
913+
isDirty: false,
914+
}
915+
const labelOnlyOwn = {
916+
input: { uri: { fsPath: `${mockCwd}/test.ts` } },
917+
label: `test.ts: ${DIFF_VIEW_LABEL_CHANGES} (Editable)`,
918+
isDirty: false,
919+
}
920+
for (const tab of [ownTab, sameNameOtherDir]) {
921+
Object.setPrototypeOf(tab.input, vscode.TabInputTextDiff.prototype)
922+
}
923+
Object.defineProperty(vscode.window.tabGroups, "all", {
924+
get: () => [{ tabs: [ownTab, sameNameOtherDir, labelOnlyOtherDir, labelOnlyOwn] }],
925+
configurable: true,
926+
})
927+
const closedTabs: unknown[] = []
928+
vi.mocked(vscode.window.tabGroups.close).mockImplementation((tab) => {
929+
closedTabs.push(tab)
930+
return Promise.resolve(true)
931+
})
932+
933+
await diffViewProvider["closeOwnDiffView"](path.join(mockCwd, "test.ts"))
934+
935+
expect(closedTabs).toEqual([ownTab, labelOnlyOwn])
936+
})
886937
})
887938

888939
it("reset() closes only this provider's tab, not another task's", async () => {

0 commit comments

Comments
 (0)