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

Issue with Java header compilation #19598

Closed
joca-bt opened this issue Sep 22, 2023 · 5 comments
Closed

Issue with Java header compilation #19598

joca-bt opened this issue Sep 22, 2023 · 5 comments
Labels

Comments

@joca-bt
Copy link

joca-bt commented Sep 22, 2023

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

@joca-bt joca-bt changed the title Java cannot find symbols when importing a target with dependency even though that target as no issues using the dependency Issue with Java header compilation Sep 22, 2023
@iancha1992 iancha1992 added the team-Rules-Java Issues for Java rules label Sep 22, 2023
@cushon
Copy link
Contributor

cushon commented Sep 23, 2023

Turbine is resolving JDK APIs from bazel-out/k8-fastbuild/bin/external/rules_java~5.5.0/toolchains/platformclasspath.jar, which doesn't contain com.sun.source.tree.SwitchExpressionTree in this build.

This looks like a DumpPlatformClassPath issue.

The DumpPlatformClassPath action here is running on JDK 11:

external/rules_java~5.5.0~toolchains~remotejdk11_linux/bin/javac -source 8 -target 8 -Xlint:-options -d bazel-out/k8-fastbuild/bin/external/rules_java~5.5.0/toolchains/platformclasspath_classes external/rules_java~5.5.0/toolchains/DumpPlatformClassPath.java

I suspect this the same issue as #19537, which will be fixed by bazelbuild/rules_java@325a9e1

@fmeum
Copy link
Collaborator

fmeum commented Sep 29, 2023

I just realized that this issues makes Bazel unable to compile certain Java 21 language features (e.g. records) even after adding Java runtime and compilation toolchains for this version unless users also set --tool_java_runtime_version.

@hvadehra It would be great if we could get a rules_java release for Bazel 6.4.0 that includes bazelbuild/rules_java@325a9e1.

@fmeum
Copy link
Collaborator

fmeum commented Sep 29, 2023

@bazel-io flag

@bazel-io bazel-io added the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Sep 29, 2023
@iancha1992
Copy link
Member

@bazel-io fork 6.4.0

@bazel-io bazel-io removed the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Sep 29, 2023
copybara-service bot pushed a commit that referenced this issue Oct 13, 2023
Unblocks a `rules_java` update to 6.5.1 or later and thus #18262, #19598, and #19537.

Closes #19791.

PiperOrigin-RevId: 573244082
Change-Id: I9dea195e97045b5755ed206302cec1a11f97c6e2
@fmeum
Copy link
Collaborator

fmeum commented Nov 6, 2023

This is fixed in 6.4.0 and 7.0.0rc2.

@fmeum fmeum closed this as completed Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants