Skip to content

JDK16+ --add-exports not working as expected #399

Closed
@elkkhan

Description

@elkkhan

Hi all,

I'm trying dig into internals of Error Prone using this library - since Error Prone is using JDK internals, I'm passing --add-exports for those modules, but this doesn't seem to work as expected.
Same --add-exports work when passing them in through Maven.

Example code below:

    final Compiler compiler = Compiler.javac().withOptions(List.of(
            "--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.code=ALL-UNNAMED",
            "--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
            "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
            "--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
            "--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
            "--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
            "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
            "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
            "--add-exports=jdk.compiler/com.sun.source.tree=ALL-UNNAMED",
            "-Xplugin:ErrorProne",
            "-XDcompilePolicy=simple"))
        .withClasspath(List.of(new File("target/classes")));
    final Compilation compiled = compiler.compile(JavaFileObjects.forResource("Victim.java"));

Stacktrace:

An exception has occurred in the compiler (21.0.1). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.IllegalAccessError: class com.google.errorprone.BaseErrorProneJavaCompiler (in unnamed module @0x1d548a08) cannot access class com.sun.tools.javac.api.BasicJavacTask (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.api to unnamed module @0x1d548a08
	at com.google.errorprone.BaseErrorProneJavaCompiler.addTaskListener(BaseErrorProneJavaCompiler.java:92)
	at com.google.errorprone.ErrorProneJavacPlugin.init(ErrorProneJavacPlugin.java:34)
	at jdk.compiler/com.sun.tools.javac.api.BasicJavacTask.initPlugin(BasicJavacTask.java:256)
	at jdk.compiler/com.sun.tools.javac.api.BasicJavacTask.initPlugins(BasicJavacTask.java:230)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.prepareCompiler(JavacTaskImpl.java:204)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:101)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(JavacTaskImpl.java:152)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
	at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
	at com.google.testing.compile.Compiler.compile(Compiler.java:201)
	at com.google.testing.compile.Compiler.compile(Compiler.java:173)
	at org.offen.compiletest.CompilationLab.main(CompilationLab.java:27)
Exception in thread "main" com.google.testing.compile.CompilationFailureException: compilation of [/Users/elkhaneminov/private/errorprone-idea/victim/target/classes/Victim.java] passing options [--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.code=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED, --add-exports=jdk.compiler/com.sun.source.tree=ALL-UNNAMED, -Xplugin:ErrorProne, -XDcompilePolicy=simple] failed, but did not report any error diagnostics or throw any exceptions. This behavior has been observed in older versions of javac, which swallow exceptions and log them on System.err. Check there for more information.
	at com.google.testing.compile.Compiler.compile(Compiler.java:210)
	at com.google.testing.compile.Compiler.compile(Compiler.java:173)
	at org.offen.compiletest.CompilationLab.main(CompilationLab.java:27)

Might be worth to note that all the options do get passed into the JavacTask - I debugged the working maven compile with mvndebug and the Options collection looks exactly the same in the happy path too.

Screenshot 2024-01-07 at 04 23 04

In this particular example I'm using JDK 21, but I've also tried with 17 and no luck.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions