Skip to content

Implement Bazel path mapping for cross-mode cache sharing#3927

Open
rejuvenile wants to merge 1 commit intobazelbuild:mainfrom
rejuvenile:feat-path-mapping
Open

Implement Bazel path mapping for cross-mode cache sharing#3927
rejuvenile wants to merge 1 commit intobazelbuild:mainfrom
rejuvenile:feat-path-mapping

Conversation

@rejuvenile
Copy link

Summary

  • Adds supports-path-mapping execution requirement to Rustc and CargoBuildScriptRun actions
  • Uses File objects in Args instead of path strings so Bazel's path mapper can rewrite output directory prefixes
  • Adds --set-env flag to process wrapper for path-mapped environment variables
  • Updates get_linker_and_args() to return the linker File object for path mapping
  • Updates rust_std_paths to use File depset with map_each callback

Motivation

Bazel's --experimental_output_paths=strip enables cross-configuration cache sharing by mapping output paths (e.g. bazel-out/k8-fastbuild/bin/... and bazel-out/k8-opt/bin/... both map to bazel-out/cfg/bin/...). This requires actions to:

  1. Declare supports-path-mapping in execution requirements
  2. Pass all file paths through Args objects (not literal strings) so Bazel can rewrite them

rules_rust was using .path strings in many places, preventing path mapping from working. This PR converts the key paths to use File objects in Args:

  • --out-dir uses map_each = _file_dirname callback
  • OUT_DIR is passed via --set-env (a new process wrapper flag) instead of the env dict
  • --codegen=linker= uses the File object when available
  • -L (stdlib paths) uses map_each = _file_dirname with uniquify = True
  • --output-file uses File objects

Build script environment variables containing file paths are passed through --extra_env command-line args (which Bazel path-maps) instead of the action env dict (which Bazel does not path-map).

Test plan

  • Build with and without --experimental_output_paths=strip and verify builds succeed
  • Build with --compilation_mode=fastbuild then --compilation_mode=opt and verify increased cache hit rate
  • Verify cargo build scripts receive correct OUT_DIR paths

Path mapping support for Rustc actions, enabling cross-configuration
cache sharing via --experimental_output_paths=strip.

Bazel's path mapper requires File objects in Args (not literal path
strings) and the supports-path-mapping execution requirement. This PR
converts key paths to use File objects:
- --out-dir uses map_each = _file_dirname callback
- OUT_DIR passed via --set-env (new process wrapper flag) instead of env dict
- Linker path uses File object when available
- -L stdlib paths use map_each = _file_dirname with uniquify = True
- build_script_env values passed through --extra_env args for path mapping

Also updates get_linker_and_args() to return the linker File object
and rust_std_paths to use File depset with map_each callback.
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