Skip to content

Remap the exec root out of build script rustflags - #4202

Merged
UebelAndre merged 2 commits into
bazelbuild:mainfrom
cpcwood:fix/build-script-remap-path-prefix
Aug 28, 2026
Merged

UebelAndre merged 2 commits into
bazelbuild:mainfrom
cpcwood:fix/build-script-remap-path-prefix

Conversation

@cpcwood

@cpcwood cpcwood commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Fixes issue: #4201

Build scripts that invoke rustc themselves bake the exec root into their output, because cargo_build_script does not include --remap-path-prefix in the CARGO_ENCODED_RUSTFLAGS it passes to them.

rustix's build.rs feature probe pipes source to rustc on stdin, so the working directory is the only path input rustc has, and it folds that into the --emit=metadata output written to $OUT_DIR. That is a declared build-script output, hashed into the cache key of every dependent crate, so the same source built at two different absolute paths produces different cache keys for the whole dependent chain.

construct_arguments() already applies --remap-path-prefix to every rustc invocation the ruleset constructs, "For determinism to help with build distribution and such". Build scripts reach rustc through CARGO_ENCODED_RUSTFLAGS rather than construct_arguments(), so they never inherited it. rustdoc is the only other exception and is documented as one.

Reproduction, with a Bazel-free minimal check and a four-workspace Bazel proof: https://github.com/cpcwood/bazel-rules-rust-rustix-cache-bust-demo

probe output
  unpatched  e27190bc...  01079d73...   differ
  patched    7e887e12...  7e887e12...   identical

Rustc actions re-executed at a second path
  unpatched  3   (rustix, //:mylib, //:myapp)
  patched    0

Verified on aarch64-darwin and ubuntu-latest. Hash values are platform-specific.

@google-cla

google-cla Bot commented Aug 4, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@cpcwood
cpcwood force-pushed the fix/build-script-remap-path-prefix branch 2 times, most recently from 0a713a3 to 666ce19 Compare August 4, 2026 09:19
Build scripts that invoke rustc themselves, such as rustix's feature probe,
receive CARGO_ENCODED_RUSTFLAGS and forward it verbatim. Without
--remap-path-prefix the exec root is folded into whatever they emit into
OUT_DIR, and because that directory is hashed into the cache key of every
dependent crate, identical sources built at two different absolute paths
produce different cache keys for the whole subtree.

construct_arguments already applies this remapping to every rustc invocation
rules_rust constructs directly. Build scripts reach rustc indirectly, through
the environment, so they did not inherit it.
@cpcwood
cpcwood force-pushed the fix/build-script-remap-path-prefix branch from 666ce19 to 5f3b621 Compare August 4, 2026 09:21

@UebelAndre UebelAndre 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.

Thanks! I had jut one question 😄

Comment thread cargo/private/cargo_build_script.bzl Resolved
@UebelAndre
UebelAndre self-requested a review August 19, 2026 15:41

@UebelAndre UebelAndre 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!

Comment thread cargo/private/cargo_build_script.bzl Resolved
@UebelAndre
UebelAndre enabled auto-merge August 25, 2026 20:23
@UebelAndre
UebelAndre added this pull request to the merge queue Aug 25, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 25, 2026
@UebelAndre
UebelAndre enabled auto-merge August 28, 2026 18:15
@UebelAndre
UebelAndre added this pull request to the merge queue Aug 28, 2026
Merged via the queue into bazelbuild:main with commit fcae471 Aug 28, 2026
3 checks passed
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]>
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