rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

rust_analyzer repository registered multiple times

Open nmattia opened this issue 1 month ago • 0 comments

I'm trying to register multiple toolchains with bzlmod (they have different settings, omitted here for simplicity):

rust.toolchain(
    edition = "2024",
    target_settings = [ "@rules_rust//rust/platform:x86_64-apple-darwin" ],
    versions = ["1.90.0"],
)

rust.toolchain(
    edition = "2024",
    target_settings = [ "@rules_rust//rust/platform:aarch64-apple-darwin" ],
    versions = ["1.90.0"],
)

...

Unfortunately this leads to an error:

A repo named rust_analyzer_1.90.0_tools is already generated by this module extension

I think the maybe() call is incorrect, and should be applied to the repository:

diff --git a/rust/repositories.bzl b/rust/repositories.bzl
index 71305a752..b9e21ca88 100644
--- a/rust/repositories.bzl
+++ b/rust/repositories.bzl
@@ -238,8 +238,7 @@ def rust_register_toolchains(
     exec_compatible_with_by_toolchain = {}
     target_compatible_with_by_toolchain = {}
 
-    maybe(
-        rust_analyzer_toolchain_repository,
+    rust_analyzer_toolchain_setup(
         name = rust_analyzer_repo_name,
         version = rust_analyzer_version,
         urls = urls,
@@ -838,7 +837,7 @@ rust_analyzer_toolchain_tools_repository = repository_rule(
     attrs = _RUST_ANALYZER_TOOLCHAIN_TOOLS_REPOSITORY_ATTRS,
 )
 
-def rust_analyzer_toolchain_repository(
+def rust_analyzer_toolchain_setup(
         name,
         version,
         exec_compatible_with = [],
@@ -866,7 +865,8 @@ def rust_analyzer_toolchain_repository(
     Returns:
         str: The name of a registerable rust_analyzer_toolchain.
     """
-    rust_analyzer_toolchain_tools_repository(
+    maybe(
+        rust_analyzer_toolchain_tools_repository,
         name = name + "_tools",
         version = version,
         sha256s = sha256s,

This however I believe only works for WORKSPACE. Even with the fix above, the error is still present when importing rules_rust via MODULE.bazel

nmattia avatar Nov 14 '25 12:11 nmattia