4.7.4 Transition Boundaries & Gotchas

extra

You add one transition to a wrapper rule, and soon bazel cquery shows a shared library in several configurations, analysis takes longer, and the same sources compile more than once. Transitions apply a configuration delta at the rule or dependency edge where they are attached. Changed values can then propagate through dependencies, but each edge's transition and each setting's scope determine what survives.1,2 Most production transition bugs are not failures of that mechanism. They come from the graph: which edges actually carry the new configuration, what was supposed to be data instead of code, and how Bazel names the resulting output directories. Once 4.7.2 Starlark Transitions and 4.7.3 Split Transitions are in place, the remaining work is keeping their footprint contained.

Audit Configuration Across Dependency Edges

A cc_test wrapper that turns on Address Sanitizer shows the everyday shape. The setting reaches the test's srcs, its deps, its data, and any genrule it depends on for code generation. A base library shared with a non-instrumented binary now exists in two configurations and builds twice.3 When transitions repeat along a dependency chain, the growth compounds. In the documented worst case, a single --//foo:owner flag, mutated by every pkg:i_b along a depth-n chain, produces (2^n) configured copies of one shared pkg:dep.4

The values travel that far because of how dependency configurations are formed. A transition attached to a rule changes that rule's configuration. One attached to a dependency attribute changes the configuration on that edge. Ordinary target-configured dependencies can carry those values farther, unless another transition or a setting's scope changes or resets them. Bazel forms each dependency's configuration from the parent's configuration, the dependency edge's transition, and the applicable setting scopes.1,2 The fix is to constrain which edges carry the changed values, not to weaken the transition.

think

Decide: A wrapper rule adds a transition to its broad deps attribute so users do not have to pass a board flag. The firmware output is now correct, but a shared logging library and a code generator underneath deps both show up as duplicate configured targets. Is the transition function wrong, or is the graph boundary wrong?

Reveal

The suspicious part is the boundary. The transition may set the right value, but attaching it to a broad edge can carry that value through ordinary target-configured dependencies below the wrapper. Bazel then has to analyze targets that did not conceptually need the board setting in another configuration. Check the code generator's exec edge separately: an exec transition does not guarantee that every custom setting resets.

The fix starts by naming the narrow edge: which dependency subtree actually needs the different configuration, and which deps should stay in the caller's configuration or no configuration at all? If the value is really part of a selected platform, 4.7.5 Platform-based Flags may remove the custom transition entirely. If it is a true rule-specific graph split, attach the transition where that split begins, not where it is merely convenient for the command line.

Concretely:

  • Audit every outgoing edge from the rule that applies the transition. Check both its transition and the scopes of the changed settings before deciding that an explicit reset is needed.
  • Separate genuinely configuration-independent data rules from generated or compiled dependencies. A rule grouping static fixtures can use config.none(). Raw source-file targets already have no configuration.5 Before you erase distinctions such as platform or build mode from a code generator's output, confirm that the output does not depend on them.
  • Tools normally use an exec transition rather than the parent's target configuration unchanged. Target-scoped settings reset there. Universal settings can propagate, and so can project-scoped settings for in-project exec dependencies. Add a reset only where the setting has genuinely become irrelevant.2

The with_cfg library is built around exactly this pain point. A naïve transition on a cc_test leaks to all dependencies — including data deps and code generators — and builds common deps twice. The library responds with two boundary tools: _builder.reset_on_attrs("srcs") (or any other attribute) prevents the transition from reaching that subtree, and a generated *_original_settings rule lets a single dependency opt out of the surrounding transition entirely — for example, a Java 7 target deliberately kept inside a Java 21 library.3 A hand-written transition() needs to enforce the same boundaries.

The same boundary discipline scales up. A cc_test macro can generate every (platform × sanitizer) combination from a single declaration, but the trade-off is direct: the analysis graph grows much larger, and compiling every combination in parallel consumes significantly more disk space.6 Multi-config matrices are powerful, but every fork is a real cost paid in analysis-graph nodes and on-disk outputs.

