Skip to content

[8.5.0] Inject repositories after root module file processed#27904

Merged
iancha1992 merged 4 commits intobazelbuild:release-8.5.0from
bazel-io:cp27795-8.5.0
Dec 9, 2025
Merged

[8.5.0] Inject repositories after root module file processed#27904
iancha1992 merged 4 commits intobazelbuild:release-8.5.0from
bazel-io:cp27795-8.5.0

Conversation

@bazel-io
Copy link
Member

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

This fixes an issue where repositories injected with --inject_repository can invalidate MODULE.bazel.lock by changing the order innate use_repo_rule extensions are registered.

For example;

# //:MODULE.bazel
local_repository = use_repo_rule("@//:defs.bzl", "local_repository")
local_repository(name = "repo")
bazel mod dump_repo_mapping '' --inject_repository=my_repo=%workspace%/other_repo
# Bazel 9+
# {"my_repo":"+local_repository+my_repo","repo":"+local_repository2+repo",...}
# Bazel 8
# {"my_repo":"+_repo_rules+my_repo","repo":"+_repo_rules2+repo",...}

Handling repository injections later changes how collisions with apparent repository names from innate extensions (use_repo_rule(...)(...)) and module extensions (use_extension(...).__(...)) are reported. To permit debugging (and improve collision investigation in general) the error message now refers to the incoming and existing repo name definition.

For example;

-Error in use_repo: The repo name 'my_repo' is already being used by --inject_repository at <builtin>
+ERROR: The repo name 'my_repo' cannot be defined by --inject_repository at <builtin> as it is already defined by a use_repo() call at /___/MODULE.bazel:2:9

Closes #27795.

PiperOrigin-RevId: 841889374
Change-Id: I01a4500ae5580338f96d593323c1d680fdbb7672

Commit d564fc9

This fixes an issue where repositories injected with `--inject_repository` can invalidate `MODULE.bazel.lock` by changing the order innate `use_repo_rule` extensions are registered.

For example;
```starlark
# //:MODULE.bazel
local_repository = use_repo_rule("@//:defs.bzl", "local_repository")
local_repository(name = "repo")
```
```shell
bazel mod dump_repo_mapping '' --inject_repository=my_repo=%workspace%/other_repo
# Bazel 9+
# {"my_repo":"+local_repository+my_repo","repo":"+local_repository2+repo",...}
# Bazel 8
# {"my_repo":"+_repo_rules+my_repo","repo":"+_repo_rules2+repo",...}
```

Handling repository injections later changes how collisions with apparent repository names from innate extensions (`use_repo_rule(...)(...)`) and module extensions (`use_extension(...).__(...)`) are reported. To permit debugging (and improve collision investigation in general) the error message now refers to the incoming and existing repo name definition.

For example;
```diff
-Error in use_repo: The repo name 'my_repo' is already being used by --inject_repository at <builtin>
+ERROR: The repo name 'my_repo' cannot be defined by --inject_repository at <builtin> as it is already defined by a use_repo() call at /___/MODULE.bazel:2:9
```

Closes bazelbuild#27795.

PiperOrigin-RevId: 841889374
Change-Id: I01a4500ae5580338f96d593323c1d680fdbb7672
@bazel-io bazel-io requested a review from a team as a code owner December 8, 2025 21:20
@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 8, 2025
@bazel-io bazel-io requested a review from fmeum December 8, 2025 21:20
@bazel-io bazel-io added the awaiting-review PR is awaiting review from an assigned reviewer label Dec 8, 2025
@bazel-io bazel-io requested a review from meteorcloudy December 8, 2025 21:20
@gemini-code-assist
Copy link

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 resolves a critical issue where the --inject_repository flag could lead to an invalid MODULE.bazel.lock file by incorrectly altering the order of repository definitions. The core fix involves adjusting the timing of repository injection to happen after the root module file has been processed, ensuring proper resolution. Additionally, the change enhances the clarity of repository name collision error messages, providing more comprehensive information for easier debugging.

Highlights

  • Repository Injection Timing: The processing of repositories injected via --inject_repository has been reordered to occur after the root MODULE.bazel file has been fully processed. This change prevents potential invalidation of MODULE.bazel.lock files caused by an incorrect registration order of innate use_repo_rule extensions.
  • Enhanced Collision Error Messages: Error messages for repository name collisions have been significantly improved. They now provide more detailed context, explicitly stating both the incoming repository definition and the existing definition that caused the collision, aiding in debugging and investigation.
  • New Test Case for Injection Behavior: A new test has been added to specifically verify that --inject_repository does not inadvertently affect repository names generated by use_repo_rule, ensuring the stability and predictability of repository name resolution.
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

@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 an issue where injected repositories could invalidate MODULE.bazel.lock by altering the registration order of use_repo_rule extensions. The fix correctly defers the injection of repositories until after the root module file has been processed, ensuring a stable registration order. Additionally, the error message for repository name collisions has been significantly improved to provide more context, which will aid in debugging. The changes are well-tested, including a new test case that validates the fix for the lock file invalidation side effect. The implementation is clean and effective. I have one minor suggestion to remove a stray comment in a test file.

)

# --inject_repository _must not_ affect `use_repo_rule` generated repo names
#.

Choose a reason for hiding this comment

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

medium

This line appears to be a stray comment or a leftover from debugging. It should be removed to improve code clarity.

@iancha1992 iancha1992 removed the request for review from fmeum December 9, 2025 00:53
@iancha1992 iancha1992 enabled auto-merge December 9, 2025 00:53
@meteorcloudy
Copy link
Member

FYI @Silic0nS0ldier @fmeum

meteorcloudy and others added 3 commits December 9, 2025 10:57
Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
@iancha1992 iancha1992 added this pull request to the merge queue Dec 9, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 9, 2025
@Silic0nS0ldier
Copy link
Contributor

Thanks for the fixes @fmeum!

@meteorcloudy meteorcloudy added this pull request to the merge queue Dec 9, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 9, 2025
@iancha1992 iancha1992 added this pull request to the merge queue Dec 9, 2025
Merged via the queue into bazelbuild:release-8.5.0 with commit 1f9e46f Dec 9, 2025
46 checks passed
@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Dec 9, 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.

5 participants