bazelbuild / bazelbuild/rules_rust
crate_universe silently bootstraps cargo-bazel with hardcoded DEFAULT_RUST_VERSION when no toolchain is registered, producing confusing downstream errors
- Dominant language
- Starlark
- Stars
- 843
- Forks
- 651
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 15
Description
## Summary
When a Bazel module has no `rust.toolchain(...)` registered via
`register_toolchains`, `crate_universe`'s `pip.parse`-equivalent
(`crate.from_specs` / `crate.spec` extension) silently falls back to
`cargo_bootstrap_repository`, which builds `cargo-bazel` from source using a
hardcoded `DEFAULT_RUST_VERSION` (currently "1.86.0" in rust/private/common.bzl).
If any of cargo-bazel's own pinned transitive dependencies require a newer
Rust language feature than that hardcoded version supports, the build fails
with a confusing compiler error inside a third-party crate deep in the
dependency tree (e.g. time-macros using a syntax feature stabilized after
1.86.0), with no indication that the real issue is "no toolchain was
registered, so we're bootstrapping cargo-bazel with an old pinned version."
## Reproduction
1. Create a Bazel module using bzlmod that depends on rules_rust's
crate_universe extension (e.g. `crate.from_specs(...)`), but does NOT call
`rust.toolchain(...)` / `register_toolchains(...)` anywhere in the module
or its transitive deps.
2. Run `bazel build //...`.
3. Observe that cargo-bazel is built from source via
`cargo_bootstrap_repository`, using DEFAULT_RUST_VERSION, and that this is
not surfaced anywhere in the build output.
4. If cargo-bazel's own Cargo.lock pulls in a crate requiring a newer
language feature, the build fails with an opaque error[E0658] deep in a
[for tool] compilation, with no mention of the toolchain fallback.
## Suggested improvement
Emit a print()/warning when cargo_bootstrap_repository is used because no
explicit toolchain was registered, e.g.:
"No rust.toolchain() registered for this module; bootstrapping cargo-bazel
from source using DEFAULT_RUST_VERSION (X.Y.Z). If the build fails with
confusing compiler errors in a [for tool] target, register an explicit
toolchain version via rust.toolchain(versions=[...])."
This would have saved significant debugging time in our case, where the fix
was simply adding an explicit rust.toolchain(edition="2021",
versions=["1.90.0"]) to MODULE.bazel.
## Environment
- rules_rust 0.68.2-score (eclipse-score fork, unmodified in this code path)
- Bazel 8.6.0
Contributor guide
Research direction
Read rust/private/common.bzl and trace cargo_bootstrap_repository from the crate.from_specs/crate.spec entry points; reproduce with a MODULE.bazel that omits rust.toolchain(...). Done when the fallback emits a warning naming DEFAULT_RUST_VERSION and explaining how to register an explicit toolchain.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- build-system, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100