Apple multi-architecture rules provide a concrete diagnostic route. The rules_ios transition guide shows how nested 1:N transitions multiply configured targets and uses aquery to inspect the actions registered for each configuration.7 That is analysis evidence, not proof that every registered action executed: confirm execution with a profile or execution log, and account for cache hits before attributing wall time to the transition. The repository map routes from that guide to the split-selection implementation when the configured graph still looks wrong.

config.none() Strips Configuration From Data Edges

Raw source-file targets already have no configuration. Reaching a checked-in fixture through several configurations does not create a configured copy of that source-file target for each one.5

A rule target that groups those files, such as a filegroup, can still be analyzed in several configurations. If its inputs and behavior are genuinely configuration-independent, use config.none() on the dependency edge to remove all configuration and analyze that rule target only once.8

In rule code, attach it to the data attribute:

my_rule = rule(
    implementation = _impl,
    attrs = {
        "src": attr.label(allow_single_file = True),
        "data": attr.label_list(cfg = config.none(), allow_files = True),
    },
)

With cfg = config.none(), the configuration-independent data rule can share one unconfigured instance across parent configurations. This avoids repeated analysis of the grouping rule, not duplicate analysis of raw source files. Because config.none() removes the target configuration, reserve it for data that is genuinely independent of the target platform. If a data dependency is platform-specific, model that restriction on a configured edge or on the consuming target instead of using config.none() to erase the distinction. The same config module also exposes config.exec() for execution transitions and config.target() as an explicit no-op, but config.none() is the one that solves the data-fork problem.8

config.none() is not a license to "make this dep cheap." If the target produces compiled artifacts, removing its configuration silently drops the ability to vary by platform, compilation mode, or sanitizer flags. Reach for it when the dependency is genuinely data. Reach for an explicit reset (or a separate top-level entry point) when it is code that simply should not move with this transition.

Remove Only The Configuration That Became Irrelevant

Consider two boards, alpha and beta, that use the same C++ toolchain and share a generic logging library. Each board can build in dbg or opt. Before any containment, the library may appear in four configured variants: alpha/dbg, beta/dbg, alpha/opt, and beta/opt. The board does not change the library's code, so half of those variants are pure overhead: each is analyzed separately and compiled into its own output directory. With many boards and features, that overhead can decide whether the build finishes at all: in one large embedded build, analysis could not complete without board resets.9

Three controls act on that duplication at different points:9,10

ControlEffect on the shared libraryWhat stays distinct
Reset transitionExplicitly sets the board setting to a neutral value on the library edge. If everything else matches, alpha/dbg and beta/dbg can converge, as can the two opt variants.dbg and opt, plus any other output-relevant setting
Setting scopeDeclares where one build setting remains meaningful. A project-scoped board value resets outside its declared project, rather than on a hand-picked reset edge.Board distinctions inside the project and unrelated settings such as compilation mode
Path mappingNormalizes configuration-derived paths for eligible equivalent actions, allowing cache reuse across configured variants.All four configured targets and their analysis cost if no graph-side control has merged them

The reset is safe only after board-dependent choices have been made. One design separates hardware-abstraction-layer headers, consumed by the libraries, from implementation requirements carried upward to the application in a HardwareInfo provider. That separation lets generic libraries stop carrying board identity while the application still selects and links the right implementation.9 Sharing a compiler alone would not justify the reset.

For a board setting whose neutral value is none, the transition body can be this small:9

def _reset_board_impl(settings, attr):
    return {"//boards:board": "none"}

reset_board = transition(
    implementation = _reset_board_impl,
    inputs = [],
    outputs = ["//boards:board"],
)

Attach it to the generic-library attribute with attr.label_list(cfg = reset_board). This resets one declared setting, not the whole configuration. The platform still selects the toolchain and --compilation_mode still distinguishes debug from optimized compilation. Resetting before a downstream board-dependent select() is a correctness bug: the dependency now sees none rather than the board it needs. Bazel does not check this for you. It applies whatever value the transition returns, so an over-zealous reset can build the wrong thing.9

