Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tbroyer opened this issue Jul 3, 2021 · 0 comments
Closed

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

tbroyer opened this issue Jul 3, 2021 · 0 comments
Labels
bug Something isn't working
Milestone

Comments

@tbroyer
Copy link
Owner

tbroyer commented Jul 3, 2021

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:

  • using Gradle >= 6.7, and configuring a JDK 8 or JDK 16+ toolchain (globally for the project, or specifically for one JavaCompile task)
  • not configuring options.fork = true on the task
  • having ErrorProne enabled for the task
  • configuring fork options for the JavaCompile task, that could be by another plugin, similar to this one but for another tool (they will be ignored/lost, that's the bug)
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
    }
}
@tbroyer tbroyer added the bug Something isn't working label Jul 3, 2021
@tbroyer tbroyer added this to the 2.1.0 milestone Jul 3, 2021
@tbroyer tbroyer changed the title Fork options are being reset by the plugin, even when using toolchains Fork options are being reset by the plugin when using toolchains Jul 3, 2021
@tbroyer tbroyer closed this as completed in 39080ef Jul 4, 2021
tbroyer added a commit that referenced this issue Jul 4, 2021
tbroyer added a commit that referenced this issue Jul 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant