feat: per-crate rustc flag configuration trimming#3923
Open
rejuvenile wants to merge 1 commit intobazelbuild:mainfrom
Open
feat: per-crate rustc flag configuration trimming#3923rejuvenile wants to merge 1 commit intobazelbuild:mainfrom
rejuvenile wants to merge 1 commit intobazelbuild:mainfrom
Conversation
Add a `skip_per_crate_rustc_flags` attribute and transition to clear the `experimental_per_crate_rustc_flag` setting for targets that would never match any per-crate flag filter. When `experimental_per_crate_rustc_flag` is set (e.g., to pass flags to first-party crates matching `//` prefix), all transitive third-party crates are placed into a different configuration even though the flags don't apply to them. This causes unnecessary rebuilds when the flag value changes. This change: - Adds `per_crate_flag_trim.bzl` with a transition that clears the setting when `skip_per_crate_rustc_flags = True` - Adds the `skip_per_crate_rustc_flags` attribute to `_common_attrs` - Applies the transition to `rust_library` and `rust_proc_macro` rules - Extends existing transitions (binary, test, static_library, shared_library) to also trim per-crate flags - Updates crate_universe's rendering to set the attribute on generated third-party crate targets This significantly improves cache hit rates for projects that use per-crate flags for first-party code while depending on many third-party crates.
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.
Summary
Add a
skip_per_crate_rustc_flagsattribute and transition to clear theexperimental_per_crate_rustc_flagsetting for targets that would never match any per-crate flag filter, improving cache hit rates for third-party crates.Problem
When
experimental_per_crate_rustc_flagis set (e.g., to pass-Copt-level=2to first-party crates matching//.*@), all transitive third-party crates are placed into a different Bazel configuration even though the flag filter never matches them. In a typical project with hundreds of third-party crates, this means:Solution
Add a configuration trimming mechanism:
New attribute:
skip_per_crate_rustc_flags(bool, defaultFalse) on all Rust rule types (rust_library,rust_binary,rust_test,rust_proc_macro,rust_static_library,rust_shared_library)New transition:
per_crate_flag_trim_transitioninrust/private/per_crate_flag_trim.bzl— whenskip_per_crate_rustc_flags = True, clears theexperimental_per_crate_rustc_flagsetting, returning the target to a canonical configurationIntegration: The transition is applied via
cfgonrust_libraryandrust_proc_macro, and integrated into existing platform transitions for binary/test/static/shared library rulescrate_universe: The renderer sets
skip_per_crate_rustc_flags: trueon all generated third-party crate targetsHow it works
When
skip_per_crate_rustc_flags = False(the default for first-party crates), the transition is a no-op and preserves the current behavior.Changes
rust/private/per_crate_flag_trim.bzl(new): Transition implementationrust/private/rust.bzl: Add attribute, wire transitionscrate_universe/src/rendering.rs: Set attribute on generated targetscrate_universe/src/utils/starlark.rs: Add field toCommonAttrsstructCompatibility
False, so existing targets are unaffected--experimental_per_crate_rustc_flagusage: The trim only affects the per-crate flag setting, not other configuration values