Skip to content

[9.0.0] Use comparator when extracting relevant facts for lockfile#27881

Merged
meteorcloudy merged 1 commit intobazelbuild:release-9.0.0from
bazel-io:cp27816-9.0.0
Dec 5, 2025
Merged

[9.0.0] Use comparator when extracting relevant facts for lockfile#27881
meteorcloudy merged 1 commit intobazelbuild:release-9.0.0from
bazel-io:cp27816-9.0.0

Conversation

@bazel-io
Copy link
Copy Markdown
Member

@bazel-io bazel-io commented Dec 5, 2025

ModuleExtensionId is not comparable by itself and needs a comparator. Before this fix, I was able to crash Bazel with this message:

FATAL: bazel crashed due to an internal error. Printing stack trace: java.lang.ClassCastException: class com.google.devtools.build.lib.bazel.bzlmod.ModuleExtensionId cannot be cast to class java.lang.Comparable (com.google.devtools.build.lib.bazel.bzlmod.ModuleExtensionId is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
        at com.google.common.collect.NaturalOrdering.compare(NaturalOrdering.java:29)
        at com.google.common.collect.ImmutableSortedMap.lambda$fromEntries$0(ImmutableSortedMap.java:536)
        at java.base/java.util.TimSort.countRunAndMakeAscending(Unknown Source)
        at java.base/java.util.TimSort.sort(Unknown Source)
        at java.base/java.util.Arrays.sort(Unknown Source)
        at com.google.common.collect.ImmutableSortedMap.fromEntries(ImmutableSortedMap.java:528)
        at com.google.common.collect.ImmutableSortedMap.fromEntries(ImmutableSortedMap.java:495)
        at com.google.common.collect.ImmutableSortedMap.copyOfInternal(ImmutableSortedMap.java:474)
        at com.google.common.collect.ImmutableSortedMap.copyOf(ImmutableSortedMap.java:372)
        at com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileModule.afterCommand(BazelLockFileModule.java:135)
        at com.google.devtools.build.lib.runtime.BlazeRuntime.afterCommand(BlazeRuntime.java:734)
        at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.execExclusively(BlazeCommandDispatcher.java:711)
        at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.exec(BlazeCommandDispatcher.java:257)
        at com.google.devtools.build.lib.server.GrpcServerImpl.executeCommand(GrpcServerImpl.java:607)
        at com.google.devtools.build.lib.server.GrpcServerImpl.lambda$run$0(GrpcServerImpl.java:677)
        at io.grpc.Context$1.run(Context.java:566)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)

Fixes #27522

Closes #27816.

PiperOrigin-RevId: 840671327
Change-Id: Ia7eb05be4fd099f0e2711065faf4c344d45265b5

Commit b2587f6

ModuleExtensionId is not comparable by itself and needs a comparator. Before this fix, I was able to crash Bazel with this message:

```
FATAL: bazel crashed due to an internal error. Printing stack trace: java.lang.ClassCastException: class com.google.devtools.build.lib.bazel.bzlmod.ModuleExtensionId cannot be cast to class java.lang.Comparable (com.google.devtools.build.lib.bazel.bzlmod.ModuleExtensionId is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')
        at com.google.common.collect.NaturalOrdering.compare(NaturalOrdering.java:29)
        at com.google.common.collect.ImmutableSortedMap.lambda$fromEntries$0(ImmutableSortedMap.java:536)
        at java.base/java.util.TimSort.countRunAndMakeAscending(Unknown Source)
        at java.base/java.util.TimSort.sort(Unknown Source)
        at java.base/java.util.Arrays.sort(Unknown Source)
        at com.google.common.collect.ImmutableSortedMap.fromEntries(ImmutableSortedMap.java:528)
        at com.google.common.collect.ImmutableSortedMap.fromEntries(ImmutableSortedMap.java:495)
        at com.google.common.collect.ImmutableSortedMap.copyOfInternal(ImmutableSortedMap.java:474)
        at com.google.common.collect.ImmutableSortedMap.copyOf(ImmutableSortedMap.java:372)
        at com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileModule.afterCommand(BazelLockFileModule.java:135)
        at com.google.devtools.build.lib.runtime.BlazeRuntime.afterCommand(BlazeRuntime.java:734)
        at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.execExclusively(BlazeCommandDispatcher.java:711)
        at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.exec(BlazeCommandDispatcher.java:257)
        at com.google.devtools.build.lib.server.GrpcServerImpl.executeCommand(GrpcServerImpl.java:607)
        at com.google.devtools.build.lib.server.GrpcServerImpl.lambda$run$0(GrpcServerImpl.java:677)
        at io.grpc.Context$1.run(Context.java:566)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
```

Fixes bazelbuild#27522

Closes bazelbuild#27816.

PiperOrigin-RevId: 840671327
Change-Id: Ia7eb05be4fd099f0e2711065faf4c344d45265b5
@bazel-io bazel-io added the team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. label Dec 5, 2025
@bazel-io bazel-io requested a review from a team as a code owner December 5, 2025 12:41
@bazel-io bazel-io added the awaiting-review PR is awaiting review from an assigned reviewer label Dec 5, 2025
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @bazel-io, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request provides a critical fix for a ClassCastException in Bazel's lockfile generation mechanism. The issue stemmed from the incorrect handling of ModuleExtensionId objects, which are not inherently comparable, when used as keys in sorted maps. By explicitly supplying a lexicographic comparator for these IDs, the system can now correctly process and sort module extension facts, preventing crashes and ensuring the integrity of the lockfile. A new test case has been added to verify the robustness of this solution.

Highlights

  • Bug Fix: ClassCastException: This pull request resolves a java.lang.ClassCastException that occurred during Bazel's lockfile generation. The crash happened because ModuleExtensionId objects, which do not implement Comparable, were being used as keys in an ImmutableSortedMap without an explicit comparator, leading to a runtime error when multiple module extensions provided facts.
  • Comparator Implementation: To address the ClassCastException, the ModuleExtensionId.LEXICOGRAPHIC_COMPARATOR is now explicitly provided to the ImmutableSortedMap.copyOf() method. This ensures that ModuleExtensionId keys are correctly sorted based on their lexicographical order, preventing the crash and ensuring stable lockfile generation.
  • Regression Test Added: A new Python test, testMultipleExtensionsWithFacts, has been introduced. This test case specifically validates that Bazel can successfully build and generate a lockfile when multiple module extensions provide facts, confirming that the ClassCastException regression is resolved.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves a ClassCastException that occurs when processing module extensions with facts. The root cause was correctly identified as creating an ImmutableSortedMap with ModuleExtensionId keys without providing a comparator. The fix, which involves supplying the ModuleExtensionId.LEXICOGRAPHIC_COMPARATOR, is direct and correct. The addition of the testMultipleExtensionsWithFacts regression test is excellent, as it specifically targets the scenario that caused the crash and ensures this issue will not reappear. The changes are of high quality.

@meteorcloudy meteorcloudy added this pull request to the merge queue Dec 5, 2025
Merged via the queue into bazelbuild:release-9.0.0 with commit 05b3774 Dec 5, 2025
46 checks passed
@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Dec 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants