Skip to content

Commit 2a2def8

Browse files
hvadehracopybara-github
authored andcommitted
Fix bootstrapped Bazel binary
For the bootstrap VanillaJavaBuilder, it is important that the AutoValue plugin classes do not end up in the deploy jar. If they do, the processor class is loaded from the app classloader (instead of the processor path classloader). When this happens, AutoValueProcessor uses the app classloader to load extensions (such as auto-value-gson needed by bzlmod), instead of the processor path classloader. Unless all extensions are also correctly present in the app classloder (i.e. in the VanillaJavaBuilder deploy jar), they won't be loaded. Unfortunately, simply adding the jars to the deploy jar is insufficient, we need to also correctly merge the `META-INF/service/...` files as well, which does not happen in our bootstrap java_binary/java_library rules. So, to fix, we remove the auto value plugins from the deploy jar, and use them only to compile our sources that require them. Added a regression test for the crash. We should probably improve it so that we can actually build with the bootstapped Bazel in tests without network access. But I don't think we need block this patch release on that. Fixes #20501 PiperOrigin-RevId: 592266992 Change-Id: I08ac91ee74140df0c4f22ad2553a8c017b497181
1 parent 04c1577 commit 2a2def8

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
21
load("//tools/build_rules:java_rules_skylark.bzl", "bootstrap_java_binary", "bootstrap_java_library")
2+
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
33

44
# Description:
55
# The Java library builders, which are used by Bazel to compile Java
@@ -174,14 +174,14 @@ bootstrap_java_library(
174174
name = "starlark-deps",
175175
srcs = ["//:bootstrap-derived-java-srcs"],
176176
jars = [
177-
"//third_party:auto_value-jars",
178177
"//:bootstrap-derived-java-jars",
179178
"//third_party:bootstrap_guava_and_error_prone-jars",
180179
"//third_party:jsr305-jars",
181180
"//third_party/java/jacoco:core-jars",
182181
"//third_party/grpc-java:bootstrap-grpc-jars",
183182
"//third_party:tomcat_annotations_api-jars",
184183
],
184+
neverlink_jars = ["//third_party:auto_value-jars"],
185185
tags = ["manual"],
186186
)
187187

@@ -213,6 +213,7 @@ bootstrap_java_binary(
213213
"javac/WerrorCustomOption.java",
214214
],
215215
main_class = "com.google.devtools.build.buildjar.VanillaJavaBuilder",
216+
neverlink_jars = ["//third_party:auto_value-jars"],
216217
tags = ["manual"],
217218
deps = [
218219
":starlark-deps",

src/test/shell/bazel/bazel_bootstrap_distfile_test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,22 @@ function test_bootstrap() {
104104

105105
env EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" ./compile.sh \
106106
|| fail "Expected to be able to bootstrap bazel. If you updated MODULE.bazel, see the NOTE in that file."
107+
107108
./output/bazel \
108109
--server_javabase=$JAVABASE --host_jvm_args=--add-opens=java.base/java.nio=ALL-UNNAMED \
109110
version --nognu_format &> "${TEST_log}" \
110111
|| fail "Generated bazel not working"
111112
expect_log "${SOURCE_DATE_EPOCH}"
113+
114+
# TODO: make the build work without network and check for success
115+
# the repo cache is currently missing:
116+
# 1) canonical IDs
117+
# 2) remote jdks & java_tools (if not using a custom java_toolchain)
118+
./output/bazel \
119+
--server_javabase=$JAVABASE --host_jvm_args=--add-opens=java.base/java.nio=ALL-UNNAMED \
120+
build --nobuild --repository_cache=derived/repository_cache \
121+
//src:bazel_nojdk &> "${TEST_log}" || true
122+
expect_not_log "FATAL: bazel crashed due to an internal error"
112123
}
113124

114125
run_suite "bootstrap test"

0 commit comments

Comments
 (0)