From 4812d28614802fc1b5f3b0e440e69ac020baeeea Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 29 Nov 2026 16:47:13 +0800 Subject: [PATCH] fix(dotnet): show only top-level solutions above the Project view folder (#6820) Since #6224 the Project view listed every discovered .NET solution and standalone project above the project folder, so a polyglot repository with many solutions in subfolders got dozens of root nodes ahead of its own folder. Only solution and project files directly in a content root (the project root, or each workspace member folder) now get a node there; nested ones stay in their folders, where they already showed. Fixes #6803 --- src/features/dotnet/tree.test.ts | 73 ++++++++++++++++++++++---------- src/features/dotnet/tree.ts | 41 +++++++++++------- 2 files changed, 77 insertions(+), 37 deletions(-) diff --git a/src/features/dotnet/tree.test.ts b/src/features/dotnet/tree.test.ts index 8d3593e541..e101832f70 100644 --- a/src/features/dotnet/tree.test.ts +++ b/src/features/dotnet/tree.test.ts @@ -114,23 +114,52 @@ describe("the .NET solution tree", () => { }); it("registers standalone projects as file roots of their own", async () => { - indexed(["/repo/App/App.csproj"]); + indexed(["/repo/App.csproj"]); vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ - projects: [project("/repo/App/App.csproj", "App")], + projects: [project("/repo/App.csproj", "App")], solutions: [], }); dispose = activateTree(); - await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App/App.csproj")); - const alone = useProjectViewRoots.getState().roots.get("dotnetProject:/repo/App/App.csproj")!; - expect([alone.label, alone.path, alone.file, alone.beforeProject]).toEqual([ - "App", - "/repo/App/App.csproj", - true, - true, - ]); + await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App.csproj")); + const alone = useProjectViewRoots.getState().roots.get("dotnetProject:/repo/App.csproj")!; + expect([alone.label, alone.path, alone.file, alone.beforeProject]).toEqual(["App", "/repo/App.csproj", true, true]); expect(alone.children).toBeUndefined(); }); + it("leaves solutions and projects in subfolders to their folders", async () => { + indexed(["/repo/App.sln", "/repo/services/Billing/Billing.sln", "/repo/tools/Gen/Gen.csproj"]); + vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ + projects: [ + project("/repo/src/App/App.csproj", "App"), + project("/repo/services/Billing/Api/Api.csproj", "Api"), + project("/repo/tools/Gen/Gen.csproj", "Gen"), + ], + solutions: [ + { path: "/repo/App.sln", projects: ["/repo/src/App/App.csproj"] }, + { path: "/repo/services/Billing/Billing.sln", projects: ["/repo/services/Billing/Api/Api.csproj"] }, + ], + }); + dispose = activateTree(); + await vi.waitFor(() => expect(rootIds()).toContain("dotnetSolution:/repo/App.sln")); + expect(dotnetRoots()).toEqual(["dotnetSolution:/repo/App.sln"]); + }); + + it("treats each workspace member folder as a content root", async () => { + useProject.setState({ + project: { root: "/ws/.state", workspace: { projects: ["/ws/api", "/ws/web"] } } as unknown as NonNullable< + typeof previousProject + >, + }); + indexed(["/ws/api/Api.sln"]); + vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ + projects: [project("/ws/api/src/Api.csproj", "Api"), project("/ws/web/legacy/Legacy.csproj", "Legacy")], + solutions: [{ path: "/ws/api/Api.sln", projects: ["/ws/api/src/Api.csproj"] }], + }); + dispose = activateTree(); + await vi.waitFor(() => expect(rootIds()).toContain("dotnetSolution:/ws/api/Api.sln")); + expect(dotnetRoots()).toEqual(["dotnetSolution:/ws/api/Api.sln"]); + }); + it("registers nothing while the workspace model is empty", async () => { indexed(["/repo/App.sln"]); vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ projects: [], solutions: [] }); @@ -175,11 +204,11 @@ describe("the .NET solution tree", () => { expect(useDotnet.getState().busy).toBe(false); // A retry (a context action or the NuGet window calling the shared scan) replaces it. scan.mockResolvedValue({ - projects: [project("/repo/App/App.csproj", "App")], + projects: [project("/repo/App.csproj", "App")], solutions: [], }); await reloadDotnet(); - await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App/App.csproj")); + await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App.csproj")); expect(useProjectViewRoots.getState().roots.get("dotnetDiscovery")).toBeUndefined(); }); @@ -216,7 +245,7 @@ describe("the .NET solution tree", () => { it("retries the gate once when indexing finishes", async () => { const scan = vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ - projects: [project("/repo/App/App.csproj", "App")], + projects: [project("/repo/App.csproj", "App")], solutions: [], }); // Fresh open: the index is still building and has no .NET files yet. @@ -226,10 +255,10 @@ describe("the .NET solution tree", () => { await gateChecked(); expect(scan).not.toHaveBeenCalled(); // Indexing finished, and now the projects are there: the one retry scans. - indexed(["/repo/App/App.csproj"]); + indexed(["/repo/App.csproj"]); indexing(false); indexSettled(); - await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App/App.csproj")); + await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App.csproj")); expect(scan).toHaveBeenCalledTimes(1); // A further settle after the retry must not scan again. indexSettled(); @@ -238,7 +267,7 @@ describe("the .NET solution tree", () => { it("retries the gate when the project opened before its first index pass started", async () => { const scan = vi.spyOn(platform, "dotnetWorkspace").mockResolvedValue({ - projects: [project("/repo/App/App.csproj", "App")], + projects: [project("/repo/App.csproj", "App")], solutions: [], }); indexed([]); @@ -246,10 +275,10 @@ describe("the .NET solution tree", () => { dispose = activateTree(); await gateChecked(); expect(scan).not.toHaveBeenCalled(); - indexed(["/repo/App/App.csproj"]); + indexed(["/repo/App.csproj"]); indexing(false); indexSettled(); - await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App/App.csproj")); + await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App.csproj")); expect(scan).toHaveBeenCalledTimes(1); }); @@ -273,18 +302,18 @@ describe("the .NET solution tree", () => { it("clears the roots and gates each project open once", async () => { const scan = vi.spyOn(platform, "dotnetWorkspace").mockImplementation(async (root: string) => ({ - projects: [project(`${root}/App/App.csproj`, "App")], + projects: [project(`${root}/App.csproj`, "App")], solutions: [], })); - indexed(["/repo/App/App.csproj", "/other/App/App.csproj"]); + indexed(["/repo/App.csproj", "/other/App.csproj"]); dispose = activateTree(); - await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App/App.csproj")); + await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/repo/App.csproj")); expect(scan).toHaveBeenCalledTimes(1); // As the feature does on a project switch: reset the model, then the tree follows. resetDotnet(); useProject.setState({ project: { root: "/other" } as NonNullable }); expect(dotnetRoots()).toEqual([]); - await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/other/App/App.csproj")); + await vi.waitFor(() => expect(rootIds()).toContain("dotnetProject:/other/App.csproj")); expect(scan).toHaveBeenCalledTimes(2); }); }); diff --git a/src/features/dotnet/tree.ts b/src/features/dotnet/tree.ts index b39a273e22..721520824b 100644 --- a/src/features/dotnet/tree.ts +++ b/src/features/dotnet/tree.ts @@ -2,7 +2,8 @@ * Discovered .NET solutions and projects (#5635) as Project view nodes: one * `Solution ''` group per solution with a leaf per project, standalone * projects as roots of their own — above the project folder, like the module - * groups of a build. The model comes from the one shared workspace scan + * groups of a build — for solution and project files directly in a content root + * only: a repository holding .NET code in subfolders keeps it in its folders. The model comes from the one shared workspace scan * (`ensureDotnetModel`, the scan the NuGet window and context actions share); * roots re-register in place on change (as External Libraries does) and nothing * registers while the model is empty. The scan starts only when the file index @@ -13,8 +14,8 @@ */ import type { DotnetWorkspace } from "@/platform/dotnet"; import { tx } from "@/platform/i18n"; -import { call, commands, onIdeEvent } from "@/platform/ipc"; -import { basename, useProject } from "@/platform/project"; +import { call, commands, onIdeEvent, type ProjectInfo } from "@/platform/ipc"; +import { basename, dirname, useProject } from "@/platform/project"; import { registerProjectViewRoot, type ProjectViewRootContribution } from "@/workbench/contributions"; import { ensureDotnetModel, useDotnet } from "./store"; import { isDotnetProjectPath, isDotnetSolutionPath } from "./target"; @@ -73,26 +74,32 @@ const stateRoot = (detail: string): ProjectViewRootContribution => ({ * retry-on-next-use behaviour: the next scan (a context action, the NuGet * window, a reopened project) replaces the placeholder. */ -export function treeRoots({ - model, - busy, - error, -}: { - model: DotnetWorkspace; - busy: boolean; - error: string; -}): ProjectViewRootContribution[] { +export function treeRoots( + { + model, + busy, + error, + }: { + model: DotnetWorkspace; + busy: boolean; + error: string; + }, + contentRoots: readonly string[], +): ProjectViewRootContribution[] { if (!model.projects.length && !model.solutions.length) { if (busy) return [stateRoot(tx`Scanning…`)]; if (error) return [stateRoot(tx`Scan failed`)]; return []; } + // Only a solution or project file directly in a content root is the project itself and earns a + // node above the folder. Nested ones (a .NET part of a larger repository) stay in their folders. + const topLevel = (path: string) => contentRoots.includes(dirname(path)); const projectName = (path: string) => model.projects.find((p) => p.path === path)?.name ?? solutionName(path); const leaf = (path: string) => ({ label: projectName(path), icon: ICON, path, file: true }); const grouped = new Set(model.solutions.flatMap((s) => s.projects)); const roots: ProjectViewRootContribution[] = []; for (const solution of model.solutions) { - if (!solution.projects.length) continue; + if (!solution.projects.length || !topLevel(solution.path)) continue; roots.push({ id: solutionId(solution.path), label: tx`Solution '${solutionName(solution.path)}'`, @@ -102,7 +109,7 @@ export function treeRoots({ }); } for (const project of model.projects) - if (!grouped.has(project.path)) + if (!grouped.has(project.path) && topLevel(project.path)) roots.push({ id: projectId(project.path), label: project.name, @@ -114,6 +121,10 @@ export function treeRoots({ return roots.map((root, i) => ({ ...root, order: -200 + i })); } +/** The folders the Project view shows as content: a workspace's member projects, else the project root. */ +const contentRoots = (project: ProjectInfo | null): string[] => + project ? (project.workspace?.projects ?? [project.root]) : []; + /** Keeps the Project view roots in step with the discovered workspace model. */ export function activateTree(): () => void { let disposals: (() => void)[] = []; @@ -128,7 +139,7 @@ export function activateTree(): () => void { prev.reverse().forEach((d) => d()); }; const sync = () => { - const roots = treeRoots(useDotnet.getState()); + const roots = treeRoots(useDotnet.getState(), contentRoots(useProject.getState().project)); const key = JSON.stringify(roots); if (key === shown) return; shown = key;