tbroyer / gradle-errorprone-plugin

Gradle plugin to use the error-prone compiler for Java
Apache License 2.0
366 stars 32 forks source link

Fork options are being reset by the plugin when using toolchains #64

Closed tbroyer closed 3 years ago

tbroyer commented 3 years ago

Gradle allows configuring fork options without setting options.fork = true. Prior to Gradle 6.7 and toolchains, those options would be completely ignored so the plugin, when configuring forking for JDK 8 (initially, now also JDK 16+), resets the fork options entirely to preserve that behavior of ignoring those options.

The thing is: with toolchains, Gradle will decide when to fork based on the current and configured toolchain JVMs, and will then happily apply the configured fork options. This allows configuring options that would be needed if forking is required, without unconditionally forking (and one could configure similar options in org.gradle.jvmargs in the gradle.properties for when forking isn't needed); or assuming that Gradle will fork anyway.

This means that, when the following conditions are met, the plugin will reset/unset the fork options, contrary to what would happen otherwise without the plugin:

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(8))
    }
}
tasks.compileJava {
    options.forkOptions.jvmArgs!!.add("-Dthis.property.will.be.lost")
}

A workaround is to configure options.fork=true explicitly, possibly conditionally based on the current and configured toolchain JVMs, as the plugin will then leave the fork options alone; e.g.

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(16))
    }
}
// workaround: conditionally configure explicit forking for cases when Gradle would fork anyway
// note: this doesn't take into account the vendor and/or implementation (hotspot vs J9)
// Gradle actually uses something like:
// if (javaToolchains.compilerFor(java.toolchain).metadata.installationPath.asFile != Jvm.current().javaHome)
// but `org.gradle.internal.jvm.Jvm` is an internal class (and `System.getProperty("java.home")` is not accurate)
if (JavaVersion.current() != JavaVersion.VERSION_16) {
    tasks.withType<JavaCompile>().configureEach {
        options.isFork = true
    }
}