crate_universe silently bootstraps cargo-bazel with hardcoded DEFAULT_RUST_VERSION when no toolchain is registered, producing confusing downstream errors

Open
#4,210 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
rust

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.

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
Dominant language
Starlark
Stars
857
Forks
701
Avg merge
4d 7h
Merged PRs (30d)
46

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/rules_rust

All issues in bazelbuild/rules_rust

Similar issues

More Build System issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.