Setting scope expresses a different boundary. Hand-written resets get tedious when many edges leave the region where a setting matters, and you cannot add them to edges in code you do not own. A project-scoped setting resets itself instead: it opts in with scope = "project", and a PROJECT.scl file lists the directories where its value stays meaningful. Entering the project still needs a transition that sets the value, and leaving it restores the default.9 A value set on the command line behaves differently: it becomes the baseline and survives outside the project.2 3.2.7 Project Specifications & Flag Sets shows both files and distinguishes this propagation rule from flagset policy.

scope = "target" does not mean “only this target”: the value still travels through ordinary target-configured dependencies, but resets for exec configuration. Project scope follows declared project membership instead, including for in-project exec dependencies.2 Neither mechanism detects which settings every rule uses. Scopes suit libraries and rulesets whose boundaries you do not control. Because a scope is declared as a list of project directories, a few reset transitions can be simpler in a codebase whose boundaries you do control.9

To check which actions are eligible for path mapping and how to prove their cache reuse, continue to 6.2.2 How Remote Cache Keys Work.

Transition wrappers also leave a gap at the command line. Building the wrapped binary traverses its transition, but building an underlying library directly does not. That library then builds in a configuration nobody intends to support. A developer or coding agent who built it can draw wrong conclusions from the result, and the extra configuration wastes capacity.10 Express the supported build modes as project defaults, such as a flagset in PROJECT.scl, and keep transitions for configuration changes that belong to one part of the graph.

Output Directory Naming Does Not Prove Graph Deduplication

The historical version of this gotcha is well known: write a transition, then write another transition that effectively undoes it, and Bazel produces two identical configurations under different output directories — the same actions run twice, the cache does not match, and a "round-trip" path through transitions doubles the build. It surfaced in practice on macOS universal binaries, where the fix was --experimental_output_directory_naming_scheme=diff_against_baseline to make Bazel recognize identical configurations and build them only once.11

That advice addressed Bazel 6's legacy default. Current Bazel defaults to diff_against_dynamic_baseline, which names output directories using differences from the build's baseline, with a separate baseline for exec configurations.12 This is output naming, not proof that every apparent round trip produces one configured target.

What this means for someone writing transitions today:

  • Do not carry the old output-naming workaround forward. The modern default already uses dynamic-baseline naming, and the hidden experimental flag is not a general tuning control.
  • Use 5.2.4 bazel config — Configuration Inspection to investigate distinct configured copies: bazel cquery shows each configured target's hash, and bazel config <hash1> <hash2> compares the exposed flag values. Several paths to the same label and hash are normal graph sharing, not duplication to fix. Two different hashes with an empty diff point to a Bazel bug rather than a real difference: a transition followed by a reset produced exactly that in #29319, which has since been fixed upstream.9

If the project still builds with Bazel 6, treat the explicit flag as part of the migration toward a modern default rather than a permanent piece of configuration.

When The Boundary Argument Points To Platforms

Boundary control becomes harder as the number of orthogonal knobs grows. A transition that carries platform, sanitizer, and compilation mode is several boundaries layered on top of each other, and every wrapper rule has to maintain them. 4.7.5 Platform-based Flags is the alternative for the cases where the configuration is really "build for this device": move the build-setting values into the platform itself, select the platform, and let toolchain resolution carry the configuration without a custom transition per binary. It does not replace transitions for cross-cutting concerns like sanitizers or feature flags, but it does shrink the surface where boundary control has to be enforced by hand.

For the cases that remain transition-shaped, the 6.8.10 Optimizing the Measured Bottleneck optimization is a graph-side complement: trimming unused configuration fragments reduces how often a flag change actually invalidates a configured target, which makes a wider transition footprint less expensive when boundary control is incomplete. It does not remove the need to contain transitions, but it lowers the cost of getting the containment slightly wrong.

key takeaway

Most transition gotchas are graph problems: a changed value travels farther than it stays meaningful. Audit each dependency edge and the scope of each changed setting, then contain configuration where its meaning ends. A reset should clear only the setting that became irrelevant, such as the board after board-dependent choices are resolved, while preserving meaningful distinctions such as dbg versus opt. Setting scope bounds one setting's propagation without a hand-picked reset edge. Use config.none() only for genuinely configuration-independent data. A transition wrapper does not apply when someone builds the library directly, so express supported build modes as project defaults.

