rules_rust archive is fetched by every consumer through register_toolchains("@rust_toolchains//:all")

Open
#4,475 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
20/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
build-system

Research direction

Start with the register_toolchains call in MODULE.bazel (lines 46-53) and the BUILD_for_toolchain generator in rust/private/repository_utils.bzl, which produces the @rust_toolchains hub. The proposal needs a small toolchain-types package that the hub can reference without loading the full @rules_rust archive. Done would mean a consumer that never requests a Rust toolchain no longer fetches @rules_rust+. This is design work across module layout and toolchain resolution, so confirm the approach with maintainers first.

Written by the indexing model from the issue text.

Description

needs-triage

Problem

A consumer that never builds Rust still downloads the whole rules_rust source archive (66.6 MB for 0.75.0, rules_rust-0.75.0.tar.gz) as soon as rules_rust is anywhere in its module graph. A common way in is a transitive non-dev bazel_dep (for example protobuf 37.0-rc3 depends on rules_rust 0.69.0).

The cause is rules_rust's own MODULE.bazel (0.75.0, lines 46-53):

rust = use_extension("//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(rust, "rust_toolchains")

register_toolchains(
    "@rust_toolchains//:all",
)

Bazel resolves every registered toolchain() target when it computes the toolchain candidates, so it loads each toolchain_type, target_settings and constraint label. The @rust_toolchains hub (generated by BUILD_for_toolchain in rust/private/repository_utils.bzl) is load-free, but every entry uses toolchain_type = "@rules_rust//rust:toolchain_type" (and, for the other tool kinds, the matching types in @rules_rust). Reading that package needs the @rules_rust repository, so Bazel fetches and extracts the full archive even though no Rust toolchain is ever requested. This matches how Bazel 9 declares toolchain_type as a label with mandatoryProviders(ToolchainTypeInfo).

Repro

I have not built this standalone; this is derived from reading the sources and from a downstream tracking issue.

# MODULE.bazel
module(name = "repro")
bazel_dep(name = "rules_rust", version = "0.75.0")

Then run bazel fetch //... (or any build that needs toolchain resolution, such as a cc or python target in a larger graph). @rules_rust+ is downloaded and extracted although no target uses it. bazel mod graph shows rules_rust reached as a plain dependency.

Cost

66.6 MB download and about 228 MB extracted for every consumer, including CI cold starts, per output base. It is separate from the rustc/cargo downloads discussed in protocolbuffers/protobuf#30684, which only happen with crate_universe.

Proposed fix

Move what the hub points at out of the big archive:

  1. Put the toolchain type targets (toolchain_type, and the types for rustfmt, cargo, rust_analyzer, rustdoc etc.) in a small module (for example rules_rust_toolchain_types, a few KB, no loads). rules_rust depends on it and re-exports with alias, so existing @rules_rust//rust:toolchain_type labels keep working.
  2. Make the generated hub reference those types and use only @platforms constraints and target_settings that live in the same small module.
  3. The toolchain = targets inside the per-platform repos can keep loading @rules_rust//rust:toolchain.bzl; they are only resolved after a toolchain is selected.

Smaller mitigation: shrink the release tarball (strip examples/, test/, extensions/ unused by consumers).

Assisted by Claude.

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.