From 69fca0aaa39dc904579d8c90cfc331f36e6375f6 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 6 Dec 2026 04:11:37 +0800 Subject: [PATCH] fix(diff): compare the Project view's two selected files when Compare Files runs from Find Action (#8287) Run from Find Action, Compare Files got no argument and asked to select two files even with two selected. It now falls back to the Project view's selection like Compare Directories and rejects a selected directory. --- .../__tests__/directoryCompareActions.test.ts | 22 +++++++++++++++++++ src/features/vcs/compare.tsx | 5 +++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/features/vcs/__tests__/directoryCompareActions.test.ts b/src/features/vcs/__tests__/directoryCompareActions.test.ts index a85d3eca75..dfff1fc10e 100644 --- a/src/features/vcs/__tests__/directoryCompareActions.test.ts +++ b/src/features/vcs/__tests__/directoryCompareActions.test.ts @@ -60,6 +60,28 @@ describe("directory compare actions", () => { expect(mocks.notify).toHaveBeenCalledWith(expect.objectContaining({ title: "Select two directories to compare" })); }); + it("compares the two files selected in the Project view from Find Action", async () => { + const unselect = provideProjectViewSelection(() => ["/p/a.txt", "/p/b.txt"]); + try { + await getAction("diff.compareFiles")!.run({ ctx: { projectOpen: true } }); + expect(mocks.openDiff).toHaveBeenCalledWith(expect.objectContaining({ path: "/p/b.txt" })); + expect(mocks.notify).not.toHaveBeenCalled(); + } finally { + unselect(); + } + }); + + it("does not compare a selected directory as a file", async () => { + const unselect = provideProjectViewSelection(() => ["/p/left", "/p/b.txt"]); + try { + await getAction("diff.compareFiles")!.run({ ctx: { projectOpen: true } }); + expect(mocks.openDiff).not.toHaveBeenCalled(); + expect(mocks.notify).toHaveBeenCalledWith(expect.objectContaining({ title: "Select two files to compare" })); + } finally { + unselect(); + } + }); + it("uses the native folder picker for Compare With on a local directory", async () => { mocks.pick.mockResolvedValue("/p/right"); await getAction("diff.compareWith")!.run({ ctx: {}, arg: { path: "/p/left", directory: true } }); diff --git a/src/features/vcs/compare.tsx b/src/features/vcs/compare.tsx index c13c6e43a6..7f494b0d11 100644 --- a/src/features/vcs/compare.tsx +++ b/src/features/vcs/compare.tsx @@ -361,8 +361,9 @@ export function registerCompareActions(): () => void { icon: "diff", when: "projectOpen", run: async ({ arg }) => { - const paths = argPaths(arg).filter((p) => !p.endsWith("/")); - if (paths.length !== 2) return info("Select two files to compare"); + const paths = (arg === undefined ? projectViewSelection() : argPaths(arg)).filter((p) => !p.endsWith("/")); + if (paths.length !== 2 || (await isDirectory(paths[0]!)) || (await isDirectory(paths[1]!))) + return info("Select two files to compare"); await openVcsDiff({ ...compareFilesData(paths[0]!, paths[1]!), swappable: true }); }, },