Skip to content

Issue with Java header compilation #19598

@joca-bt

Description

@joca-bt

Description of the bug:

Java cannot find symbols from com.sun.source.tree when importing a target which depends on com.sun.source even though that target as no issues compiling on itself.

See the example below for an example. :a compiles fine but :b fails. For packages under com.sun.tools.javac adding --add-exports=jdk.compiler/com.sun.tools.javac.<package>=ALL-UNNAMED to javacopts addresses the issue. However, doing the similar thing for this package has no effect.

I think it's something related with header compilation since when header compilation is disabled it seems to work fine (--nojava_header_compilation).

.bazelrc --------------------------------------------------

build --java_language_version=17
build --java_runtime_version=remotejdk_17

build --javacopt=-Xlint:all
build --javacopt=-Xlint:-processing
build --javacopt=-Werror

common --enable_bzlmod
build --symlink_prefix=.bazel/

BUILD --------------------------------------------------

java_library(name = "a", srcs = [ "A.java" ])

java_library(name = "b", srcs = [ "B.java" ], deps = [ ":a" ] )

A.java --------------------------------------------------

package test;

import com.sun.source.tree.SwitchExpressionTree;

public class A {
    public SwitchExpressionTree get(SwitchExpressionTree node) {
        System.out.println(node);
        return node;
    }
}

B.java --------------------------------------------------

package test;

public class B {}

--------------------------------------------------

> bazel build :a
INFO: Analyzed target //:a (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:a up-to-date:
  .bazel/bin/liba.jar
INFO: Elapsed time: 0.091s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

> bazel build :b
INFO: Analyzed target //:b (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: BUILD:1:13: Compiling Java headers liba-hjar.jar (1 source file) failed: (Exit 1): java failed: error executing command (from target //:a) external/rules_java~5.3.5~toolchains~remotejdk17_linux/bin/java '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' ... (remaining 40 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
A.java:3: error: symbol not found com.sun.source.tree.SwitchExpressionTree
import com.sun.source.tree.SwitchExpressionTree;
       ^
A.java:6: error: could not resolve SwitchExpressionTree
    public SwitchExpressionTree get(SwitchExpressionTree node) {
           ^
A.java:6: error: could not resolve SwitchExpressionTree
    public SwitchExpressionTree get(SwitchExpressionTree node) {
                                    ^
Target //:b failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: BUILD:8:13 Building libb.jar (1 source file) failed: (Exit 1): java failed: error executing command (from target //:a) external/rules_java~5.3.5~toolchains~remotejdk17_linux/bin/java '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' ... (remaining 40 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
INFO: Elapsed time: 0.397s, Critical Path: 0.31s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

Which category does this issue belong to?

Java Rules

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

No response

Which operating system are you running Bazel on?

Ubuntu

What is the output of bazel info release?

release 6.0.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions