Skip to content

Add exec config trimming to stabilize tool config hashes across modes#3926

Open
rejuvenile wants to merge 1 commit intobazelbuild:mainfrom
rejuvenile:feat-exec-settings-trim
Open

Add exec config trimming to stabilize tool config hashes across modes#3926
rejuvenile wants to merge 1 commit intobazelbuild:mainfrom
rejuvenile:feat-exec-settings-trim

Conversation

@rejuvenile
Copy link

Summary

  • Adds a marker-based exec settings trim mechanism that resets target-specific Starlark settings in exec configuration
  • rust_proc_macro transitions unconditionally reset settings (proc macros are always exec tools)
  • Other rule transitions (rust_binary, rust_test, rust_static_library, rust_shared_library) propagate the reset when the marker is set
  • Consolidates all rule transitions into a shared rule_transition_impl() function

Motivation

Bazel's built-in exec transition resets native flags (like --compilation_mode) but does NOT reset Starlark build settings. This means settings like --@rules_rust//rust/settings:lto=thin leak into the exec configuration hash, causing proc macros and build scripts to rebuild unnecessarily when switching between e.g. fastbuild and opt modes.

rules_rust already strips these settings at action construction time (via is_exec_configuration() checks), so the actual rustc commands for tools are identical. But the different config hash prevents cache reuse.

Settings reset in exec configuration

  • Native flags: compilation_mode, stamp, strip, run_under
  • Starlark settings: lto, codegen_units, extra_rustc_flags, extra_rustc_flag, extra_rustc_env, incremental, pipelined_compilation, no_std, experimental_per_crate_rustc_flag

How it works

  1. rust_proc_macro's rule transition sets exec_settings_trimmed=True and resets all settings (via force=True)
  2. When any rule's transition sees the marker is True, it also resets settings, propagating transitively through the exec dependency subgraph
  3. The run_under flag is write-only (cannot be read in Starlark) so it's only included in proc macro outputs

Test plan

  • Build with --compilation_mode=opt then --compilation_mode=fastbuild and verify proc macros are not rebuilt
  • Verify --@rules_rust//rust/settings:lto=thin does not affect proc macro config hashes
  • Verify normal target builds are unaffected (non-exec targets preserve all settings)

Configuration transitions that strip build-mode-specific settings from
exec-config targets, improving cache hit rates across debug/asan/tsan/
release configurations.

When switching between build modes, proc macros and build scripts were
rebuilding because Starlark settings (lto, codegen_units, etc.) and
native flags (compilation_mode, stamp, strip) leaked into the exec
configuration hash. The actual rustc commands were identical, but
different hashes prevented cache reuse.

Key changes:
- Add exec_settings_trim.bzl with marker-based reset mechanism
- rust_proc_macro transitions unconditionally reset settings
- Other rule transitions propagate the reset when the marker is set
- Consolidate all rule transitions into shared rule_transition_impl()
- Handle write-only flags (run_under) and build script trimming
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