Support bzlmod
This is a rough proof of concept of making rules_rust load dependencies via bzlmod. Currently it can only compile and run rust crates, and cannot load crates from Cargo.toml.
There is one new module, examples/bzlmod/hello_world, that depends on the root rules_rust module. This example can be built and run using:
cd examples/bzlmod/hello_world
USE_BAZEL_VERSION=last_green bazel run //:hello_world
Currently toolchain registration has quite bad user experience, because all the toolchains to register have to be listed by the user in the MODULE.bazel file. This is because module extensions cannot directly register toolchains and instead must only expose repositories with toolchains, that are then registered using toolchains_to_register. It would be nice if all the toolchains could be aliased to one label and them all registered together. However, I can't see how this would be possible.
re https://github.com/bazelbuild/rules_rust/issues/1493#issuecomment-1227590691
@scentini do you or anyone you have contact with at Google know anything about bzlmod?
I'm not familiar with bzlmod, in case we're doing something silly here it's not obvious to me. @Wyverald could you PTAL?
cc @meteorcloudy
Bzlmod may have some bugs in Bazel 5.3.0, it's recommended to test with Bazel@HEAD, you can use Bazelisk:
export USE_BAZEL_VERSION=last_green
bazelisk build //:hello_world
I got the following toolchain error instead of the visibility error:
ERROR: /Users/pcloudy/workspace/rules_rust/examples/bzlmod/hello_world/BUILD.bazel:5:12: While resolving toolchains for target //:hello_world: No matching toolchains found for types @rules_rust~override//rust:toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@rules_rust~override//rust:toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
I've updated the PR with a rough proof of concept that can compile and run a rust binary.
Nice, can you also fix the buildifier errors?
Is there any work I can take on here to help this get landed?
@cameron-martin hey, any updates? 😄
@cameron-martin hey, any updates? smile
Sorry I've been busy with other things. I may be able to get back to this in the next week or so.
hello @cameron-martin, do you have any update ? Thanks!
I've got a prototype working for a module extension for bzlmod (changes stacked on top of this PR). It's still very much a WIP, and needs a lot of cleaning up, but you can at least build examples/bzlmod/crates_repository.
https://github.com/matts1/rules_rust/tree/module_extension_prototype
TLDR on how to use:
crates_vendor = use_extension("@rules_rust//:extensions.bzl", "crates_vendor")
crates_vendor.crates_vendor(
manifests = ["//:Cargo.toml"],
cargo_lockfile = "//:Cargo.lock",
lockfile = "//:cargo-bazel-lock.json",
)
use_repo(
crates_vendor,
crates = "crates_crates_repository",
)
I've rebased this PR. Here are the main things that need finishing:
- [x] Support all options when registering toolchains via bzlmod.
- [x] Work out how to run the examples on CI
We also have to consider the API for the module extensions. How many module extensions do we want? Just a single one, called "rust" for everything including crates_universe, etc? One per top-level package, e.g. rust, crates_universe, cargo?
IMO, we should have a seperate module extension per area of concern, similar to how other bzlmod modules do it.
Also, IMO, other types of toolchains don't need to be supported as part of the initial MVP.
This should now be ready to review.
cc @scentini @krasimirgg Hoping one of you two know anything about bzlmod and would be able to review this 😅
I read up a bit on bzlmod and unless someone with in-depth knowledge offers to do the review, I'll claim the 'expert' title. This PR LGTM, and aside from rebasing and updating the version in MODULE.bazel, I'd try merging.
Thank you @Wyverald !
Nice! Will there be the next step to integrate with cargo to actually fetch rust dependencies?
Nice! Will there be the next step to integrate with cargo to actually fetch rust dependencies?
@matts1 currently has a PR for this here: https://github.com/bazelbuild/rules_rust/pull/1910