Skip to content

Commit 0d6aac3

Browse files
authored
Allow crate_universe to use external rust_host_tools (#3418)
This allows users of `rules_rust` to define their own `rust_host_tools` modules and use it with `crate_universe` to control the versions of rustc and cargo used by it. For example: ```python rust_host_tools = use_extension("@rules_rust//rust:extensions.bzl", "rust_host_tools") rust_host_tools.host_tools( name = "my_rust_host_tools", version = "1.86.0", ) use_repo( rust_host_tools, "my_rust_host_tools", ) crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate") crate.from_cargo( name = "crate_index", manifests = ["//:Cargo.toml"], cargo_lockfile = "//:Cargo.lock", lockfile = "//:cargo-bazel-lock.json", host_tools = "@my_rust_host_tools", ) use_repo( crate, "crate_index", ) ```
1 parent 49a718d commit 0d6aac3

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

MODULE.bazel

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,9 @@ rust_host_tools = use_extension("//rust:extensions.bzl", "rust_host_tools")
5656
rust_host_tools.host_tools(
5757
name = "rust_host_tools",
5858
)
59-
rust_host_tools.host_tools(
60-
name = "rust_host_tools_nightly",
61-
version = "nightly",
62-
)
6359
use_repo(
6460
rust_host_tools,
6561
"rust_host_tools",
66-
"rust_host_tools_nightly",
6762
)
6863

6964
rust_test = use_extension("//test:test_extensions.bzl", "rust_test", dev_dependency = True)

crate_universe/extensions.bzl

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ You could use the syntax specified in the above document to place it in `Cargo.t
195195
196196
This method has the following consequences:
197197
* if you use shared dependency tree with your project these binary dependencies will interfere with yours (may conflict)
198-
* you have to use nightly `host_tools_repo` to generate dependencies because
198+
* you have to use nightly `host_tools` to generate dependencies because
199199
200200
Alternatively you can specify this in a separate `repo` with `cargo.from_specs` syntax:
201201
@@ -207,7 +207,7 @@ bindeps.annotation(crate = "cargo-machete", gen_all_binaries = True)
207207
208208
bindeps.from_specs(
209209
name = "bindeps",
210-
host_tools_repo = "rust_host_tools_nightly",
210+
host_tools = "@rust_host_tools_nightly",
211211
)
212212
213213
use_repo(bindeps, "bindeps")
@@ -835,21 +835,14 @@ def _get_host_cargo_rustc(module_ctx, host_triple, host_tools_repo):
835835
"""A helper function to get the path to the host cargo and rustc binaries.
836836
837837
Args:
838-
module_ctx: The module extension's context.
839-
host_triple: The platform triple for the machine executing this extension.
840-
host_tools_repo: The `rust_host_tools` repository to use.
838+
module_ctx (module_ctx): The module extension's context.
839+
host_triple (triple): The platform triple for the machine executing this extension.
840+
host_tools_repo (Label): The `rust_host_tools` repository to use.
841841
Returns:
842-
A tuple of path to cargo, path to rustc.
842+
tuple[Path, Path]: A tuple of path to cargo, path to rustc.
843843
"""
844844
binary_ext = system_to_binary_ext(host_triple.system)
845-
846-
# For Bazel 7 and below, `module_ctx.path` will also watch files so to avoid
847-
# volatility in lock files caused by referencing host specific files, direct
848-
# references are avoided. Note that `BUILD.bazel` is not used as it also
849-
# contains host specific data.
850-
root = module_ctx.path(Label("@{rust_host_tools}//:WORKSPACE.bazel".format(
851-
rust_host_tools = host_tools_repo,
852-
)))
845+
root = module_ctx.path(host_tools_repo)
853846

854847
cargo_path = root.dirname.get_child("bin/cargo{}".format(binary_ext))
855848
rustc_path = root.dirname.get_child("bin/rustc{}".format(binary_ext))
@@ -972,7 +965,7 @@ def _crate_impl(module_ctx):
972965
for m in cfg.manifests:
973966
module_ctx.watch(m)
974967

975-
cargo_path, rustc_path = _get_host_cargo_rustc(module_ctx, host_triple, cfg.host_tools_repo)
968+
cargo_path, rustc_path = _get_host_cargo_rustc(module_ctx, host_triple, cfg.host_tools)
976969
cargo_bazel_fn = new_cargo_bazel_fn(
977970
repository_ctx = module_ctx,
978971
cargo_bazel_path = generator,
@@ -1037,9 +1030,9 @@ _FROM_COMMON_ATTRS = {
10371030
"cargo_lockfile": CRATES_VENDOR_ATTRS["cargo_lockfile"],
10381031
"generate_binaries": CRATES_VENDOR_ATTRS["generate_binaries"],
10391032
"generate_build_scripts": CRATES_VENDOR_ATTRS["generate_build_scripts"],
1040-
"host_tools_repo": attr.string(
1041-
doc = "The name of the `rust_host_tools` repository to use.",
1042-
default = "rust_host_tools",
1033+
"host_tools": attr.label(
1034+
doc = "The `rust_host_tools` repository to use.",
1035+
default = "@rust_host_tools",
10431036
),
10441037
"isolated": attr.bool(
10451038
doc = (

examples/crate_universe/MODULE.bazel

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,23 @@ use_repo(
263263
# C A R G O B I N D E P S
264264
###############################################################################
265265

266+
rust_host_tools = use_extension("@rules_rust//rust:extensions.bzl", "rust_host_tools")
267+
rust_host_tools.host_tools(
268+
name = "rust_host_tools_nightly",
269+
version = "nightly",
270+
)
271+
use_repo(
272+
rust_host_tools,
273+
"rust_host_tools_nightly",
274+
)
275+
266276
# https://bazelbuild.github.io/rules_rust/crate_universe_bzlmod.html
267277
crate_index_cargo_bindeps = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
268278
crate_index_cargo_bindeps.from_cargo(
269279
name = "crate_index_cargo_bindeps",
270280
cargo_lockfile = "//cargo_bindeps:Cargo.lock",
271281
generate_binaries = True,
272-
host_tools_repo = "rust_host_tools_nightly",
282+
host_tools = "@rust_host_tools_nightly",
273283
manifests = ["//cargo_bindeps:Cargo.toml"],
274284
)
275285
use_repo(

rust/repositories.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,14 @@ def _rust_toolchain_tools_repository_impl(ctx):
526526
)
527527
sha256s.update(rustc_dev_sha256)
528528

529-
ctx.file("WORKSPACE.bazel", "")
529+
ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
530+
ctx.name,
531+
))
530532
ctx.file("BUILD.bazel", "\n".join(build_components))
531533

534+
# Used to locate `rust_host_tools` repositories.
535+
ctx.file(ctx.name, "")
536+
532537
repro = {"name": ctx.name}
533538
for key in _RUST_TOOLCHAIN_REPOSITORY_ATTRS:
534539
repro[key] = getattr(ctx.attr, key)

0 commit comments

Comments
 (0)