From 93edad8300b7d48069903eb09cfb03f9e3c2f92f Mon Sep 17 00:00:00 2001 From: tinder-ryantrontz Date: Fri, 6 Mar 2026 10:59:59 -0800 Subject: [PATCH] fix: use configureEach instead of all for JavaCompile tasks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The eager .all {} API forces realization of lazily-registered JavaCompile tasks during afterEvaluate. When a project defines custom source sets (e.g. intTest) with a Java toolchain, Gradle finalizes the javaCompiler property during task realization — before JavaBasePlugin.createCompileJavaTask finishes setting its convention. This causes: 'property javaCompiler is final and cannot be changed'. Replacing .all {} with .configureEach {} defers the configuration action until Gradle has fully initialized each task. Amp-Thread-ID: https://ampcode.com/threads/T-019cbfff-d981-727b-9564-00e5705dbcec Co-authored-by: Amp --- .../main/scala/SemanticdbGradlePlugin.scala | 2 +- .../scala/tests/GradleBuildToolSuite.scala | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/semanticdb-gradle-plugin/src/main/scala/SemanticdbGradlePlugin.scala b/semanticdb-gradle-plugin/src/main/scala/SemanticdbGradlePlugin.scala index 04e84ff5..b44e2c07 100644 --- a/semanticdb-gradle-plugin/src/main/scala/SemanticdbGradlePlugin.scala +++ b/semanticdb-gradle-plugin/src/main/scala/SemanticdbGradlePlugin.scala @@ -104,7 +104,7 @@ class SemanticdbGradlePlugin extends Plugin[Project] { project .getTasks() .withType(classOf[JavaCompile]) - .all { task => + .configureEach { task => // If we run on JDK 17, we need to add special flags to the JVM // to allow access to the compiler. diff --git a/tests/buildTools/src/test/scala/tests/GradleBuildToolSuite.scala b/tests/buildTools/src/test/scala/tests/GradleBuildToolSuite.scala index 9eb470e1..9dc70e0d 100644 --- a/tests/buildTools/src/test/scala/tests/GradleBuildToolSuite.scala +++ b/tests/buildTools/src/test/scala/tests/GradleBuildToolSuite.scala @@ -511,4 +511,37 @@ abstract class GradleBuildToolSuite(gradle: Tool.Gradle) // NOTE(olafur): no packages because we use more modern APIs. ) + // Regression test: projects that lazily register custom source sets (e.g. intTest) + // with a Java toolchain would fail because the eager `.all {}` API in the plugin + // caused the javaCompiler property to be finalized before Gradle finished + // configuring the task. + checkGradleBuild( + "lazy-sourceset-with-toolchain", + s"""|/build.gradle + |plugins { + | id 'java' + |} + |java { + | toolchain { + | languageVersion = JavaLanguageVersion.of(11) + | } + |} + |sourceSets { + | intTest { + | compileClasspath += sourceSets.main.output + | runtimeClasspath += sourceSets.main.output + | } + |} + |configurations { + | intTestImplementation.extendsFrom implementation + |} + |/src/main/java/Example.java + |public class Example {} + |/src/intTest/java/ExampleIntTest.java + |public class ExampleIntTest {} + |""".stripMargin, + expectedSemanticdbFiles = 1, + gradleVersions = List(Gradle8, Gradle7) + ) + }