fix(run): find openideaRun on Gradle 7.0 and older when jvmargs are set (#4108)

With org.gradle.jvmargs set (project or ~/.gradle), Gradle 7.0 and older pass the command line's -D values to a build JVM only after settings load, so gradleRun.gradle read openidea.run.project as null at the top of the init script and skipped registering openideaRun ("Task 'openideaRun' not found"). The property is now read inside the allprojects action, which runs after settings. gradleBoot.gradle and gradleTests.gradle already read theirs in projectsEvaluated.

Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
This commit is contained in:
LU XIANGRUI
2026-11-18 11:09:34 +08:00
committed by GitHub
co-authored by wildcatDownstairs
parent 9f39cf2963
commit d5caebc2bb
2 changed files with 58 additions and 29 deletions
+28 -29
View File
@@ -7,37 +7,36 @@ import org.gradle.util.GradleVersion
def lazyTasks = GradleVersion.current() >= GradleVersion.version('4.9')
def target = System.getProperty('openidea.run.project')
if (target != null) {
allprojects { p ->
// `target` is an identity path (`:lib:core` for a project of an included build).
def id = { try { p.identityPath.toString() } catch (ignored) { p.path } }
if (id() != target) return
p.plugins.withId('java') {
def json = new JsonSlurper()
def list = { String key -> (List) json.parseText(System.getProperty(key, '[]')) }
def configure = { JavaExec t ->
t.description = 'Runs a main class for OpenIDEA.'
t.classpath = p.sourceSets.getByName(System.getProperty('openidea.run.sourceSet', 'main')).runtimeClasspath
def main = System.getProperty('openidea.run.main')
if (t.hasProperty('mainClass')) t.mainClass.set(main) else t.main = main
t.args(list('openidea.run.args'))
t.jvmArgs(list('openidea.run.jvmArgs'))
// The IDE attaches to this JVM, not to the Gradle daemon; port 0 prints the one picked.
if (Boolean.getBoolean('openidea.run.debug')) {
// Opens the attach socket for the memory view, unless the VM options decide about it.
if (Boolean.getBoolean('openidea.run.attachListener')
&& !list('openidea.run.jvmArgs').any { it.toString().find(/StartAttachListener|DisableAttachMechanism/) != null }) {
t.jvmArgs('-XX:+StartAttachListener')
}
t.jvmArgs('-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=127.0.0.1:0')
// Gradle 7.0 and older give a build JVM with its own `org.gradle.jvmargs` the command line's
// `-D` values only once the settings load, after init scripts run: read them in project actions (#4093).
allprojects { p ->
// An identity path (`:lib:core` for a project of an included build); none: not a delegated run.
def id = { try { p.identityPath.toString() } catch (ignored) { p.path } }
if (id() != System.getProperty('openidea.run.project')) return
p.plugins.withId('java') {
def json = new JsonSlurper()
def list = { String key -> (List) json.parseText(System.getProperty(key, '[]')) }
def configure = { JavaExec t ->
t.description = 'Runs a main class for OpenIDEA.'
t.classpath = p.sourceSets.getByName(System.getProperty('openidea.run.sourceSet', 'main')).runtimeClasspath
def main = System.getProperty('openidea.run.main')
if (t.hasProperty('mainClass')) t.mainClass.set(main) else t.main = main
t.args(list('openidea.run.args'))
t.jvmArgs(list('openidea.run.jvmArgs'))
// The IDE attaches to this JVM, not to the Gradle daemon; port 0 prints the one picked.
if (Boolean.getBoolean('openidea.run.debug')) {
// Opens the attach socket for the memory view, unless the VM options decide about it.
if (Boolean.getBoolean('openidea.run.attachListener')
&& !list('openidea.run.jvmArgs').any { it.toString().find(/StartAttachListener|DisableAttachMechanism/) != null }) {
t.jvmArgs('-XX:+StartAttachListener')
}
def dir = System.getProperty('openidea.run.workingDir')
if (dir) t.workingDir = dir
t.standardInput = System.in
t.jvmArgs('-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=127.0.0.1:0')
}
if (lazyTasks) p.tasks.register('openideaRun', JavaExec, configure)
else p.tasks.create('openideaRun', JavaExec, configure)
def dir = System.getProperty('openidea.run.workingDir')
if (dir) t.workingDir = dir
t.standardInput = System.in
}
if (lazyTasks) p.tasks.register('openideaRun', JavaExec, configure)
else p.tasks.create('openideaRun', JavaExec, configure)
}
}
+30
View File
@@ -206,6 +206,36 @@ describe("Build and run using: Gradle", () => {
expect(unguarded).toEqual([]);
});
// Up to Gradle 7.0, a build JVM with its own `org.gradle.jvmargs` gets the command line's `-D`
// values only once the settings load: read at an init script's top level, they are null (#4093).
it.each([
["gradleRun", RUN_SCRIPT],
["gradleTests", TESTS_SCRIPT],
["gradleBoot", BOOT_SCRIPT],
])("%s reads the launch's -D values in project actions, not at its top level", (_, script) => {
const topLevel = script
.split("\n")
.filter((l) => /^\S/.test(l) && /System\.getProperty|Boolean\.getBoolean|systemPropertiesArgs/.test(l));
expect(topLevel).toEqual([]);
});
it.each([
// The report: a Spring Boot application in the root project, Gradle wrapper 6.4.1.
["", ":", ":openideaRun"],
["app", ":app", ":app:openideaRun"],
[CORE, ":billing:core", ":billing:core:openideaRun"],
])("runs module %j as project %s on a Gradle 6.4.1 wrapper (#4093)", (module, project, task) => {
const file = `${module ? `${module}/` : ""}src/main/java/com/example/demo/DemoApplication.java`;
const app = applicationConfig({ className: "com.example.demo.DemoApplication", spring: true }, file, []);
expect(wrapperGradleVersion("distributionUrl=https\\://x/gradle-6.4.1-bin.zip")).toEqual([6, 4]);
// Below Gradle 8.1: no configuration cache.
const run = delegatedGradleConfig(app, module, { ...ctx, configurationCache: false })!;
expect(run.args).toContain(`-Dopenidea.run.project=${project}`);
expect(run.args.at(-1)).toBe(task);
// The init script compares each project's identity path with it inside `allprojects`.
expect(RUN_SCRIPT).toContain("if (id() != System.getProperty('openidea.run.project')) return");
});
it("sets the main class with `main` and creates the task eagerly on old Gradle (#3864)", () => {
expect(RUN_SCRIPT).toContain("if (t.hasProperty('mainClass')) t.mainClass.set(main) else t.main = main");
expect(RUN_SCRIPT).toContain("else p.tasks.create('openideaRun', JavaExec, configure)");