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

Allow bzlmod users to inhibit toolchain registration #2819

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions rust/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ def _find_modules(module_ctx):

return root, our_module

def _empty_repository_impl(repository_ctx):
repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
repository_ctx.name,
))
repository_ctx.file("BUILD.bazel", "")

_empty_repository = repository_rule(
doc = ("Declare an empty repository."),
implementation = _empty_repository_impl,
)

def _rust_impl(module_ctx):
# Toolchain configuration is only allowed in the root module, or in
# rules_rust.
Expand All @@ -40,18 +51,25 @@ def _rust_impl(module_ctx):
toolchains = root.tags.toolchain or rules_rust.tags.toolchain

for toolchain in toolchains:
rust_register_toolchains(
dev_components = toolchain.dev_components,
edition = toolchain.edition,
allocator_library = toolchain.allocator_library,
rustfmt_version = toolchain.rustfmt_version,
rust_analyzer_version = toolchain.rust_analyzer_version,
sha256s = toolchain.sha256s,
extra_target_triples = toolchain.extra_target_triples,
urls = toolchain.urls,
versions = toolchain.versions,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not modify the implementation of rust_register_toolchains to not register any toolchains when passed versions = []? This way we avoid the extra check and _empty_repository setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There did not seem to be a clean way to get an empty list passed as toolchain_names to toolchain_repository_hub here (https://github.com/bazelbuild/rules_rust/blob/main/rust/repositories.bzl#L252) without nesting most of that function under an if len(versions) > 0:. versions is not considered until inside calls to rust_repository_set and _get_toolchain_repositories. I could add the following at the beginning of rust_register_toolchains to avoid having to declare _empty_repository:

if len(versions) == 0:
    toolchain_repository_hub(
        name = "rust_toolchains",
        toolchain_names = [],
        toolchain_labels = {},
        toolchain_types = {},
        exec_compatible_with = {},
        target_compatible_with = {},
    )

Other refactor suggestions are welcome.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! I think what you have now it cleaner than the toolchain_repository_hub approach.

register_toolchains = False,
)
if len(toolchain.versions) == 0:
# If the root module has asked for rules_rust to not register default
# toolchains, an empty repository named `rust_toolchains` is created
# so that the `register_toolchains()` in MODULES.bazel is still
# valid.
_empty_repository(name = "rust_toolchains")
else:
rust_register_toolchains(
dev_components = toolchain.dev_components,
edition = toolchain.edition,
allocator_library = toolchain.allocator_library,
rustfmt_version = toolchain.rustfmt_version,
rust_analyzer_version = toolchain.rust_analyzer_version,
sha256s = toolchain.sha256s,
extra_target_triples = toolchain.extra_target_triples,
urls = toolchain.urls,
versions = toolchain.versions,
register_toolchains = False,
)

_COMMON_TAG_KWARGS = dict(
allocator_library = attr.string(
Expand Down Expand Up @@ -92,7 +110,8 @@ _RUST_TOOLCHAIN_TAG = tag_class(
versions = attr.string_list(
doc = (
"A list of toolchain versions to download. This paramter only accepts one versions " +
"per channel. E.g. `[\"1.65.0\", \"nightly/2022-11-02\", \"beta/2020-12-30\"]`."
"per channel. E.g. `[\"1.65.0\", \"nightly/2022-11-02\", \"beta/2020-12-30\"]`. " +
"May be set to an empty list (`[]`) to inhibit `rules_rust` from registering toolchains."
),
default = _RUST_TOOLCHAIN_VERSIONS,
),
Expand Down
Loading