From ebef364f63c9f0afdcc6293dddb9ff4d5b6ad540 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:56:10 +0800 Subject: [PATCH] fix(project): confirm trust before leaving the current project --- src/features/app/__tests__/windows.test.ts | 74 +++++++++++++++++++--- src/features/app/windows.ts | 11 ++-- src/platform/project.ts | 7 +- src/workbench/shellActions.ts | 8 +-- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/features/app/__tests__/windows.test.ts b/src/features/app/__tests__/windows.test.ts index a7c8d10853..41ea7078b2 100644 --- a/src/features/app/__tests__/windows.test.ts +++ b/src/features/app/__tests__/windows.test.ts @@ -27,8 +27,7 @@ const lightEditOpen = vi.fn(async (_path: string) => ({ status: "ok", data: useP const fsReadDir = vi.fn(async (_path: string) => ({ status: "ok", data: [] })); const openProject = vi.fn(async (path: string) => { const project = { root: path, name: path, trusted: true, isGit: false, settings: {} }; - useProject.setState({ project, lightEdit: false } as never); - return project; + return { status: "ok", data: project }; }); vi.mock("@/platform/ipc", async (orig) => { @@ -47,6 +46,7 @@ vi.mock("@/platform/ipc", async (orig) => { windowTakeLaunch: () => windowTakeLaunch(), windowCompleteLaunch: () => windowCompleteLaunch(), wslResolve: (p: string) => wslResolve(p), + openProject: (p: string) => openProject(p), lightEditOpen: (p: string) => lightEditOpen(p), fsReadDir: (p: string) => fsReadDir(p), }, @@ -54,7 +54,7 @@ vi.mock("@/platform/ipc", async (orig) => { }); vi.mock("@/platform/project", async (orig) => { const real = await orig(); - return { ...real, confirmLeaveProject: vi.fn(real.confirmLeaveProject), openProject: (p: string) => openProject(p) }; + return { ...real, confirmLeaveProject: vi.fn(real.confirmLeaveProject) }; }); const diffs: unknown[] = []; const openDiff = vi.fn(async (data: unknown) => void diffs.push(data)); @@ -70,7 +70,8 @@ const { openProjectWithPolicy, windowActions, } = await import("../windows"); -const { useProject, startupLaunchHandled, confirmLeaveProject } = await import("@/platform/project"); +const { useProject, startupLaunchHandled, confirmLeaveProject, provideLeaveProjectGuard } = + await import("@/platform/project"); const { provideEditorService, provideNotificationService } = await import("@/workbench/contributions"); const { isRemoteWindow, connectRemote } = await import("@/platform/remote"); const { ask } = await import("@/workbench/messages"); @@ -110,7 +111,7 @@ beforeEach(() => { projectTrustStatus.mockReset().mockResolvedValue(ok({ trusted: true })); workspaceAttach.mockReset(); workspaceUpdate.mockReset(); - projectCurrent.mockReset(); + projectCurrent.mockReset().mockImplementation(() => Promise.resolve(useProject.getState().project)); for (const f of [windowOpenProject, windowOpenFiles, windowTakeLaunch, wslResolve]) f.mockReset(); windowTakeLaunch.mockResolvedValue(null); windowCompleteLaunch.mockReset().mockResolvedValue(ok(null)); @@ -354,9 +355,7 @@ describe("launch", () => { const openedProject = deferred(); openProject.mockImplementationOnce(async (path) => { await openedProject.promise; - const state = projectState(path); - useProject.setState({ ...state, lightEdit: false } as never); - return state.project; + return { status: "ok", data: projectState(path).project }; }); let onLaunch: (() => void) | undefined; listen.mockImplementationOnce(async (_event, handler: () => void) => { @@ -751,6 +750,63 @@ describe("explicit project-window requests", () => { }); }); +describe("trust before leaving the current project", () => { + it.each([ + { name: "This Window from the open prompt", open: () => openProjectWithPolicy("/new", ["/new/file.txt"]) }, + { name: "a dropped folder", open: () => openPaths(["/new"]) }, + { name: "a folder launch", open: () => consumeLaunch() }, + ])("leaves the current project and its processes alone when declining $name", async ({ open }) => { + useProject.setState(projectState("/old") as never); + vi.mocked(ask).mockResolvedValueOnce({ button: "same", checked: false }); + windowOpenProject.mockResolvedValue(ok({ kind: "current" })); + windowTakeLaunch.mockResolvedValueOnce({ project: "/new", files: ["/new/file.txt"] }); + vi.mocked(confirmProjectTrust).mockRejectedValueOnce( + new IpcError({ kind: "cancelled", message: "Project opening cancelled" }), + ); + const leave = vi.fn(async () => true); + const dispose = provideLeaveProjectGuard(leave); + try { + await open(); + expect(confirmProjectTrust).toHaveBeenCalledWith("/new", "local"); + expect(leave).not.toHaveBeenCalled(); + expect(cancelClose).not.toHaveBeenCalled(); + expect(openProject).not.toHaveBeenCalled(); + expect(useProject.getState().project?.root).toBe("/old"); + expect(opened).toEqual([]); + expect(notify).not.toHaveBeenCalled(); + } finally { + dispose(); + } + }); + + it.each([false, true])("honors the leave decision after accepting trust (leave=%s)", async (leaves) => { + useProject.setState(projectState("/old") as never); + vi.mocked(ask).mockResolvedValueOnce({ button: "same", checked: false }); + windowOpenProject.mockResolvedValue(ok({ kind: "current" })); + const trust = deferred(); + vi.mocked(confirmProjectTrust).mockReturnValueOnce(trust.promise); + const leave = vi.fn(async () => leaves); + const dispose = provideLeaveProjectGuard(leave); + const opening = openProjectWithPolicy("/new", ["/new/file.txt"]); + try { + await vi.waitFor(() => expect(confirmProjectTrust).toHaveBeenCalledOnce()); + expect(leave).not.toHaveBeenCalled(); + expect(openProject).not.toHaveBeenCalled(); + expect(useProject.getState().project?.root).toBe("/old"); + trust.resolve(); + await opening; + expect(leave).toHaveBeenCalledWith(true); + expect(useProject.getState().project?.root).toBe(leaves ? "/new" : "/old"); + expect(opened).toEqual(leaves ? ["/new/file.txt (focus)"] : []); + expect(notify).not.toHaveBeenCalled(); + } finally { + trust.resolve(); + await opening; + dispose(); + } + }); +}); + describe("trust is asked before a new project window opens", () => { const openProjectPath = (arg: unknown) => windowActions() @@ -796,7 +852,7 @@ describe("trust is asked before a new project window opens", () => { windowOpenProject.mockResolvedValue(ok({ kind: "current" })); await openProjectInWindow("/p/b", false); expect(windowOpenProject).toHaveBeenCalledWith("/p/b", false, []); - expect(confirmProjectTrust).not.toHaveBeenCalled(); + expect(confirmProjectTrust).toHaveBeenCalledExactlyOnceWith("/p/b", "local"); expect(openProject).toHaveBeenCalledWith("/p/b"); }); }); diff --git a/src/features/app/windows.ts b/src/features/app/windows.ts index eff7843c8c..b5018e3994 100644 --- a/src/features/app/windows.ts +++ b/src/features/app/windows.ts @@ -57,13 +57,16 @@ async function openHere(path: string): Promise { if (!isRemoteWindow() && useProject.getState().project?.root === path) return true; // A folder of a WSL distro runs there, not as a network folder; the window then drops untitled buffers. const wsl = await commands.wslResolve(path).catch(() => null); - if (!(await confirmLeaveProject(!wsl))) return false; + let leaving = false; + const confirmLeave = async () => (leaving = await confirmLeaveProject(!wsl)); try { - if (wsl) await connectRemote(wsl.target, wsl.path); - else await openProject(path); + if (wsl) { + if (!(await confirmLeave())) return false; + await connectRemote(wsl.target, wsl.path); + } else await openProject(path, confirmLeave); return true; } catch (e) { - maybeEditorService()?.cancelClose?.(); + if (leaving) maybeEditorService()?.cancelClose?.(); openFailed(path, e); return false; } diff --git a/src/platform/project.ts b/src/platform/project.ts index 5e812c7428..8fddc92379 100644 --- a/src/platform/project.ts +++ b/src/platform/project.ts @@ -2,7 +2,7 @@ import { confirmProjectTrust } from "./projectTrust"; import { create } from "zustand"; import { setContext } from "./context"; -import { call, commands, type ProjectInfo, type Settings } from "./ipc"; +import { call, commands, IpcError, type ProjectInfo, type Settings } from "./ipc"; import { isVirtualUri, virtualContainer, virtualName, virtualParent } from "./virtualDocs"; interface ProjectState { @@ -59,8 +59,11 @@ export function startupLaunchHandled(): Promise { return (startupLaunch ?? Promise.resolve()).catch(() => {}); } -export async function openProject(path: string): Promise { +/** Checks trust before a caller that is switching projects asks to leave the current one. */ +export async function openProject(path: string, beforeOpen?: () => Promise): Promise { await confirmProjectTrust(path, "local"); + if (beforeOpen && !(await beforeOpen())) + throw new IpcError({ kind: "cancelled", message: "Project opening cancelled" }); useProject.setState({ opening: true, openingPath: path }); try { const project = await call(commands.openProject(path)); diff --git a/src/workbench/shellActions.ts b/src/workbench/shellActions.ts index 097555bd0b..fbaefd99ea 100644 --- a/src/workbench/shellActions.ts +++ b/src/workbench/shellActions.ts @@ -1,6 +1,6 @@ import { showBackgroundTasks } from "./StatusBar"; import { executeAction, getAction, registerActions } from "@/platform/actions"; -import { isTauri } from "@/platform/ipc"; +import { isCancelled, isTauri } from "@/platform/ipc"; import { closeProject, confirmLeaveProject, openProject, useProject } from "@/platform/project"; import { runSearchFieldCommand, showContextMenu } from "@/ui"; import { showCustomizeToolbar } from "./MainToolbarDialog"; @@ -32,11 +32,11 @@ export async function openProjectDialog() { await executeAction("file.openProjectPath", path); return; } - if (!(await confirmLeaveProject())) return; try { - await openProject(path); + await openProject(path, confirmLeaveProject); } catch (e) { - notify({ level: "error", title: "Could not open project", body: e instanceof Error ? e.message : String(e) }); + if (!isCancelled(e)) + notify({ level: "error", title: "Could not open project", body: e instanceof Error ? e.message : String(e) }); } }