diff --git a/src/features/run/contextRun.test.ts b/src/features/run/contextRun.test.ts index e30bfdcaf..acf6f1583 100644 --- a/src/features/run/contextRun.test.ts +++ b/src/features/run/contextRun.test.ts @@ -115,6 +115,20 @@ describe("run context menu group", () => { expect(contextMenuItems("projectView", { paths: [`${ROOT}/src/components`], dirs: [true] })).toEqual([]); }); + it("project view: Run / Debug '' on a .NET project file (#5635)", () => { + const items = contextMenuItems("projectView", { paths: [`${ROOT}/src/App/App.csproj`], dirs: [false] }); + expect(items[0]).toMatchObject({ + action: "run.runContext", + label: "Run 'App'", + icon: "run", + arg: { path: `${ROOT}/src/App/App.csproj`, dir: false }, + }); + expect(items[1]).toMatchObject({ action: "run.debugContext", label: "Debug 'App'", icon: "debug" }); + // A solution or a shared props file gets no Run / Debug entries. + expect(contextMenuItems("projectView", { paths: [`${ROOT}/src/App/App.sln`], dirs: [false] })).toEqual([]); + expect(contextMenuItems("projectView", { paths: [`${ROOT}/Directory.Build.props`], dirs: [false] })).toEqual([]); + }); + it("routes an attached Spring Boot main and its tests through the member's Maven build", () => { const member = "/work/spring"; const project = useProject.getState().project!; diff --git a/src/features/run/dotnetTypes.test.tsx b/src/features/run/dotnetTypes.test.tsx index c4d682651..ad88a71f6 100644 --- a/src/features/run/dotnetTypes.test.tsx +++ b/src/features/run/dotnetTypes.test.tsx @@ -3,7 +3,7 @@ import { fireEvent, render, screen } from "@testing-library/react"; import { commands } from "@/platform/ipc"; import type { RunConfig } from "@/platform/ipc"; import { useProject } from "@/platform/project"; -import { dotnetProjectFile, dotnetProjectType, dotnetTestType } from "./dotnetTypes"; +import { dotnetProjectFile, dotnetProjectProduction, dotnetProjectType, dotnetTestType } from "./dotnetTypes"; import { ConfigForm } from "./ConfigForm"; import { validateConfig } from "./configs"; import { formParams, newConfigOfType, registerBuiltinTypes, withParams } from "./types"; @@ -99,6 +99,28 @@ describe(".NET configurations", () => { expect(formParams({ ...typed, args: ["build", "A"] }, dotnetProjectType)).toBeNull(); }); + it("produces a context run configuration for a project file (#5635)", () => { + const p = dotnetProjectProduction("src/App/App.csproj"); + expect(p).toMatchObject({ + id: "context:dotnet:src/App/App.csproj", + name: "App", + kind: "dotnet", + command: "dotnet", + args: ["run", "--project", "src/App/App.csproj", "--no-launch-profile"], + debugAdapter: "netcoredbg", + detected: true, + isTest: false, + params: { project: "src/App/App.csproj" }, + }); + // Like a configuration from the form, it builds before launching so Debug works unbuilt. + expect(p.beforeLaunch).toEqual([{ kind: "dotnetBuild", target: "" }]); + // F#, Visual Basic and a project file at the root get the same shape. + expect(dotnetProjectProduction("lib/Worker.vbproj").name).toBe("Worker"); + expect(dotnetProjectProduction("App.fsproj").name).toBe("App"); + // The form can show it back ("Modify Run Configuration…"). + expect(formParams(p, dotnetProjectType)).toEqual({ project: "src/App/App.csproj" }); + }); + it("builds the edited project from the open root instead of a profile cwd", () => { useProject.setState({ project: { root: "/repo" } as never }); const c = withParams({ ...config(), cwd: "profile/work" }, { ...params, project: "other/App.csproj" }); diff --git a/src/features/run/dotnetTypes.ts b/src/features/run/dotnetTypes.ts index b2951ef9c..af8b421cd 100644 --- a/src/features/run/dotnetTypes.ts +++ b/src/features/run/dotnetTypes.ts @@ -70,6 +70,31 @@ export function dotnetProjectFile(config: RunConfig): string | null { return root ? `${root}/${project}` : null; } +/** An MSBuild project file of C#, F# or Visual Basic (not its `.csproj.user`). */ +export const isDotnetProjectFile = (path: string) => /\.(?:cs|fs|vb)proj$/i.test(path); + +/** + * "Run ''" on a project file in the project view (Rider's project node): `dotnet run + * --project` without launch profiles — those are the launchSettings.json gutter's entries. + * Debugged by netcoredbg like the saved configurations of the type. + */ +export function dotnetProjectProduction(project: string): RunConfig { + return { + id: `context:dotnet:${project}`, + name: project.replace(/^.*\//, "").replace(/\.(?:cs|fs|vb)proj$/i, ""), + kind: "dotnet", + command: "dotnet", + args: dotnetCommand({ project }).args, + cwd: null, + env: {}, + detected: true, + isTest: false, + debugAdapter: "netcoredbg", + beforeLaunch: [{ kind: "dotnetBuild", target: "" }], + params: { project }, + }; +} + export const dotnetProjectType: RunConfigTypeContribution = { id: "dotnet", label: ".NET Project", diff --git a/src/features/run/producers.test.ts b/src/features/run/producers.test.ts index b54dd828f..c56d08997 100644 --- a/src/features/run/producers.test.ts +++ b/src/features/run/producers.test.ts @@ -858,6 +858,25 @@ describe("project producers", () => { expect(p.config!.args).toEqual(["run", "test", "--", "src/a.test.ts"]); }); + it("runs a .NET project file with dotnet run --project (#5635)", () => { + const p = produceContext({ path: `${ROOT}/src/App/App.csproj`, root: ROOT, configs: [] }, OPTIMISTIC)!; + expect(p).toMatchObject({ name: "App", scope: "file" }); + expect(p.config).toMatchObject({ + id: "context:dotnet:src/App/App.csproj", + kind: "dotnet", + command: "dotnet", + args: ["run", "--project", "src/App/App.csproj", "--no-launch-profile"], + debugAdapter: "netcoredbg", + params: { project: "src/App/App.csproj" }, + }); + // A `.csproj.user` is not a project, and a source file has no entry-point production. + expect(produceContext({ path: `${ROOT}/src/App/App.csproj.user`, root: ROOT, configs: [] }, OPTIMISTIC)).toBeNull(); + expect(produceContext({ path: `${ROOT}/src/App/Program.cs`, root: ROOT, configs: [] }, OPTIMISTIC)).toBeNull(); + // Neither is a solution or a shared props file. + expect(produceContext({ path: `${ROOT}/src/App/App.sln`, root: ROOT, configs: [] }, OPTIMISTIC)).toBeNull(); + expect(produceContext({ path: `${ROOT}/Directory.Build.props`, root: ROOT, configs: [] }, OPTIMISTIC)).toBeNull(); + }); + it("⌃⇧R picks the innermost context: test > describe > file", () => { const src = [ "describe('math', () => {", diff --git a/src/features/run/producers.ts b/src/features/run/producers.ts index e5f0a9bc8..4fae57c8a 100644 --- a/src/features/run/producers.ts +++ b/src/features/run/producers.ts @@ -28,6 +28,7 @@ import { relToCwd as relativeToCwd, } from "./context"; import { djangoLabel, djangoTestsOf, hasDjangoTests } from "./djangoTypes"; +import { dotnetProjectProduction, isDotnetProjectFile } from "./dotnetTypes"; import { firstTopLevelType, scanJvmTargets, simpleClassName } from "./jvmTargets"; import { isWindows } from "./jvmTypes"; import { inheritedRunTargets } from "./inheritedMain"; @@ -819,6 +820,11 @@ export function produceFile(input: ContextInput, env: ToolEnv): Produced | null const rel = s.idPath; if (s.inProject) { + // #5635: Rider's "Run ''" on a project file, Debug through its netcoredbg adapter. + if (isDotnetProjectFile(rel)) { + const config = dotnetProjectProduction(rel); + return { name: config.name, scope: "file", config }; + } if (lang === "c" || lang === "cpp") { const target = cmakeTargets(input)[0]; if (target) return { name: target.name, scope: "file", config: target };