rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

Support bzlmod

Open cameron-martin opened this issue 3 years ago • 6 comments

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.

cameron-martin avatar Aug 25 '22 17:08 cameron-martin

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?

UebelAndre avatar Aug 27 '22 21:08 UebelAndre

I'm not familiar with bzlmod, in case we're doing something silly here it's not obvious to me. @Wyverald could you PTAL?

scentini avatar Aug 30 '22 11:08 scentini

cc @meteorcloudy

scentini avatar Aug 31 '22 11:08 scentini

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.

meteorcloudy avatar Aug 31 '22 13:08 meteorcloudy

I've updated the PR with a rough proof of concept that can compile and run a rust binary.

cameron-martin avatar Sep 03 '22 14:09 cameron-martin

Nice, can you also fix the buildifier errors?

meteorcloudy avatar Sep 05 '22 10:09 meteorcloudy

Is there any work I can take on here to help this get landed?

benbrittain avatar Dec 16 '22 14:12 benbrittain

@cameron-martin hey, any updates? 😄

UebelAndre avatar Jan 18 '23 16:01 UebelAndre

@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.

cameron-martin avatar Jan 18 '23 17:01 cameron-martin

hello @cameron-martin, do you have any update ? Thanks!

opicaud avatar Feb 11 '23 18:02 opicaud

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",
)

matts1 avatar Feb 16 '23 02:02 matts1

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

cameron-martin avatar Feb 18 '23 14:02 cameron-martin

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?

cameron-martin avatar Feb 18 '23 16:02 cameron-martin

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.

matts1 avatar Feb 20 '23 03:02 matts1

This should now be ready to review.

cameron-martin avatar Feb 23 '23 21:02 cameron-martin

cc @scentini @krasimirgg Hoping one of you two know anything about bzlmod and would be able to review this 😅

UebelAndre avatar Mar 23 '23 15:03 UebelAndre

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.

scentini avatar Mar 28 '23 16:03 scentini

Thank you @Wyverald !

scentini avatar Mar 29 '23 08:03 scentini

Nice! Will there be the next step to integrate with cargo to actually fetch rust dependencies?

meteorcloudy avatar Mar 31 '23 09:03 meteorcloudy

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

cameron-martin avatar Mar 31 '23 10:03 cameron-martin