Skip to content

Add a Cargo toolchain - #4339

Merged
UebelAndre merged 4 commits into
bazelbuild:mainfrom
UebelAndre:cargo-build-script
Oct 23, 2026
Merged

UebelAndre merged 4 commits into
bazelbuild:mainfrom
UebelAndre:cargo-build-script

Conversation

@UebelAndre

Copy link
Copy Markdown
Collaborator

This change adds a stand-alone toolchain for Cargo. This change aims to help reduce the size of rust_toolchain by removing components not needed by core @rules_rust//rust/....

@UebelAndre
UebelAndre marked this pull request as ready for review October 20, 2026 18:30
@krasimirgg
krasimirgg self-requested a review October 22, 2026 12:57
@UebelAndre
UebelAndre enabled auto-merge October 22, 2026 14:39
Comment thread cargo/BUILD.bazel
visibility = ["//visibility:public"],
)

cargo_toolchain_alias(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks bazel build @rules_rust//cargo/... when no cargo_toolchain is registered. Let's:

  • add tags = ["manual"] (matching current_rust_analyzer_toolchain), and
  • (maybe?) have _cargo_toolchain_alias_impl fall back to rust_toolchain.cargo (temporarily, until fully removed).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took option 2!

Comment thread cargo/private/cargo_build_script.bzl Outdated
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("//cargo:cargo_toolchain.bzl", "cargo_version_mismatch", "find_cargo")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: load these from //cargo/private instead.

(internally we have some bzl_lib consistency checks that are unfortunately not available upstream, and this avoids a potential //cargo:bzl_lib -> //cargo/private:bzl_lib -> //cargo:bzl_lib cycle)

(this currently is not flagged upstream because the deps of bzl_libs are not accurate and there is no enforcement mechanism available)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!


def _crates_vendor_impl(ctx):
toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain_type")]
cargo = find_cargo(ctx, toolchain)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This could be None (and then the cargo.cargo below will fail with unclear error message).
  2. Consider adding a cargo_version_mismatch check.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Comment thread cargo/BUILD.bazel
@@ -1,9 +1,20 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not for this BUILD file but I can't figure out how to leave a comment for a file outside the PR without submitting a partial review, so bear with me...)

rules_rust//rust/toolchain:current_cargo_files still uses toolchain_files(tool = "cargo") (reading the deprecated rust_toolchain.cargo), and internal clients still depend on it. Consider aliasing/deprecating current_cargo_files to //cargo:current_cargo_toolchain (just as current_rustfmt_files was aliased).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rules_rust//rust/toolchain:current_cargo_files is now a deprecated alias

"exec_compatible_with": tc_exec_compatible_with,
"name": companion_name,
"target_compatible_with": tc_target_compatible_with,
"target_settings": target_settings,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this is pre-existing also at new line 1212 above, but pointing it out as I noticed possible inconsistency): this drops channel_target_settings from all_toolchain_details, possibly might be better to update both to "target_settings": channel_target_settings + target_settings.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's preexisting but I fixed it here

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ended up being the source of a bug so I'll defer to another change.

// The deprecated `rust_toolchain.cargo` is a symlink inside the
// generated sysroot, under a `rust_toolchain/` directory.
assert!(
!cargo.contains("/rust_toolchain/"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit
!cargo.replace('\\', "/").contains("/rust_toolchain/") for Windows path compatibility.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@@ -0,0 +1,22 @@
load("//cargo:defs.bzl", "cargo_build_script")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider placing this test package under cargo/tests/cargo_build_script/cargo_toolchain along with other pre-existing cargo tests.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved!

@UebelAndre
UebelAndre requested a review from krasimirgg October 23, 2026 13:47

@krasimirgg krasimirgg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@UebelAndre
UebelAndre added this pull request to the merge queue Oct 23, 2026
Merged via the queue into bazelbuild:main with commit 87b5c63 Oct 23, 2026
17 checks passed
UebelAndre added a commit to UebelAndre/rules_rust that referenced this pull request Nov 6, 2026
This is a followup to bazelbuild#4339
and aims to further minimize `rust_toolchain` by removing components not
strictly required by core `Rustc` actions by moving `rustdoc` and
`clippy` to their own toolchains.
AleDema pushed a commit to AleDema/ic that referenced this pull request Nov 18, 2026
Upgrades `rules_rust` from 0.74.0 to
[0.75.0](https://github.com/bazelbuild/rules_rust/releases/tag/0.75.0),
the latest release.

## Changes

- **`bazel/rust.MODULE.bazel`**: version bump. BCR's `rules_rust/0.75.0`
has no patches of its own, and `rules_rust` is still depended on only by
the root module (`bazel mod explain rules_rust`).
- **`bazel/rules_rust_static_runtime.patch`**: content unchanged. Only
the `index` lines and hunk positions are regenerated, so the patch
applies to the 0.75.0 release tarball with zero fuzz and zero offset
(the old one applied with offsets of up to 102 lines). The patch is
still needed, because 0.75.0 still links the *dynamic* C++ runtime into
dylib/cdylib crates. It also still chooses the runtime in exactly the
three places the patch routes through `cc_runtime_libs`
(`collect_inputs`, `_add_native_link_flags`, and rustdoc's doc-test link
flags). Unlike the last bump, no new selection site appeared that the
patch would miss.
- **`Cargo.Bazel.json.lock`**: repinned with `./bin/bazel-pin.sh
--force`. Only the `checksum` changes, since it covers the `cargo-bazel`
version. The generated crate graph is identical.
- **Four Rust files, via the Autofix commit**: whitespace-only, see
below.

## Formatting changes from the Autofix commit

We don't set `rustfmt_version`, so `bazel run :rustfmt`, which CI's
Autofix job runs, uses rules_rust's default nightly rustfmt. 0.75.0
moves that from `nightly/2026-08-20` to `nightly/2026-10-15` (rustfmt
1.11.0-nightly). The newer nightly indents `*` continuation lines that
start at column 0 in block comments by one space, aligning them under
the `*` of `/*`. Autofix applied that to four files in 82449b5, and
all 50 changed lines differ only by that one space. Without this commit,
every later PR would get the same Autofix commit. Stable `cargo fmt --
--check` (rustfmt 1.10.0-stable, which `ci/scripts/rust-lint.sh` runs)
passes on the result, so the two formatters still agree.

## Upstream changes that touch our setup

- **The rustc 1.99.1 toolchain downloads are now checksum-verified.**
0.74.0's built-in `known_shas.bzl` has no 1.99.x entries and we pass no
`sha256s` to `rust.toolchain`, so those archives were fetched with an
empty `sha256`. 0.75.0 ships their checksums
([bazelbuild/rules_rust#4311](bazelbuild/rules_rust#4311)).
-
[bazelbuild/rules_rust#4347](bazelbuild/rules_rust#4347)
declares the C++ toolchain type through rules_cc's `use_cc_toolchain()`.
With our rules_cc 0.2.22 that is
`@bazel_tools//tools/cpp:toolchain_type`, the same type the
hermetic-llvm wrapper toolchains in `//bazel` register under. This
needed checking because rules_rust requests that type with `mandatory =
False`, so a mismatch would silently resolve no C++ toolchain instead of
failing.
-
[bazelbuild/rules_rust#4339](bazelbuild/rules_rust#4339)
adds a `cargo_toolchain`, and `cargo_build_script` now fails when the
Cargo and Rust toolchains come from different releases. The `rust`
extension registers a matching `cargo_toolchain` next to every
`rust_toolchain`, and we register only 1.99.1, so the two can't
disagree.
-
[bazelbuild/rules_rust#4271](bazelbuild/rules_rust#4271)
changes how legacy doc tests map paths into runfiles: the writer now
strips the root of every action input. That replaces the explicit
runtime-lib handling our hermetic-llvm doc-test links relied on. The
runtime libs still reach those inputs through the patched
`collect_inputs`, so they stay covered.
-
[bazelbuild/rules_rust#4326](bazelbuild/rules_rust#4326)
drops `-lresolv` from the default macOS link flags. No first-party code
calls resolver functions; the macOS CI jobs cover third-party crates.

## Verification

- `bazel build //... --nobuild`: all 3387 targets analyze.
- `//rs/ic_os/networking/nss_icos`, our only `rust_shared_library` and
the case the patch exists for, links the static LLVM libunwind. Its
`_Unwind_*` symbols are defined locally, and every undefined dynamic
symbol is a versioned glibc one.
- All 60 non-manual `rust_doc_test` targets pass.
- These build: `//rs/replica`, `//rs/orchestrator`,
`//rs/pocket_ic_server:pocket-ic-server`, `//rs/canister_sandbox`, the
NNS governance and ICRC-1 ledger canisters (wasm), `//:rustfmt` and
`//:gen_rust_project`.
- CI covers the full build and test suite.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: IDX GitHub Automation <infra+github-automation@dfinity.org>
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.

2 participants