Skip to content

Read the Rust version from rust-toolchain.toml in rust.toolchain - #4284

Open
u9g wants to merge 1 commit into
bazelbuild:mainfrom
u9g:rust-toolchain-file
Open

u9g wants to merge 1 commit into
bazelbuild:mainfrom
u9g:rust-toolchain-file

Conversation

@u9g

@u9g u9g commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

A repository that has a rust-toolchain.toml for rustup and cargo has to write the same version a second time in MODULE.bazel, and nothing keeps the two in step (#2753). This is the toml.bzl version of #3792 that @UebelAndre asked for there.

rust.toolchain(rust_toolchain_file = "//:rust-toolchain.toml") reads the file with toml.bzl, which is a new bazel_dep:

  • channel becomes versions. It must be an exact release (1.85.0, nightly-2025-01-01, beta-2025-01-01): rules_rust downloads a fixed release, and resolving stable or 1.85 to one is something rustup does with the network, so those fail with a message saying so.
  • targets are added to extra_target_triples.
  • rustc-dev in components enables dev_components.
  • Setting versions as well is an error, so neither attribute silently does nothing.

module_ctx.read watches the file, so editing it re-evaluates the extension.

Tested with the unit tests in test/unit/rust_toolchain_file, and by pointing examples/hello_world_no_cargo at a rust-toolchain.toml (included, so CI keeps exercising the attribute): with channel = "1.97.0" @rust_toolchains registers rust_analyzer_1.97.0 and only stable toolchains, channel = "1.98" fails with the exact-release message, and adding versions fails with the both-set message.

Fixes #2753

A repository that has a rust-toolchain.toml for rustup and cargo has to
write the same version a second time in MODULE.bazel, and nothing keeps
the two in step. The new rust_toolchain_file attribute on rust.toolchain
reads the file instead, with toml.bzl: channel becomes versions (it must
be an exact release, since rules_rust does not resolve stable or 1.85 the
way rustup does), targets add to extra_target_triples, and rustc-dev in
components enables dev_components. Setting versions as well is an error.

Fixes bazelbuild#2753
@u9g
u9g force-pushed the rust-toolchain-file branch from 9f3825f to b03f74e Compare September 25, 2026 08:54

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for toolchain version from rust-toolchain.toml file

1 participant