Two mechanisms can look like deduplication without merging the graph. Path mapping can reuse compatible actions but does not remove configured targets or their analysis cost. Output-directory naming is not graph sharing either. When in doubt, use bazel cquery plus bazel config to check whether apparent fan-out is distinct configurations or several paths to one shared configured target.

Check your understanding · 4 questions

1.A transition-enabled test wrapper is reconfiguring static fixtures and code generators that should not vary with the test's sanitizer flag. What is the first rule-author fix to consider?

Select one answer

2.Which of the following are appropriate uses of config.none() on a dependency attribute?

Select all that apply

3.True or false: --experimental_output_directory_naming_scheme in Bazel 6 vs Bazel 7+.

Choose True or False for each sentence

In Bazel 7 and 8, the default value is diff_against_dynamic_baseline.
On Bazel 7+, setting =diff_against_baseline is usually necessary to deduplicate identical configurations. The default already deduplicates.
The value legacy is the only one that disables the round-trip-transition deduplication.
One configured target reached by several paths with the same cquery hash is normal graph sharing, not a rule-side duplication to fix.

4.Match each transition-boundary tool to the situation it fits:

Drag each answer onto the matching prompt, or click an answer and then click a prompt

Answers
config.none() on a data attribute
with_cfg(...)._builder.reset_on_attrs("srcs")
bazel cquery //foo:bar plus bazel config <hashA> <hashB>
Platform-bound build settings (4.7.5)
0 of 4 answered

Footnotes

  1. Configurations — transition mechanism, inputs/outputs declaration, attaching to incoming vs outgoing edges, and the rule that the implementation must return exactly the declared outputs. ↩1 ↩2

  2. Bazel — core implementation, documentation, and regression corpus, flags_scoping_integration_tests.sh at 9.3.1 — project-scoped flags reset outside project directories, with separate in-project and out-of-project exec tests. The tests enable --experimental_enable_scl_dialect. ↩1 ↩2 ↩3 ↩4 ↩5

  3. with_cfg: Making transitions more accessible — the leak to data deps and code generators, common deps building twice, reset_on_attrs() for attribute-level boundaries, and the per-dependency reset rule. ↩1 ↩2

  4. Configurations — "Memory and performance considerations" and the depth-n worked example showing how a per-edge transition produces (2^n) configured copies of a shared dep. ↩

  5. Configurable Query (cquery) — input-file targets have a null configuration, and build_options(target) returns None for them. ↩1 ↩2

  6. Multi-platform & Sanitizer Builds With One Command — analysis-graph and disk-space cost of generating every (platform × config) combination of a target. ↩

  7. rules_ios repository map — public multi-architecture transition guide, aquery inspection boundary, and focused implementation escalation path. ↩

  8. config — config.none() removes all configuration for data-only deps. config.exec() and config.target() are the other built-in transition constructors. ↩1 ↩2

  9. Taming Configurations Explosion — board resets that kept analysis completing, unchecked resets, HAL headers separated from implementations, HardwareInfo, flag scopes and the team's decision to keep resets, the #29319 empty-diff report and upstream fix, and the distinction between reducing analysis and reusing actions. Production observations come from one embedded C/C++ codebase, not general performance guarantees. ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8

  10. One Build, Many Configurations — transitions miss directly built libraries, flagsets express project defaults, and path mapping retains separately analyzed configured targets. ↩1 ↩2

  11. Utilizing Bazel for Cross-Platform and Cross-Architecture Compilation and Testing — the round-trip transition duplication on macOS universal binaries and the historical --experimental_output_directory_naming_scheme=diff_against_baseline recommendation. ↩

  12. Bazel — core implementation, documentation, and regression corpus, CoreOptions.java at 9.3.1 — experimental_output_directory_naming_scheme defaults to diff_against_dynamic_baseline. The flag is undocumented and experimental, not a general tuning recommendation. ↩