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.
This commit is contained in:
l0ng-ai
2026-11-18 19:26:48 +08:00
committed by GitHub
parent 6135a5326d
commit 738dc12ef7
2 changed files with 29 additions and 2 deletions
+10 -2
View File
@@ -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;
+19
View File
@@ -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<typeof useProject.getState>["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();
}
});
});