aspect-build / rules_lint

Run static analysis tools with Bazel
Apache License 2.0
109 stars 67 forks source link

[Bug]: When formatting with this rule, Record and other modern Java constructs fail #134

Open hlawrence-cn opened 1 year ago

hlawrence-cn commented 1 year ago

What happened?

Added a Record file in a java project

When the google java formatter is run directly against the file via a java_binary target, it succeeds

When run via the multi_formatter_binary that consumes that target, we see error: class interface or enum expected

Version

Development (host) and target OS/architectures: Mac

Output of bazel --version: bazel 7.0.2

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: bazel_dep(name = "aspect_rules_lint", version = "0.10.0") (Also tried 11.0)

Language(s) and/or frameworks involved: java

How to reproduce

java_binary(
    name = "java-format",
    jvm_flags = [
        "--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",
    ],
    main_class = "com.google.googlejavaformat.java.Main",
    runtime_deps = ["@google-java-format//jar"],
)

multi_formatter_binary(
    name = "format",
    java = ":java-format",
    visibility = ["//visibility:public"],
)

com.my.package

public record MyRecord(String field1, String field2) {

}

running bazel run :java-format -- /path/from/root/to/MyRecord.java, it succeeds without error

running bazel run :format, we receive that error

Any other information?

No response

alexeagle commented 1 year ago

This seems like a bug with rules_java, in that it produces a java_binary that's not fully hermetic. We see a similar problem in https://github.com/aspect-build/rules_lint/actions/runs/8483630419/job/23245094613?pr=189 where it fails like

java.lang.NoClassDefFoundError: com/sun/source/tree/Tree
        at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:74)
        at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:29)
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.sun.source.tree.Tree
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        ... 8 more

I'm not sure how this could be a bug in this repo, though maybe there's some workaround flags needed for Java. @bencodes maybe knows?