Repository navigation
fix(rustdoc): link doc tests against the cc_toolchain's runtime libs - #4220
Merged
illicitonion merged 5 commits intoAug 19, 2026
Merged
illicitonion merged 5 commits into
illicitonion merged 5 commits into
Conversation
…untime libs
Adds a failing analysis test for `rust_doc_test` under a cc_toolchain that
supplies its C++/unwind runtime through `static_runtime_lib`.
NOTE: this test fails on its own. It is committed separately to record the
reproduction; the fix follows in the next commit.
What breaks
-----------
On a toolchain enabling `static_link_cpp_runtimes`, doc tests fail to link:
ld.lld: error: undefined symbol: _Unwind_Resume
>>> referenced by alloc.rs:0 ... liballoc-*.rlib
... and the rest of the _Unwind_* family
`rust_library` / `rust_binary` / `rust_test` are unaffected, which makes the
failure look toolchain-specific when it is not.
The test extends the fixture added in #3741, which already declares a
cc_toolchain with `static_runtime_lib = ":dummy.a"` and the
`static_link_cpp_runtimes` feature enabled -- exactly the configuration needed
to observe this. It asserts that the doc test's rustdoc action names the
runtime lib on the command line (`-Clink-arg=-ldummy`).
Today it does not. The action's argv shows the search path arriving while the
library reference never does:
"-Lnative=test/unit/cc_toolchain_runtime_lib", <- present
... <- no -Clink-arg=-ldummy
That asymmetry is the bug: `add_native_link_flags` emits the runtime libs'
`-Lnative=` unconditionally but gates the matching `-lstatic=` behind
`include_link_flags`, which rustdoc sets to False (#2467). #4080 added a
compensating `-Clink-arg=-l` loop for doc tests, but it iterates only
`dep_info.transitive_noncrates`, which never contains the toolchain runtime
libs: Bazel injects those into C++ link actions, and rustdoc never runs one --
it drives the doc test link itself. The archives end up on the search path with
nothing referencing them.
A system GNU toolchain hides this, because the `-lgcc_s` rustc emits resolves
to a real libgcc_s carrying the `_Unwind_*` symbols. Toolchains shipping their
own unwinder tend to make `-lgcc_s` / `-lunwind` resolve to empty stub archives
so third-party build systems do not fail on a missing library, which is what
turns the omission into a link error.
Fixes the reproduction added in the previous commit: `//test/unit/cc_toolchain_runtime_lib:runtime_libs_test/doc_test` now passes. Two parts, both required. 1. `rustdoc.bzl` extends the #4080 `-Clink-arg=-l` loop to the cc_toolchain runtime libs, mirroring the crate-type split in `collect_inputs` so the libs it names are the ones that were added to the action inputs. Guarded on `cc_toolchain` being present, since #3665 made it optional. The libs are returned in the action struct for use by (2). 2. `rustdoc_test.bzl` adds those libs' root to the `--strip_substring` list. Without this the `-l` alone still fails with "unable to find library": the runtime libs are built in a different configuration than the crate outputs, so their root is not among the crate roots already collected, and the `-Lnative=` path stays an execroot path that does not exist in the runfiles tree the doc test runs from. Roots are only added when non-empty. Source-file runtime libs have an empty root and need no stripping -- their `-Lnative=` path is already workspace-relative -- and an empty root would emit `--strip_substring=/`. `rustdoc_test_writer` applies these as plain `str::replace`, so that would delete every `/` in every argument. The libs already reach the doc test's runfiles via `ctx.runfiles(transitive_files = action.inputs)`, thanks to #3741, so no additional wiring is needed there. Verified on macOS with `bazel test -- //... -//test/unit/remap_path_prefix:integration_test`: 569 tests pass and 35 are skipped, against 568/35 before this series. The only target that changed status is the new doc test. Every other action was a cache hit, so the change is a provable no-op for toolchains whose `static_runtime_lib` is empty.
Fixes the reproduction added in the previous commit: `//test/unit/cc_toolchain_runtime_lib:runtime_libs_test/doc_test` now passes. Two parts, both required. 1. `rustdoc.bzl` extends the #4080 `-Clink-arg=-l` loop to the cc_toolchain runtime libs, mirroring the crate-type split in `collect_inputs` so the libs it names are the ones that were added to the action inputs. Guarded on `cc_toolchain` being present, since #3665 made it optional. The libs are returned in the action struct for use by (2). 2. `rustdoc_test.bzl` adds those libs' root to the `--strip_substring` list. Without this the `-l` alone still fails with "unable to find library": the runtime libs are built in a different configuration than the crate outputs, so their root is not among the crate roots already collected, and the `-Lnative=` path stays an execroot path that does not exist in the runfiles tree the doc test runs from. Roots are only added when non-empty. Source-file runtime libs have an empty root and need no stripping -- their `-Lnative=` path is already workspace-relative -- and an empty root would emit `--strip_substring=/`. `rustdoc_test_writer` applies these as plain `str::replace`, so that would delete every `/` in every argument. The libs already reach the doc test's runfiles via `ctx.runfiles(transitive_files = action.inputs)`, thanks to #3741, so no additional wiring is needed there. Verified on macOS with `bazel test -- //... -//test/unit/remap_path_prefix:integration_test`: 569 tests pass and 35 are skipped, against 568/35 before this series. The only target that changed status is the new doc test. Every other action was a cache hit, so the change is a provable no-op for toolchains whose `static_runtime_lib` is empty.
…lib' into fix-rustdoc-test-static-runtime-lib
The doc test assertion added alongside the runtime-lib fix hardcoded
`-Clink-arg=-ldummy`, which fails on Windows CI:
error: expected '-Clink-arg=-ldummy' in the rustdoc test link args:
'[..., "-Clink-arg=dummy"]'
The fix itself works there -- `rustdoc.bzl` omits the `-l` prefix when the
target ABI is msvc, where link.exe takes bare library names, matching the
existing #4080 loop. Only the test was wrong.
Signed-off-by: Marvin Hansen <[email protected]>
marvin-hansen
added a commit
to marvin-hansen/deep_causality
that referenced
this pull request
Aug 12, 2026
Fix a broken MODULE.bazel
-------------------------
`toolchain.version(llvm_version = ...)` is not valid: the `toolchain`
extension only accepts `exec` and `target` tags. Every build failed analysis
with "does not have a tag class named version". The LLVM version belongs to the
separate `llvm` extension, so it moves to `llvm.version()`. 22.1.6 is a version
the module actually publishes.
Vendor the rule sources
-----------------------
Check the five registry modules into thirdparty/ behind local_path_override:
aspect_bazel_lib, bazel_skylib, platforms, toolchains_buildbuddy and llvm.
5.4 MB in total -- these are Starlark only.
Two things worth knowing about how they were vendored:
* Each tree is copied from the RESOLVED external repo, not from an upstream
release tarball. aspect_bazel_lib, toolchains_buildbuddy and llvm each
carry a BCR remote_patch; downloading from GitHub would silently drop it.
* The vendored trees are added to .bazelignore. Without that, `//...` tries
to load them as packages of the root module and fails -- bazel_skylib's
internal labels such as `//lib:unittest.bzl` resolve against the root
workspace rather than against the module.
build/scripts/vendor_modules.sh re-vendors them after a version bump, and
refuses to run while the overrides are active (they would otherwise resolve to
the tree being overwritten instead of to the registry).
This is deliberately limited to rule sources. The toolchains these rules
configure -- Rust ~1.8 GB, LLVM ~1 GB, Lean/Mathlib ~10 GB -- arrive through
module extensions, which local_path_override cannot vendor; those stay in the
CI repository cache. Vendoring rule sources removes ~54 MB of a ~12 GB cold
fetch, so the value here is not fetch time but not depending on the registry
being reachable to build at all.
rules_rust is not vendored: it is pinned to a fork through git_override while
bazelbuild/rules_rust#4220 is open, and the two override kinds are mutually
exclusive.
Also
----
The CI test step ran under --config=remote, so it never picked up the ROLE and
USER build metadata that --config=ci sets; both steps now use --config=ci and
BuildBuddy can tell CI runs from developer machines. Drops the .bazelignore
entry for the removed thirdparty/rules_astro.
Verified with `bazel test --config=ci //...`: 1153 tests pass, covering the
Lean proofs and all 27 doc tests.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Marvin Hansen <[email protected]>
pull Bot
pushed a commit
to mikeyhodl/ic
that referenced
this pull request
Sep 11, 2026
Upgrades `rules_rust` from 0.71.3 to 0.74.0, stops depending on the `dfinity/rules_rust` fork, and drops two local patches that 0.74.0 makes redundant. ## Drops the fork The pin was an `archive_override` on `dfinity/rules_rust`, whose sole commit adds a `strip_level_select` tag class ([bazelbuild/rules_rust#4167](bazelbuild/rules_rust#4167), still open upstream). That tag's only caller was removed in 9e29205 when hermetic-llvm replaced zig cc — clang emits deterministic debug info, so the `strip_level_select(none)` workaround went away with it. `strip_level` now appears nowhere in the repo outside the patch file, so the fork is dead code. ```diff -bazel_dep(name = "rules_rust", version = "0.71.3") -archive_override( +bazel_dep(name = "rules_rust", version = "0.74.0") +single_version_override( module_name = "rules_rust", - integrity = "sha256-5GXwZtp5VlNxMI+dolYYxhyhG5f5ReZ5qz30gAbPsrg=", patch_strip = 1, patches = ["//bazel:rules_rust_static_runtime.patch"], - strip_prefix = "rules_rust-f8f382c7e351a797242ce4a92262342f8994e265", - type = "tar.gz", - urls = ["https://codeload.github.com/dfinity/rules_rust/tar.gz/f8f382c7e351a797242ce4a92262342f8994e265"], ) ``` No integrity hash, no `strip_prefix`, no URL, and no fork to rebase on every future release — a bump is now a one-line version change. Checked before switching: BCR's `rules_rust/0.74.0/source.json` carries no patches of its own, and `rules_rust` is depended on **only by the root module** (`bazel mod explain rules_rust`), so nothing else can pull MVS off 0.74.0. ## Regenerates `bazel/rules_rust_static_runtime.patch` The `rust_doc_test` half of the patch is **upstream as of 0.74.0** ([dfinity#4220](bazelbuild/rules_rust#4220), commit `ed7afd2fc`) and is a superset of ours — it threads the runtime libs through `rustdoc_compile_action`'s return value rather than recomputing them, adds an empty-lib-root guard we lacked (an empty root would emit `--strip_substring=/`, which the writer applies as a plain string replacement), and ships a unit test. So the patch no longer carries it. What remains is the dylib/cdylib static-runtime preference, which is still not upstream and has no replacement mechanism (no new `unwindlib`, toolchain or rule attribute controls C++ runtime linkage in this range). dfinity#4220 also introduced a **new** site in `rustdoc.bzl` that picks the dynamic runtime for dylib/cdylib, which the old patch would have missed — leaving a cdylib doc test with the static runtime in its action inputs but the dynamic runtime's names in its `-l` flags. The policy therefore now lives in a single exported `cc_runtime_libs` helper, shared by all three sites that choose a runtime (`collect_inputs`, `_add_native_link_flags`, and rustdoc's doc-test link flags), so inputs and link flags stay consistent by construction. The patch applies with **zero fuzz and zero offset** (`git apply --check` and `patch -F0`), verified against the release tarball specifically, not just the git tag. ## Drops `bazel/rustix.patch` That patch deleted rustix's `can_compile()` probe artifact from `OUT_DIR` — which rules_rust captures as a cacheable `CargoBuildScriptRun` output — because the artifact was not byte-reproducible. 0.74.0 makes it unnecessary: [dfinity#4202](bazelbuild/rules_rust#4202) adds `--remap-path-prefix=${pwd}=.` to the `CARGO_ENCODED_RUSTFLAGS` that `cargo_build_script` passes to build scripts, and rustix's `can_compile()` forwards all of them to its probe `rustc`. ## Follow-ups (not in this PR) - The remaining dylib/cdylib half of the static-runtime patch has no upstream issue. Opening one would let it retire the way the doc-test half just did — though upstream deliberately links the dynamic runtime for shared libraries (its own `runtime_libs_test/shared_library` asserts exactly that), so it would need a toolchain-level opt-in rather than a straight behaviour change. - `.claude/CLAUDE.md`'s repin trigger could mention the `rules_rust` pin, since a bump on its own requires `./bin/bazel-pin.sh --force`. - If the rustix probe ever regresses (a toolchain bump, or anything adding a remap-scope rustflag), the fix is rules_rust 0.74.0's new `remove_nondeterministic_out_dir_files`: appending `rustix_test_can_compile` to `--@rules_rust//cargo/settings:out_dir_volatile_file_basenames` (re-listing its seven defaults, since the flag replaces rather than extends) empties `OUT_DIR` again with no crate patch and no repin. That, not a resurrected `bazel/rustix.patch`. - Worth an upstream issue: `cargo_build_script` could pass the same `${output_base}`/`${exec_root}` remaps that `rustc.bzl` already passes, closing the symlink gap described above. - The `dfinity/rules_rust` fork branches can be deleted once this merges; nothing will reference the fork any more. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
UebelAndre
pushed a commit
to UebelAndre/rules_rust
that referenced
this pull request
Sep 29, 2026
…d#4271) Legacy doctests run from a runfiles directory, but the launcher only strips output roots found through crate and C++ library providers. A generated rustc environment file built in an execution configuration retains a `bazel-out/...` path and fails before rustdoc starts. Collect the nonempty roots of every declared action input instead. This also covers generated compiler/linker tools and keeps source paths intact. The new regression executes a doctest using an environment file forwarded through an execution transition; it fails with a missing-file error on main and passes with this change. Validation on macOS with Bazel 9.2.0: 17 owning tests pass on this standalone branch. This extends the C++ runtime-root handling from bazelbuild#4220. Windows execution was not tested locally. Closes bazelbuild#4288.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
rust_doc_testfails to link on any cc_toolchain that supplies its C++/unwindruntime via
static_runtime_lib(i.e. enablesstatic_link_cpp_runtimes).This happens when cross compiling with the hermetic-LLVM e.g.:
rust_library/rust_binary/rust_testare fine. Only doc tests fail.Evidence
The first commit adds only a test to reproduce the issue by extending the
fixture from #3741 (which already declares a cc_toolchain with
static_runtime_lib = ":dummy.a"); the second commit adds the fix://test/unit/cc_toolchain_runtime_lib:runtime_libs_test/doc_testtest(rustdoc): reproduce ...fix(rustdoc): link doc tests ...The failing action's argv shows the search path arriving while the library
reference never does:
Cause
add_native_link_flagsemits the runtime libs'-Lnative=unconditionally butgates the matching
-lstatic=behindinclude_link_flags, which rustdoc setsto False (#2467). #4080 added a compensating
-Clink-arg=-lloop for doc tests,but this does not work for a
static_runtime_lib.Alternatives
Injecting the flag from outside doesn't work. The archive is already in the
doc test's runfiles (via #3741), so
-Clink-arg=<its short_path>does fix doctests. However,
toolchain.extra_rustc_flagsapplies to every rustc invocation,and normal
rust_testbinaries link in the execroot, where thatrunfiles-relative path doesn't exist:
clang++: error: no such file or directory. Doc tests and normal links need different paths to the same file,No single toolchain-level flag satisfies both. Per-target
rustdoc_flagsdoes work, but only by hardcoding the canonical repo name and the runtime lib's
internal layout in every
rust_doc_test. However, this is unmaintainable in any major project.Changes
rustdoc.bzl— extend the Fix rust_doc_test linking with native libraries #4080 loop to the cc_toolchain runtime libs,mirroring the crate-type split in
collect_inputs. Guarded oncc_toolchainbeing present (Add support for optional cc_toolchain #3665).
rustdoc_test.bzl— add those libs' root to--strip_substring. Required:they're built in a different configuration than the crate outputs, so
otherwise the
-Lnative=path stays an execroot path that doesn't exist inthe runfiles tree the doc test runs from. Only non-empty roots are added —
source-file runtime libs need no stripping, and an empty root would emit
--strip_substring=/, whichrustdoc_test_writerapplies as a plainstr::replace.The libs already reach runfiles via
ctx.runfiles(transitive_files = action.inputs)thanks to #3741.
Testing
bazel test -- //... -//test/unit/remap_path_prefix:integration_teston macOS:569 passed / 35 skipped, against 568 / 35 before. The new doc test is
the only target that changed status; every other action was a cache hit, so
this is a provable no-op where
static_runtime_libis empty.The patch has also been verified against a repo that builds with hermetic-LLVM and all failing doc tests
passed.