Skip to content

[8.5.0] Use comparator when extracting relevant facts for lockfile#27882

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

[8.5.0] Use comparator when extracting relevant facts for lockfile#27882
meteorcloudy merged 1 commit intobazelbuild:release-8.5.0from
bazel-io:cp27816-8.5.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 team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. awaiting-review PR is awaiting review from an assigned reviewer labels Dec 5, 2025
@bazel-io bazel-io requested a review from a team as a code owner December 5, 2025 12:41
@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 addresses a critical stability issue in Bazel's Bzlmod system where the absence of a proper comparator for ModuleExtensionId objects could lead to a ClassCastException and a crash when generating lockfiles. The change ensures that ModuleExtensionId keys are correctly sorted by providing an explicit lexicographic comparator, thereby preventing runtime errors and improving the robustness of the module extension mechanism. A dedicated regression test has been included to safeguard against future occurrences of this bug.

Highlights

  • Bug Fix: Resolves a ClassCastException that occurred when ModuleExtensionId instances were implicitly sorted without a comparator, leading to Bazel crashes during lockfile fact extraction.
  • Comparator Usage: Explicitly introduces ModuleExtensionId.LEXICOGRAPHIC_COMPARATOR when creating ImmutableSortedMap instances to correctly handle ModuleExtensionId keys and prevent sorting-related errors.
  • Regression Test: Adds a new Python test case, testMultipleExtensionsWithFacts, to specifically validate that multiple module extensions with facts can be processed without triggering the ClassCastException.
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 addresses a ClassCastException by explicitly providing ModuleExtensionId.LEXICOGRAPHIC_COMPARATOR when creating an ImmutableSortedMap of facts. This is a crucial fix as ModuleExtensionId does not implement Comparable by itself, which previously led to runtime errors. A new regression test, testMultipleExtensionsWithFacts, has been added to ensure the stability and correctness of this behavior, particularly when multiple extensions with facts are present. The changes are well-justified and correctly implemented.

@malt3
Copy link
Copy Markdown
Contributor

malt3 commented Dec 5, 2025

The CI failure is a timeout and probably an (unrelated) flake.

@meteorcloudy meteorcloudy added this pull request to the merge queue Dec 5, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 5, 2025
@meteorcloudy meteorcloudy added this pull request to the merge queue Dec 5, 2025
Merged via the queue into bazelbuild:release-8.5.0 with commit 3c84606 Dec 5, 2025
47 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