From 2cad48b4017050840c8b3d07ef4f8619459a3655 Mon Sep 17 00:00:00 2001 From: Vadim Lazuk Date: Fri, 4 Dec 2026 15:17:47 +0300 Subject: [PATCH] fix(build-tools): running a goal or task from the tool window leaves the selected configuration alone (#8028) Running a Maven goal or Gradle task from its tool window went through run.runConfig with a temporary configuration, and adoptTemporary always selected it, so the just-run goal replaced the configuration selected in the Run Configurations dropdown. The tool window now passes select: false and adoptTemporary only selects when asked; editor and gutter runs still select their temporary configuration. Fixes #8016 Co-authored-by: Inf1mum <8135225+Inf1mum@users.noreply.github.com> --- src/features/build-tools/store.ts | 7 ++++++- src/features/build-tools/workspace.test.tsx | 14 ++++++++++---- src/features/run/index.ts | 10 ++++++++-- src/features/run/java.test.ts | 11 +++++++++++ src/features/run/store.ts | 10 ++++++---- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/features/build-tools/store.ts b/src/features/build-tools/store.ts index 50495c1582..ee3d16e3b7 100644 --- a/src/features/build-tools/store.ts +++ b/src/features/build-tools/store.ts @@ -828,7 +828,12 @@ export function resetBuild() { // Running (through the run feature's actions) export function runConfig(config: RunConfig, mode: "run" | "debug") { - void executeAction(mode === "debug" ? "run.debugConfig" : "run.runConfig", { config, temporary: true }); + // `select: false`: running a goal or task from the tool window leaves the selected configuration alone, like IDEA. + void executeAction(mode === "debug" ? "run.debugConfig" : "run.runConfig", { + config, + temporary: true, + select: false, + }); } /** Runs `config` in the Run tool window; resolves with its exit code (`null` when it didn't start or was stopped). */ diff --git a/src/features/build-tools/workspace.test.tsx b/src/features/build-tools/workspace.test.tsx index 8637f43792..5326ec258f 100644 --- a/src/features/build-tools/workspace.test.tsx +++ b/src/features/build-tools/workspace.test.tsx @@ -132,13 +132,13 @@ it("lists linked Maven projects and keeps duplicate profile choices and goal dir await vi.advanceTimersByTimeAsync(PROFILE_RELOAD_DELAY_MS); }); expect(mocks.sync).toHaveBeenCalledWith("maven", expect.any(Number), false, ["local"], members[1], expect.anything()); - const runs: RunConfig[] = []; + const runs: { config: RunConfig; select?: boolean }[] = []; const off = registerAction({ id: "run.runConfig", title: "Run", hidden: true, run: ({ arg }) => { - runs.push((arg as { config: RunConfig }).config); + runs.push(arg as { config: RunConfig; select?: boolean }); }, }); await act(async () => { @@ -146,8 +146,14 @@ it("lists linked Maven projects and keeps duplicate profile choices and goal dir }); expect(executeBuildLine("maven", "validate", maven.projects[0]!.pom)).toBe(true); off(); - expect(runs[0]).toMatchObject({ cwd: members[1], command: `${members[1]}/mvnw`, args: ["validate", "-Plocal"] }); - expect(runs[1]).toMatchObject({ cwd: members[0], command: `${members[0]}/mvnw`, args: ["validate"] }); + expect(runs[0]!.config).toMatchObject({ + cwd: members[1], + command: `${members[1]}/mvnw`, + args: ["validate", "-Plocal"], + }); + expect(runs[1]!.config).toMatchObject({ cwd: members[0], command: `${members[0]}/mvnw`, args: ["validate"] }); + // IDEA parity: running a goal from the tool window leaves the selected configuration alone. + expect(runs.every((r) => r.select === false)).toBe(true); }); it("reloads all Maven members with their own profile choices", async () => { diff --git a/src/features/run/index.ts b/src/features/run/index.ts index 1d844a5024..28528f7550 100644 --- a/src/features/run/index.ts +++ b/src/features/run/index.ts @@ -440,10 +440,16 @@ function configFromArg(arg: unknown): RunConfig | undefined { if (typeof arg === "string") return useRun.getState().configs.find((c) => c.id === arg); if (!arg || typeof arg !== "object") return undefined; if ("config" in arg) { - const { config, temporary, saved } = arg as { config: RunConfig; temporary?: boolean; saved?: boolean }; + const { config, temporary, saved, select } = arg as { + config: RunConfig; + temporary?: boolean; + saved?: boolean; + select?: boolean; + }; // `saved`: a rerun launches the configuration as saved now, edited since it started. if (saved) return useRun.getState().configs.find((c) => c.id === config.id) ?? config; - return temporary ? adoptTemporary(config) : config; + // `select: false` (a tool-window goal run): the temporary configuration doesn't become selected. + return temporary ? adoptTemporary(config, select) : config; } return arg as RunConfig; } diff --git a/src/features/run/java.test.ts b/src/features/run/java.test.ts index 6476fbd5a8..7cc5418ade 100644 --- a/src/features/run/java.test.ts +++ b/src/features/run/java.test.ts @@ -736,6 +736,17 @@ describe("temporary configurations", () => { expect(useRun.getState().temporary).toEqual([]); }); + it("leave the selected configuration alone when the run asks not to select (Maven / Gradle tool windows)", () => { + useRun.setState({ selectedId: "maven:spring-boot:run" }); + const goal = cfg({ id: "maven:clean", kind: "maven", command: "./mvnw", args: ["clean"], cwd: "/p" }); + adoptTemporary(goal, false); + expect(useRun.getState().selectedId).toBe("maven:spring-boot:run"); + expect(useRun.getState().temporary).toEqual([goal.id]); + // The default still selects: editor-context runs rely on it. + const app = adoptTemporary(applicationConfig({ className: "a.Main" }, "src/main/java/a/Main.java")); + expect(useRun.getState().selectedId).toBe(app.id); + }); + it("reuse a configuration of the same main class and module, whatever its id", () => { const own = (module: string) => cfg({ diff --git a/src/features/run/store.ts b/src/features/run/store.ts index bb3fdf9c87..2a085a8cd1 100644 --- a/src/features/run/store.ts +++ b/src/features/run/store.ts @@ -563,9 +563,11 @@ function sameBuildGoal(a: RunConfig, b: RunConfig): boolean { /** * Selects the configuration for a gutter / context run: a saved or detected * configuration with the same id is reused, otherwise `config` becomes a - * temporary one (the oldest idle temporary is dropped beyond five). + * temporary one (the oldest idle temporary is dropped beyond five). A `select` + * of false (a Maven / Gradle tool-window goal run, like IDEA) adopts the temporary + * configuration without changing which configuration is selected. */ -export function adoptTemporary(config: RunConfig): RunConfig { +export function adoptTemporary(config: RunConfig, select = true): RunConfig { const state = useRun.getState(); // IDEA's `findExistingConfiguration`: a configuration the user keeps for this context wins. const existing = @@ -574,7 +576,7 @@ export function adoptTemporary(config: RunConfig): RunConfig { (c) => !state.temporary.includes(c.id) && (sameApplication(c, config) || sameBuildGoal(c, config)), ); if (existing && !state.temporary.includes(config.id)) { - selectConfig(existing.id); + if (select) selectConfig(existing.id); return existing; } // Two configurations with one name can't be saved (IDEA's `suggestUniqueName`): goals that differ @@ -595,7 +597,7 @@ export function adoptTemporary(config: RunConfig): RunConfig { ]; useRun.setState({ configs, temporary }); persistTemporary(); - selectConfig(config.id); + if (select) selectConfig(config.id); return config; }