From 738dc12ef7dd224983cb2f2ef85249f756051ebf Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 18 Nov 2026 19:26:48 +0800 Subject: [PATCH] fix(libraries): clear the previous project's External Libraries on switching (#4276) The External Libraries node and last complete result were kept across a project switch until the new discovery answered, so the new project showed and filled in the old project's libraries. Refreshing for another project now clears them first. Fixes #4194. --- src/features/libraries/index.ts | 12 ++++++++++-- src/features/libraries/refresh.test.ts | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/features/libraries/index.ts b/src/features/libraries/index.ts index 6ce4d7603..0defb6e69 100644 --- a/src/features/libraries/index.ts +++ b/src/features/libraries/index.ts @@ -149,19 +149,27 @@ export function activate(): () => void { /** The last complete result: what a refresh shows for ecosystems that haven't reported yet. */ let settled: LibraryGroup[] = []; let request: RequestId | undefined; + /** The project whose libraries are shown (and `settled`). */ + let shownFor: string | null = null; const clear = () => { disposeRoot(); disposeReadOnly(); disposeRoot = disposeReadOnly = () => {}; shown = ""; settled = []; + shownFor = null; }; const refresh = () => { const id = ++seq; if (request !== undefined) void commands.cancelRequest(request).catch(() => {}); request = undefined; - if (!useProject.getState().project || useProject.getState().project?.trusted === false || !isTauri()) - return clear(); + const project = useProject.getState().project; + if (!project || project.trusted === false || !isTauri()) return clear(); + // Another project's libraries must not show (or fill in) while this one's are discovered. + if (shownFor !== project.root) { + clear(); + shownFor = project.root; + } let collected: LibraryGroup[] = []; const show = (groups: LibraryGroup[]) => { if (id !== seq) return; diff --git a/src/features/libraries/refresh.test.ts b/src/features/libraries/refresh.test.ts index 1e38d4720..6554fa0ab 100644 --- a/src/features/libraries/refresh.test.ts +++ b/src/features/libraries/refresh.test.ts @@ -4,6 +4,8 @@ import type { LibraryGroup } from "@/bindings"; const results: LibraryGroup[][] = []; /** Batches the next request streams before it answers. */ const streamed: LibraryGroup[][] = []; +/** The next request never answers (discovery still running). */ +let hang = false; vi.mock("@tauri-apps/api/core", () => ({ Channel: class { @@ -19,6 +21,7 @@ vi.mock("@/bindings", () => ({ vi.mock("@/platform/ipc", () => ({ call: (request: [string, { onmessage: (v: unknown) => void }]) => { for (const batch of streamed.splice(0)) request[1].onmessage(batch); + if (hang) return new Promise(() => {}); return Promise.resolve(results.shift() ?? []); }, isCancelled: () => false, @@ -67,4 +70,20 @@ describe("External Libraries refresh", () => { dispose(); expect(useProjectViewRoots.getState().roots.has("externalLibraries")).toBe(false); }); + + it("drops the previous project's libraries while the next project's are discovered", async () => { + const project = (root: string) => ({ root, trusted: true }) as ReturnType["project"]; + useProject.setState({ project: project("/p") }); + results.push([rust("1.0.19")]); + const dispose = activate(); + await vi.waitFor(() => expect(useProjectViewRoots.getState().roots.has("externalLibraries")).toBe(true)); + hang = true; + try { + useProject.setState({ project: project("/q") }); + expect(useProjectViewRoots.getState().roots.has("externalLibraries")).toBe(false); + } finally { + hang = false; + dispose(); + } + }); });