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

Bootstrapped bazel does not support bzlmod lock file #20501

Closed
avdv opened this issue Dec 12, 2023 · 10 comments
Closed

Bootstrapped bazel does not support bzlmod lock file #20501

avdv opened this issue Dec 12, 2023 · 10 comments
Assignees
Labels
area-Bzlmod Bzlmod-specific PRs, issues, and feature requests P1 I'll work on this now. (Assignee required) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug

Comments

@avdv
Copy link
Contributor

avdv commented Dec 12, 2023

Description of the bug:

When bootstrapping bazel from scratch using a distribution zip archive, the resulting bazel binary's A-server.jar does not contain the *_GsonTypeAdapter.class files which are needed for reading / writing the MODULE.bazel.lock file.

Which category does this issue belong to?

Core

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

  1. download https://github.com/bazelbuild/bazel/releases/download/6.4.0/bazel-6.4.0-dist.zip
  2. mkdir build && cd build
  3. unzip ~/Downloads/bazel-6.4.0-dist.zip
  4. env EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" bash ./compile.sh
  5. unzip output/bazel A-server.jar
  6. unzip -l A-server.jar | grep -F _GsonTypeAdapter.class (this should produce some output, but does not)

In a project with bzlmod enabled, trying to update the MODULE.bazel.lock file:

$ output/bazel mod deps --lockfile_mode=update 
...
$ cat MODULE.bazel.lock
{}

When trying to run inside a project with a valid lock file, bazel crashes:

$ output/bazel mod deps --lockfile_mode=update
FATAL: bazel crashed due to an internal error. Printing stack trace:
java.lang.RuntimeException: Unrecoverable error while evaluating node 'com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileValue$$Lambda$223/0x00000008401fe440@e6399be' (requested by nodes 'com.google.devtools.build.lib.bazel.bzlmod.BazelDepGraphValue$$Lambda$388/0x0000000840452440@260c34d0')
	at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:633)
	at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:365)
	at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Caused by: java.lang.RuntimeException: Failed to invoke public com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileValue() with no args
	at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:113)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
	at com.google.gson.Gson.fromJson(Gson.java:932)
	at com.google.gson.Gson.fromJson(Gson.java:897)
	at com.google.gson.Gson.fromJson(Gson.java:846)
	at com.google.gson.Gson.fromJson(Gson.java:817)
	at com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileFunction.getLockfileValue(BazelLockFileFunction.java:112)
	at com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileFunction.compute(BazelLockFileFunction.java:87)
	at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:562)
	... 7 more
Caused by: java.lang.InstantiationException
	at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:110)
	... 15 more

Which operating system are you running Bazel on?

Ubuntu Linux

What is the output of bazel info release?

n/a

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?

Previously: #19672

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

Contrary to when bootstrapping from scratch, building bazel with bazel works:

$ BAZEL=/usr/bin/bazel ./compile.sh 
🍃  Building Bazel with Bazel.
.2023/12/12 13:06:16 Downloading https://releases.bazel.build/6.4.0/release/bazel-6.4.0-linux-x86_64...
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
...

Target //src:bazel_nojdk up-to-date:
  bazel-bin/src/bazel_nojdk
INFO: Elapsed time: 431.183s, Critical Path: 138.05s
INFO: 2947 processes: 63 internal, 2084 local, 800 worker.
INFO: Build completed successfully, 2947 total actions

Build successful! Binary is here: /home/build/testme/output/bazel

$ unzip output/bazel A-server.jar
Archive:  output/bazel
  inflating: A-server.jar
$ unzip -l A-server.jar | grep -F _GsonTypeAdapter.class
     3981  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/AttributeValues_GsonTypeAdapter.class
     7945  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileValue_GsonTypeAdapter.class
     6648  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/BzlmodFlagsAndEnvVars_GsonTypeAdapter.class
     4605  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/GsonTypeAdapterUtil_RootModuleFileEscapingLocation_GsonTypeAdapter.class
    10559  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/InterimModule_GsonTypeAdapter.class
     6948  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/LockFileModuleExtension_GsonTypeAdapter.class
     3867  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionEvalFactors_GsonTypeAdapter.class
     5393  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionMetadata_GsonTypeAdapter.class
     9548  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionUsage_GsonTypeAdapter.class
     8541  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/Module_GsonTypeAdapter.class
     4684  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/RepoSpec_GsonTypeAdapter.class
     5475  01-01-1980 00:00   com/google/devtools/build/lib/bazel/bzlmod/Tag_GsonTypeAdapter.class
