Skip to content

Disable path mapping for a test inheriting location-expanded rustc_env - #4467

Open
rohnnyjoy wants to merge 1 commit into
bazelbuild:mainfrom
rohnnyjoy:location-expansion-inherited-rustc-env
Open

rohnnyjoy wants to merge 1 commit into
bazelbuild:mainfrom
rohnnyjoy:location-expansion-inherited-rustc-env

Conversation

@rohnnyjoy

Copy link
Copy Markdown

#4117 turns path mapping off for a Rustc action when the target's rustc_flags or rustc_env carry a location expansion, since an expanded value names a configuration's output directory, which path mapping never rewrites. A rust_test built with crate = ... slips past that check. It inherits the crate's rustc_env, already expanded in the crate's own rule, and has no marker in its own attr.rustc_env to find. The test's Rustc action is then mapped: its inputs are staged under bazel-out/cfg/, while the inherited env value still names bazel-out/k8-fastbuild/bin/.... Any include!(env!(...)) or include_bytes!(env!(...)) of a generated file then fails under --experimental_output_paths=strip with "couldn't read bazel-out/k8-fastbuild/bin/...".

This change also treats the action as unmappable when any value in crate_info.rustc_env contains ctx.bin_dir.path. An env value is never path-mapped, so a value naming this configuration's output directory is as configuration-specific as an unexpanded macro.

Test: location_expansion_test.bzl gains mylibrary_env_test, a rust_test(crate = ":mylibrary_env") with no rustc_env of its own, and an analysis test, location_expansion_inherited_rustc_env_test, that checks the inherited MY_DATA value. As with mylibrary_env and mytest_env, the build-time guard is the target itself: mylibrary_env.rs does include_bytes!(env!("MY_DATA")), so under --experimental_output_paths=strip the new target fails to build without the rustc.bzl change and builds with it.

We found this turning on --experimental_output_paths=strip in a repository where about 15 crates pass a generated file's path through rustc_env and test themselves with rust_test(crate = ...). We carry this change as a patch on 0.74.0 until a release ships it.

AI assistance: this change was written by an AI agent; we reviewed it and ran the tests above, with and without the fix.

bazelbuild#4117 disables path mapping when a target's rustc_flags or rustc_env
carry a location expansion. A rust_test built with `crate = ...`
inherits that crate's rustc_env, already expanded in the crate's own
rule, so its own attr.rustc_env holds no marker and its Rustc action is
mapped: inputs are staged under bazel-out/cfg/ while the inherited env
value still names the configuration's bin directory, and an
include!(env!(...)) of a generated file fails to read it under
--experimental_output_paths=strip.

Treat the action as unmappable when any crate_info.rustc_env value
contains ctx.bin_dir.path. Add mylibrary_env_test, a rust_test of
mylibrary_env, which fails to build under strip without this change,
and an analysis test of its inherited MY_DATA.

Assisted-by: an AI coding agent

This branch has not been deployed

No deployments
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.

1 participant