Skip to content

Commit 5ded574

Browse files
Quentin PerezUebelAndre
andauthored
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 <[email protected]>
1 parent 4bda11a commit 5ded574

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

rust/extensions.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ def _rust_impl(module_ctx):
8080
fail("Multiple host_tools were defined in your root MODULE.bazel")
8181

8282
host_triple = get_host_triple(module_ctx)
83+
84+
iso_date = None
85+
version = host_tools.version or rust_common.default_version
86+
87+
# Any version containing a slash is expected to be a nightly/beta release with iso date. E.g. `nightly/2024-03-21`
88+
if "/" in version:
89+
version, _, iso_date = version.partition("/")
90+
8391
rust_toolchain_tools_repository(
8492
name = "rust_host_tools",
8593
exec_triple = host_triple.str,
@@ -90,7 +98,8 @@ def _rust_impl(module_ctx):
9098
rustfmt_version = host_tools.rustfmt_version,
9199
sha256s = host_tools.sha256s,
92100
urls = host_tools.urls,
93-
version = host_tools.version or rust_common.default_version,
101+
version = version,
102+
iso_date = iso_date,
94103
)
95104

96105
for toolchain in toolchains:

rust/repositories.bzl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,37 +301,44 @@ def rust_repositories(**kwargs):
301301
def _rust_toolchain_tools_repository_impl(ctx):
302302
"""The implementation of the rust toolchain tools repository rule."""
303303

304-
check_version_valid(ctx.attr.version, ctx.attr.iso_date)
304+
iso_date = ctx.attr.iso_date
305+
version = ctx.attr.version
306+
version_array = version.split("/")
307+
if len(version_array) > 1:
308+
version = version_array[0]
309+
iso_date = version_array[1]
310+
311+
check_version_valid(ctx.attr.version, iso_date)
305312

306313
exec_triple = triple(ctx.attr.exec_triple)
307314

308315
build_components = [
309316
load_rust_compiler(
310317
ctx = ctx,
311-
iso_date = ctx.attr.iso_date,
318+
iso_date = iso_date,
312319
target_triple = exec_triple,
313-
version = ctx.attr.version,
320+
version = version,
314321
),
315322
load_clippy(
316323
ctx = ctx,
317-
iso_date = ctx.attr.iso_date,
324+
iso_date = iso_date,
318325
target_triple = exec_triple,
319-
version = ctx.attr.version,
326+
version = version,
320327
),
321328
load_cargo(
322329
ctx = ctx,
323-
iso_date = ctx.attr.iso_date,
330+
iso_date = iso_date,
324331
target_triple = exec_triple,
325-
version = ctx.attr.version,
332+
version = version,
326333
),
327334
]
328335

329336
if ctx.attr.rustfmt_version:
330337
rustfmt_version = ctx.attr.rustfmt_version
331338
rustfmt_iso_date = None
332339
if rustfmt_version in ("nightly", "beta"):
333-
if ctx.attr.iso_date:
334-
rustfmt_iso_date = ctx.attr.iso_date
340+
if iso_date:
341+
rustfmt_iso_date = iso_date
335342
else:
336343
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(
337344
ctx.name,
@@ -346,7 +353,7 @@ def _rust_toolchain_tools_repository_impl(ctx):
346353
))
347354

348355
# Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib
349-
include_llvm_tools = ctx.attr.version >= "1.45.0" or (ctx.attr.version == "nightly" and ctx.attr.iso_date > "2020-05-22")
356+
include_llvm_tools = version >= "1.45.0" or (version == "nightly" and iso_date > "2020-05-22")
350357
if include_llvm_tools:
351358
build_components.append(load_llvm_tools(
352359
ctx = ctx,

0 commit comments

Comments
 (0)