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

Verify bootclasspath version is at most compilation runtime version #18343

Closed
wants to merge 1 commit into from

Conversation

fmeum
Copy link
Collaborator

@fmeum fmeum commented May 8, 2023

The bootclasspath is loaded by the Java toolchain's runtime used for compilation and thus needs to be compatible with it, otherwise this triggers errors such as:

error: cannot access module-info
  bad class file: /modules/jdk.net/module-info.class
    class file has wrong version 64.0, should be 61.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
error: cannot access module-info
  bad class file: /modules/jdk.accessibility/module-info.class
    class file has wrong version 64.0, should be 61.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
error: cannot access module-info
  bad class file: /modules/jdk.internal.vm.compiler.management/module-info.class
    class file has wrong version 64.0, should be 61.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.

Work towards #17281

@fmeum
Copy link
Collaborator Author

fmeum commented May 8, 2023

@cushon Could you review this PR? It implements one more version check that wasn't mentioned in #17281, but discovered in #18309.

The tests fail in CI as for some reason I don't understand - and only in CI - Bazel picks up remote JDKs from rules_java, which doesn't include 7556e11. As a side effect, this means that Bzlmod also won't be picking up java_runtime versions.

CC @comius

@fmeum fmeum marked this pull request as ready for review May 9, 2023 14:06
@fmeum fmeum requested a review from lberki as a code owner May 9, 2023 14:06
@fmeum fmeum requested review from cushon and removed request for lberki May 9, 2023 14:06
@github-actions github-actions bot added awaiting-review PR is awaiting review from an assigned reviewer team-Rules-Java Issues for Java rules labels May 9, 2023
@meteorcloudy
Copy link
Member

meteorcloudy commented May 12, 2023

The tests fail in CI as for some reason I don't understand - and only in CI - Bazel picks up remote JDKs from rules_java, which doesn't include 7556e11. As a side effect, this means that Bzlmod also won't be picking up java_runtime versions.

This is due to the CI test setup to reusing remote JDKs fetched by the outter Bazel. Can be reproduced by:

$ bazel build //src:test_repos
$ bazel info output_base
/root/.cache/bazel/_bazel_root/c564d33b2d1d9ff3bd9f69877f355ccc
$ bazel test --test_env=TEST_REPOSITORY_HOME=/root/.cache/bazel/_bazel_root/c564d33b2d1d9ff3bd9f69877f355ccc/external //src/test/shell/bazel:bazel_with_jdk_test

The root cause is the remotejdk_**_for_testing repos are using a wrong BUILD file:

bazel/WORKSPACE

Line 313 in 03ca286

build_file = "@local_jdk//:BUILD.bazel",

Instead, the template at

JDK_BUILD_TEMPLATE = """load("@rules_java//java:defs.bzl", "java_runtime")
should be used, sending a fix now

@meteorcloudy
Copy link
Member

A even better solution is to switch to use rules_java as the only source of truth as discussed in #18373

Copy link
Contributor

@cushon cushon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this generally LGTM

I wondered if there mgiht be any merit to doing this as runtime check. One benefit of that is that it avoids needing to set the version in BootClassPathInfo.

One disadvantage is that we'd have to spend a little time inspecting the bootclasspath before passing it to javac. Another disadvantage is that the error isn't reported to analysis time, but if we can provide a better error than error: cannot access module-info maybe that would be good enough?

@@ -160,6 +161,17 @@ public ConfiguredTarget create(RuleContext ruleContext)

JavaRuntimeInfo javaRuntime = JavaRuntimeInfo.from(ruleContext, "java_runtime");

if (javaRuntime.version() != 0 && javaRuntime.version() < bootclasspath.version()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about using OptionalInt in the Java code here, but I don't have a strong opinion. If starlark is using 0, it might be clearer to be consistent

@cushon cushon requested a review from hvadehra May 12, 2023 23:47
copybara-service bot pushed a commit that referenced this pull request May 15, 2023
Address #18343 (comment)

RELNOTES: None
PiperOrigin-RevId: 532041694
Change-Id: I46fa41073470d042c9955c66b513b899424c77b1
The bootclasspath is loaded by the Java toolchain's runtime used for
compilation and thus needs to be compatible with it, otherwise this
triggers errors such as:

```
error: cannot access module-info
  bad class file: /modules/jdk.net/module-info.class
    class file has wrong version 64.0, should be 61.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
error: cannot access module-info
  bad class file: /modules/jdk.accessibility/module-info.class
    class file has wrong version 64.0, should be 61.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
error: cannot access module-info
  bad class file: /modules/jdk.internal.vm.compiler.management/module-info.class
    class file has wrong version 64.0, should be 61.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
```
fweikert pushed a commit to fweikert/bazel that referenced this pull request May 25, 2023
Address bazelbuild#18343 (comment)

RELNOTES: None
PiperOrigin-RevId: 532041694
Change-Id: I46fa41073470d042c9955c66b513b899424c77b1
@hvadehra
Copy link
Member

Oops, this dropped off my radar somehow. @fmeum could you please rebase and resolve conflicts?

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 15, 2023

Oops, this dropped off my radar somehow. @fmeum could you please rebase and resolve conflicts?

I can no longer reproduce the original failure. I will have to dig into this again.

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 15, 2023

While investigating this, I ran into an actual bug: #19537

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 16, 2023

When #19537 is fixed, I will implement this in JavaBuilder instead, as @cushon recommended. It should be possible to detect the diagnostic raised in this case and add a Bazel-specific note. This has the advantage that we don't fail in setups that work even though we think they shouldn't.

@fmeum
Copy link
Collaborator Author

fmeum commented Sep 21, 2023

At least partially superseded by #19547. The missing case is --java_language_version=8, but that currently doesn't result in an error because of #19537 (comment). Once it does, I will revisit this, but also by extending JavaBuilder.

@fmeum fmeum closed this Sep 21, 2023
@fmeum fmeum deleted the 17281-bootclasspath branch September 21, 2023 10:38
@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Rules-Java Issues for Java rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants