spotless: google-java-format (and removeUnusedImports) broken on JDK 16+ (has workaround)

When running under JDK 16 I get this error message:

Task :spotlessJava FAILED
Step 'google-java-format' found problem in 'src/main/java/se/inoviagroup/v2t/gateway/api/AuthUtils.java':
        ...
Caused by: java.lang.IllegalAccessError: class com.google.googlejavaformat.java.JavaInput (in unnamed module @0x615edab4) cannot access class com.sun.tools.javac.parser.Tokens$TokenKind (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.parser to unnamed module @0x615edab4
        at com.google.googlejavaformat.java.JavaInput.buildToks(JavaInput.java:349)
        at com.google.googlejavaformat.java.JavaInput.buildToks(JavaInput.java:334)
        at com.google.googlejavaformat.java.JavaInput.<init>(JavaInput.java:276)
        at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:280)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:233)
        ... 142 more

EDIT:

EDIT 2 & 3

  • we thought no workaround necessary starting with plugin-gradle 6.5.1 and plugin-maven 2.22.3, but that was incorrect, the workaround is still required

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 30
  • Comments: 35 (19 by maintainers)

Commits related to this issue

Most upvoted comments

@nedtwigg The following can be added to your gradle.properties to work-around the bug:

org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

This can be done in maven via MAVEN_OPTS or ${maven.projectBasedir}/.mvn/jvm.config, see https://maven.apache.org/configure.html

I think this can be worked-around if we somehow add these flags to the JVM which runs Gradle.

  --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \

The deeper fix will be related to #724.

Ah, the workaround was applied to googleJavaFormat and removeUnusedImports, but not to palantirJavaFormat. That has been fixed in plugin-gradle 6.7.2 and plugin-maven 2.22.8.

Another fix has been published in plugin-gradle 6.7.1 and plugin-maven 2.22.7.

Fixed in plugin-gradle 6.5.1 and plugin-maven 2.22.3. Nope, still broken but fixable with workaround, as explained by this comment. 😢

Based on the release notes of google-java-format v1.15.0 the issue is fixed with the latest version.

Any plans to upgrade to google-java-format v1.15.0 soon?

Edit: Unfortunately, the version of google-java-format cannot be configured for the step RemoveUnusedImportsStep.

@dili91 to let jvm below 9 silently accept new flags you can use -XX:+IgnoreUnrecognizedVMOptions at the beginning of your custom JVM args line.

@ZacSweers you can see also #1228 .

You can track Spotless support for toolchain here: #724

@nedtwigg

I think this can be worked-around if we somehow add these flags to the JVM which runs Gradle.

It is worth being wary of doing this, since passing these flags will cause javac for JDK 1.8 to fail, as the flags are unsupported.

Edit: Nevermind! I had an override of jvmargs in my gradle home that was overridding this