From 22a5e4c9e6043f9c458901f73617813ab337a117 Mon Sep 17 00:00:00 2001 From: Vadim Lazuk Date: Thu, 19 Nov 2026 14:29:44 +0300 Subject: [PATCH] fix(maven): reuse an existing build configuration for Maven goal runs (#4503) Running a lifecycle goal such as package from the Maven tool window always created a temporary ' [package]' configuration, even when a saved or detected configuration already ran the same goal. adoptTemporary only matched by id or main class. Maven and Gradle runs now also match a kept configuration with the same build tool, command, directory and exact arguments, which is then selected and run instead; a different flag set (for example -DskipTests) still gets its own temporary configuration. Co-authored-by: Inf1mum <8135225+Inf1mum@users.noreply.github.com> --- src/features/run/java.test.ts | 46 +++++++++++++++++++++++++++++++++++ src/features/run/store.ts | 15 +++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/features/run/java.test.ts b/src/features/run/java.test.ts index ffeed732c..2071c470f 100644 --- a/src/features/run/java.test.ts +++ b/src/features/run/java.test.ts @@ -668,6 +668,52 @@ describe("temporary configurations", () => { await saveTemporary(app.id).catch(() => undefined); expect(useRun.getState().temporary).toEqual([]); }); + + it("reuse the detected configuration of the same Maven goal instead of adding a temporary one", () => { + const detected = cfg({ id: "maven:package", kind: "maven", name: "mvn package", args: ["package"] }); + useRun.setState({ configs: [detected] }); + const goal = cfg({ + id: "maven:goal:pom.xml:package", + kind: "maven", + name: "demo [package]", + args: ["package"], + detected: false, + }); + expect(adoptTemporary(goal)).toBe(detected); + expect(useRun.getState().temporary).toEqual([]); + expect(useRun.getState().selectedId).toBe(detected.id); + }); + + it("reuse a Maven goal configuration with the same command line, but not across goals, flags, builds or tools", () => { + const detected = cfg({ id: "maven:package", kind: "maven", name: "mvn package", args: ["package"] }); + useRun.setState({ configs: [detected] }); + const goal = (over: Partial): RunConfig => + cfg({ id: "maven:goal:pom.xml:package", kind: "maven", args: ["package"], detected: false, ...over }); + expect(adoptTemporary(goal({}))).toBe(detected); + expect( + adoptTemporary(goal({ id: "maven:goal:pom.xml:package.skipTests", args: ["package", "-DskipTests"] })).id, + ).toBe("maven:goal:pom.xml:package.skipTests"); + expect(adoptTemporary(goal({ id: "maven:goal:pom.xml:clean", args: ["clean"] })).id).toBe( + "maven:goal:pom.xml:clean", + ); + expect(adoptTemporary(goal({ id: "maven:goal:core/pom.xml:package", args: ["package"], cwd: "core" })).id).toBe( + "maven:goal:core/pom.xml:package", + ); + expect(adoptTemporary(goal({ id: "gradle:task:...:package", kind: "gradle", command: "gradle" })).id).toBe( + "gradle:task:...:package", + ); + const { configs, temporary } = useRun.getState(); + expect(temporary).toHaveLength(4); + expect(configs.filter((c) => !temporary.includes(c.id))).toEqual([detected]); + }); + + it("reuse the detected configuration of the same Gradle tasks", () => { + const detected = cfg({ id: "gradle:test", kind: "gradle", name: "gradle test", args: ["test"], isTest: true }); + useRun.setState({ configs: [detected] }); + const task = cfg({ id: "gradle:task:...:test", kind: "gradle", args: ["test"], detected: false }); + expect(adoptTemporary(task)).toBe(detected); + expect(useRun.getState().temporary).toEqual([]); + }); }); describe("jvmMainCandidates", () => { diff --git a/src/features/run/store.ts b/src/features/run/store.ts index 2ca392af9..627e58990 100644 --- a/src/features/run/store.ts +++ b/src/features/run/store.ts @@ -447,6 +447,17 @@ function sameApplication(a: RunConfig, b: RunConfig): boolean { return main !== null && main === parseJavaArgs(b.args).mainClass && (a.module ?? a.cwd) === (b.module ?? b.cwd); } +/** + * IDEA's `isConfigurationFromContext` for a Maven / Gradle goal: the same command line run by the + * same build tool in the same build directory, whatever the configuration is named (#4436). The + * existing configuration is run as is, so reuse must not drop option flags. + */ +function sameBuildGoal(a: RunConfig, b: RunConfig): boolean { + if ((a.kind !== "maven" && a.kind !== "gradle") || a.kind !== b.kind) return false; + if (a.command !== b.command || (a.cwd ?? "") !== (b.cwd ?? "")) return false; + return a.args.length > 0 && a.args.length === b.args.length && a.args.every((arg, i) => arg === b.args[i]); +} + /** * Selects the configuration for a gutter / context run: a saved or detected * configuration with the same id is reused, otherwise `config` becomes a @@ -457,7 +468,9 @@ export function adoptTemporary(config: RunConfig): RunConfig { // IDEA's `findExistingConfiguration`: a configuration the user keeps for this context wins. const existing = state.configs.find((c) => c.id === config.id) ?? - state.configs.find((c) => !state.temporary.includes(c.id) && sameApplication(c, config)); + state.configs.find( + (c) => !state.temporary.includes(c.id) && (sameApplication(c, config) || sameBuildGoal(c, config)), + ); if (existing && !state.temporary.includes(config.id)) { selectConfig(existing.id); return existing;