@Wyverald
Copy link
Member

@meteorcloudy any ideas?

@meteorcloudy meteorcloudy self-assigned this Dec 15, 2023
@meteorcloudy meteorcloudy added P1 I'll work on this now. (Assignee required) and removed untriaged labels Dec 15, 2023
@meteorcloudy
Copy link
Member

I'll take a look at this

@meteorcloudy
Copy link
Member

/cc @SalmaSamy since this is related to lockfile

@meteorcloudy
Copy link
Member

meteorcloudy commented Dec 15, 2023

From #20535

The missing classes are generated by an annotation processor, which may not be run during the bootstrap build.

@hvadehra I need some help from a Java expert. Initially I thought this is a problem in the bootstrap java toolchain

But simply adding the relevant jars didn't fix the issue.

diff --git a/third_party/BUILD b/third_party/BUILD
index 912beec0fe..35210f3d7b 100644
--- a/third_party/BUILD
+++ b/third_party/BUILD
@@ -251,6 +251,11 @@ distrib_jar_filegroup(
     srcs = [
         "@maven//:com_google_auto_value_auto_value_annotations_file",
         "@maven//:com_google_auto_value_auto_value_file",
+        "@maven//:com_ryanharter_auto_value_auto_value_gson_runtime_file",
+        "@maven//:com_ryanharter_auto_value_auto_value_gson_extension_file",
+        "@maven//:com_ryanharter_auto_value_auto_value_gson_factory_file",
+        "@maven//:com_squareup_javapoet_file",
+        "@maven//:com_google_code_gson_gson_file",
     ],
     enable_distributions = ["debian"],
 )

And from the issue descriptions, building with an existing bazel binary with the same bootstrap toolchain resolves the problem. So something is missing from the raw bootstrap bazel we compiled from javac? How does annotation processor work?

@meteorcloudy
Copy link
Member

meteorcloudy commented Dec 18, 2023

Found a fix in #20587, somehow the target/source java version matters.

@iancha1992
Copy link
Member

@bazel-io fork 7.0.1

bazel-io pushed a commit to bazel-io/bazel that referenced this issue Dec 19, 2023
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 bazelbuild#20501

PiperOrigin-RevId: 592266992
Change-Id: I08ac91ee74140df0c4f22ad2553a8c017b497181
iancha1992 pushed a commit that referenced this issue Dec 19, 2023
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

Commit
2a2def8

PiperOrigin-RevId: 592266992
Change-Id: I08ac91ee74140df0c4f22ad2553a8c017b497181

Co-authored-by: Googler <[email protected]>
avdv added a commit to avdv/nixpkgs that referenced this issue Dec 20, 2023
This works around an issue with bootstrapping bazel from scratch, see
bazelbuild/bazel#20501

Fixes NixOS#273500
@avdv
Copy link
Contributor Author

avdv commented Dec 20, 2023

Will this fix be backported to 6.x ?

@iancha1992
Copy link
Member

@bazel-io fork 6.5.0

@iancha1992
Copy link
Member

@bazel-io fork 7.1.0

meteorcloudy pushed a commit to meteorcloudy/bazel that referenced this issue Jan 9, 2024
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 bazelbuild#20501

PiperOrigin-RevId: 592266992
Change-Id: I08ac91ee74140df0c4f22ad2553a8c017b497181
github-merge-queue bot pushed a commit that referenced this issue Jan 9, 2024
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

Co-authored-by: Googler <[email protected]>
@iancha1992
Copy link
Member

A fix for this issue has been included in Bazel 6.5.0 RC1. Please test out the release candidate and report any issues as soon as possible. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Bzlmod Bzlmod-specific PRs, issues, and feature requests P1 I'll work on this now. (Assignee required) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants