Skip to content

Commit

Permalink
Avoid resolving the errorproneJavac configuration when not needed
Browse files Browse the repository at this point in the history
In Gradle 6.7 where toolchains are supported, the
configuration was resolved everytime, whether the
build or compile task (through toolchains) used
Java 8 or not; and this led to emitting a warning
about missing errorprone javac dependency (or
resolving it) even in cases where it would never
be needed.
  • Loading branch information
tbroyer committed Mar 20, 2021
1 parent f87dd12 commit 63e5538
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/kotlin/net/ltgt/gradle/errorprone/ErrorPronePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Add a dependency to com.google.errorprone:javac with the appropriate version cor
}
}

val providers = project.providers
project.tasks.withType<JavaCompile>().configureEach {
val errorproneOptions =
(options as ExtensionAware).extensions.create(ErrorProneOptions.NAME, ErrorProneOptions::class.java)
Expand All @@ -102,7 +103,21 @@ Add a dependency to com.google.errorprone:javac with the appropriate version cor
.add(ErrorProneCompilerArgumentProvider(errorproneOptions))

if (HAS_TOOLCHAINS || JavaVersion.current().isJava8) {
inputs.files(javacConfiguration).withPropertyName(JAVAC_CONFIGURATION_NAME).withNormalizer(ClasspathNormalizer::class)
inputs.files(
providers.provider {
when {
!options.errorprone.isEnabled.getOrElse(false) -> emptyList()
HAS_TOOLCHAINS && javaCompiler.isPresent -> {
when (javaCompiler.get().metadata.languageVersion.asInt()) {
8 -> javacConfiguration
else -> emptyList()
}
}
JavaVersion.current().isJava8 -> javacConfiguration
else -> emptyList()
}
}
).withPropertyName(JAVAC_CONFIGURATION_NAME).withNormalizer(ClasspathNormalizer::class)
doFirst("configure errorprone in bootclasspath") {
when {
!options.errorprone.isEnabled.getOrElse(false) -> return@doFirst
Expand Down
21 changes: 21 additions & 0 deletions src/test/kotlin/net/ltgt/gradle/errorprone/Java8IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ class Java8IntegrationTest : AbstractPluginIntegrationTest() {
}
}

@Test
fun `does not warn if Error Prone javac dependency is not configured with non-Java 8 VM`() {
assume().withMessage("isJava8").that(JavaVersion.current().isJava8).isFalse()

// given
// Remove the errorproneJavac dependency
buildFile.writeText(
buildFile.readLines().filterNot {
it.contains("""errorproneJavac("com.google.errorprone:javac:$errorproneJavacVersion")""")
}.joinToString(separator = "\n")
)

// when
buildWithArgs("compileJava").also { result ->
// then
assertThat(result.task(":compileJava")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.output).doesNotContain(ErrorPronePlugin.NO_JAVAC_DEPENDENCY_WARNING_MESSAGE)
assertThat(result.output).doesNotContain(JVM_ARG_BOOTCLASSPATH)
}
}

@Test
fun `is build-cache friendly`() {
assume().withMessage("isJava8").that(JavaVersion.current().isJava8).isTrue()
Expand Down

0 comments on commit 63e5538

Please sign in to comment.