bzlmod: fix issue with nightly versions (#2545)
Hi there 👋
I tried using bzlmod with rules_rust, and I encountered an issue with
the download of the toolchains.
I applied a "hotfix," but I'm sure it's not the correct approach. Hence,
feel free to take over the PR.
```
bazel_dep(name = "rules_rust", version = "0.40.0")
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = "2021",
extra_target_triples = [
"aarch64-apple-ios-sim",
"aarch64-apple-ios",
"aarch64-linux-android",
"x86_64-unknown-linux-gnu",
],
rustfmt_version = "nightly/2024-02-22",
versions = ["nightly/2024-02-22"],
)
crate = use_extension(
"@rules_rust//crate_universe:extension.bzl",
"crate",
)
crate.from_cargo(
name = "crates",
cargo_lockfile = "//:Cargo.lock",
manifests = [
"//:Cargo.toml",
],
)
use_repo(crate, "crates")
```
---------
Co-authored-by: UebelAndre <github@uebelandre.com>
diff --git a/rust/extensions.bzl b/rust/extensions.bzl
index f45a557..77b6920 100644
--- a/rust/extensions.bzl
+++ b/rust/extensions.bzl
@@ -80,6 +80,14 @@
fail("Multiple host_tools were defined in your root MODULE.bazel")
host_triple = get_host_triple(module_ctx)
+
+ iso_date = None
+ version = host_tools.version or rust_common.default_version
+
+ # Any version containing a slash is expected to be a nightly/beta release with iso date. E.g. `nightly/2024-03-21`
+ if "/" in version:
+ version, _, iso_date = version.partition("/")
+
rust_toolchain_tools_repository(
name = "rust_host_tools",
exec_triple = host_triple.str,
@@ -90,7 +98,8 @@
rustfmt_version = host_tools.rustfmt_version,
sha256s = host_tools.sha256s,
urls = host_tools.urls,
- version = host_tools.version or rust_common.default_version,
+ version = version,
+ iso_date = iso_date,
)
for toolchain in toolchains:
diff --git a/rust/repositories.bzl b/rust/repositories.bzl
index 77ce548..0a32957 100644
--- a/rust/repositories.bzl
+++ b/rust/repositories.bzl
@@ -301,28 +301,35 @@
def _rust_toolchain_tools_repository_impl(ctx):
"""The implementation of the rust toolchain tools repository rule."""
- check_version_valid(ctx.attr.version, ctx.attr.iso_date)
+ iso_date = ctx.attr.iso_date
+ version = ctx.attr.version
+ version_array = version.split("/")
+ if len(version_array) > 1:
+ version = version_array[0]
+ iso_date = version_array[1]
+
+ check_version_valid(ctx.attr.version, iso_date)
exec_triple = triple(ctx.attr.exec_triple)
build_components = [
load_rust_compiler(
ctx = ctx,
- iso_date = ctx.attr.iso_date,
+ iso_date = iso_date,
target_triple = exec_triple,
- version = ctx.attr.version,
+ version = version,
),
load_clippy(
ctx = ctx,
- iso_date = ctx.attr.iso_date,
+ iso_date = iso_date,
target_triple = exec_triple,
- version = ctx.attr.version,
+ version = version,
),
load_cargo(
ctx = ctx,
- iso_date = ctx.attr.iso_date,
+ iso_date = iso_date,
target_triple = exec_triple,
- version = ctx.attr.version,
+ version = version,
),
]
@@ -330,8 +337,8 @@
rustfmt_version = ctx.attr.rustfmt_version
rustfmt_iso_date = None
if rustfmt_version in ("nightly", "beta"):
- if ctx.attr.iso_date:
- rustfmt_iso_date = ctx.attr.iso_date
+ if iso_date:
+ rustfmt_iso_date = iso_date
else:
fail("`rustfmt_version` does not include an iso_date. The following reposiotry should either set `iso_date` or update `rustfmt_version` to include an iso_date suffix: {}".format(
ctx.name,
@@ -346,7 +353,7 @@
))
# Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib
- include_llvm_tools = ctx.attr.version >= "1.45.0" or (ctx.attr.version == "nightly" and ctx.attr.iso_date > "2020-05-22")
+ include_llvm_tools = version >= "1.45.0" or (version == "nightly" and iso_date > "2020-05-22")
if include_llvm_tools:
build_components.append(load_llvm_tools(
ctx = ctx,