4.6.4 Execution Configuration for Tools (cfg = "exec")

A rule that drives a build will eventually depend on something its actions need to run: a compiler, a code generator, a stamping tool, a protoc plugin. The exec transition is how a rule says "this dep is a build-time tool, not a library that ships with the final artifact." Setting cfg = "exec" on a dependency attribute switches that edge from the target configuration to an execution configuration so the tool is built for the machine that runs Bazel actions, not for the platform the final binary is destined for.1

Two Configurations Show Up In One Rule

Bazel keeps the target/execution split from 4.6.1 Platform Model for Rule Authors in scope around every rule.2 The target platform describes what the final artifact is built for. The execution platform describes where actions actually run. They are often the same machine, but they don't have to be — bazel build on a Mac driving remote execution on Linux is a routine setup.3

The mismatch matters as soon as a rule registers an action that invokes a tool. The user asked to build "this Android binary" (target = Android arm64), but the code generator runs as a normal x86-64 Linux process on the build host. Building the code generator for Android would just produce something that can't run on the executor. Building it for the executor is what you actually want. The exec transition is Bazel's standard answer to that mismatch.4

Marking An Attribute As A Tool

The exec transition attaches to a dependency attribute. The canonical shape — straight from the rules reference — is a private label attribute that points at a tool and is marked both executable and exec-configured:5

example_library = rule(
    implementation = _example_library_impl,
    attrs = {
        "srcs": attr.label_list(allow_files = [".proto"]),
        "_compiler": attr.label(
            default = "//tools:example_compiler",
            allow_single_file = True,
            executable = True,
            cfg = "exec",
        ),
    },
)

Inside the implementation, ctx.executable._compiler resolves to the compiler built for the execution platform, ready to hand to ctx.actions.run(executable = ...). The same shape is the implicit-dependency pattern the rule guide recommends for build-time helpers, and the same cfg = "exec" clause appears throughout production rule code — it's the production-action contract spelled out in 4.4.3 Action Execution Contract, viewed from the configuration side.

If an attribute has executable = True, the rules docs require cfg to be set explicitly.6 That rule exists precisely to guard against accidentally building a tool for the wrong configuration: an executable that ends up in the target configuration when it should run as a build tool is one of the easiest mistakes to make, and Bazel forces the rule author to pick a side.

The rules_glyph mini ruleset uses the same attribute shape inside its toolchain implementation: compiler = attr.label(executable = True, cfg = "exec") and worker = attr.label(executable = True, cfg = "exec") wrap tools that its consuming rules will run during actions. Glyph uses toolchains because the compiler is a reusable tool family, not one private helper. The cfg decision is the same, but the public boundary moves to 4.6.2 Defining, Registering & Accessing Toolchains.

What The Transition Actually Does

When Bazel walks into a cfg = "exec" edge, it flips that subtree from the target configuration into an exec configuration: the tool dependency, its libraries, and its own tool deps all get configured for the execution platform.7 Bazel selects an execution platform for the consuming rule's actions, and the exec-configured tool is built for that same side. When the rule also declares toolchains, toolchain resolution participates in choosing that execution platform. For a fixed private helper, the important invariant is simpler: the tool binary and the action that invokes it agree on where they run.8

You can see the split in bazel aquery. In a small Go and Typst project built on an ARM Mac, the action of a custom gallery rule reports Configuration: darwin_arm64-fastbuild and writes its HTML page under bazel-out/darwin_arm64-fastbuild/. Its command line, though, starts with the tool from bazel-out/darwin_arm64-opt-exec/bin/tools/gallery/..., the separate output tree of the exec configuration that the rule's cfg = "exec" _tool attribute selected.9 The payoff shows up when the target changes. Building the same server with --platforms=@rules_go//go/toolchain:linux_amd64 produced a statically linked x86-64 Linux binary and reported 27 action cache hits. The gallery tool and the Typst compiler kept running on the Mac, and only the server code had to be compiled for Linux. Without cfg = "exec", Bazel would have built the gallery tool for Linux and then tried to run it on the Mac.9

extra

Why the target-side outputs can hit too. The gallery page and the Typst images are target-configuration outputs, so cfg = "exec" alone does not explain cache hits on them. An action's output paths are part of its cache key, so such an output can be an action cache hit after a --platforms switch only if its output directory keeps its name. The output-dir-path-mapping snippet prints where one target lands under several configuration changes. On Bazel 9.1.0 on an x86_64 Linux host:10

//:mapped                              bazel-out/k8-fastbuild/bin/mapped.txt
//:mapped --platforms=//:alpha_board   bazel-out/k8-fastbuild/bin/mapped.txt
//:mapped --platforms=//:arm64_linux   bazel-out/k8-fastbuild/bin/mapped.txt
//:mapped under //:on_alpha            bazel-out/k8-fastbuild-ST-8636886c103f/bin/mapped.txt
//:mapped under //:on_beta             bazel-out/k8-fastbuild-ST-96a4945d8bfb/bin/mapped.txt
//:tool as an exec tool                bazel-out/k8-opt-exec/bin/tool.sh

The rows follow three rules:

  • --platforms alone keeps the name in 9.1.0, even for //:arm64_linux, whose CPU constraint is aarch64. The k8 part is not taken from the target platform. The command-line reference describes --experimental_platform_in_output_dir as the switch that puts a platform shortname in place of the CPU, and at its 9.1.0 default the snippet still gets the CPU name.10,11 An action that does not otherwise depend on the platform therefore keeps its key: rebuilding the snippet's //:plain with --platforms=//:alpha_board executed no actions.10
  • A transition that sets a flag adds a suffix. //:on_alpha and //:on_beta set //:board to different values, and each value gets its own -ST-<hash> directory, so the same copy action gets two keys.10,11
  • Exec-configured tools get their own -opt-exec tree, the Linux counterpart of darwin_arm64-opt-exec above.10

The first rule depends on the Bazel version. Bazel 10.0.0rc1 makes --experimental_platform_in_output_dir a no-op, and under the 10.0.0rc4 release candidate the same snippet writes to bazel-out/alpha_board-fastbuild/ and reruns every action after the switch.12 Count on cfg = "exec" to keep tools cached across target platforms. Do not count on target-configuration outputs surviving a --platforms switch, because that is a property of one Bazel version's naming. When eligible actions must share results across configurations, use path mapping from 6.2.2 How Remote Cache Keys Work.

If rule implementation or test code needs to distinguish this state explicitly, ctx.configuration.is_tool_configuration() reports whether the current target is being analyzed in the tool/exec configuration.13 Treat it as an inspection hook, not a reason to hide tool-vs-target behavior that should be expressed through attributes, toolchains, or exec groups.

This is the simple fixed-helper path. You add cfg = "exec" to one tool attribute, ctx.actions.run(executable = ...), and Bazel decides on one execution platform for the rule and routes both the tool build and the action to it.14 This is the simplest place where the target-vs-execution split becomes concrete — there is no per-action wiring, no custom transition function, no exec_group argument anywhere.

For a production-shaped boundary case, rules_py keeps a focused regression workspace where a PEP 517 frontend must run as an execution tool while a native wheel's C/C++ toolchain follows the transitioned target platform.15 It is executable evidence for keeping "tool that runs now" separate from "artifact being produced". The case exercises private ruleset machinery, so downstream projects should learn from the platform split rather than load its private symbols.

cfg = "exec" means "build for the executor's platform", so for a tool built from source it is the right choice only when the built program runs natively there. rules_wine runs Windows .exe tools under Wine on Linux and macOS executors and documents the boundary. A prebuilt .exe on a cfg = "exec" attribute works, because nothing is compiled and the runner selected for the executor launches it. Setting cfg = "exec" on a source-built cc_binary instead builds a Linux executable on a Linux executor, not the Windows program Wine needs. The rule that owns such a payload must transition it to a Windows platform whose CPU matches the executor.16 When a tool's binary must target a different platform than the executor that launches it, as a Windows program under Wine does, that payload needs a custom transition (4.7.2 Starlark Transitions), not the exec transition.

The Three Values Of cfg On A Dependency

A dependency attribute generally takes one of three cfg shapes:17

cfg valueMeaning
"exec"Tool that runs during the build. Bazel transitions the dep into an exec configuration.
"target"Stays in the target configuration. Library, runtime data, test fixture — anything that ships or runs alongside the final artifact.
omittedSame as inheriting the parent's configuration. Allowed only when executable = False.

Setting cfg = "target" does not actually change anything: it is purely a convenience value that lets rule designers make their intentions explicit — use it when it improves readability, not because Bazel demands it.18 The asymmetry is real: omitting cfg is fine for plain sources and libraries. Flipping to exec is a deliberate decision tied to "this thing must execute on the build machine."

The cfg slot also accepts a Starlark transition object, which is the entry point to custom configuration flips covered in 4.7.2 Starlark Transitions.19 The exec transition is not a custom transition — it is one of Bazel's standard built-in transitions, and that is why it is the smallest tool-routing transition to understand before reaching for custom configuration changes.

A Historical Note: cfg = "host"

Reading old rule code, you'll run into cfg = "host" on tool attributes. Bazel used to model only the host platform (where Bazel itself runs) and conflate it with where actions execute, which is wrong whenever remote execution puts actions on a different platform than the CLI and can make those actions fail.20 The 6.0 release retired the cfg = "host" spelling in favor of the explicit execution-platform model. Rule code written for current Bazel should use cfg = "exec". cfg = "host" appears only in legacy rulesets and old documentation.21

Where The Exec Transition Sits

The exec transition is the smallest building block in a stack of related mechanisms:

  • Implicit tool deps with cfg = "exec" route one tool through the rule's default execution platform. This article.
  • Toolchain resolution (4.6.3 Toolchain Resolution) generalizes the same idea: instead of hardcoding a _compiler label, the rule asks for a toolchain type and Bazel picks a registered toolchain that fits the target + execution platform pair. Inside toolchain rules, cfg = "exec" on a tool attribute means "the execution platform of the rule that consumes the toolchain," not the toolchain rule itself.22
  • Execution groups (4.6.5 Execution Groups & Auto Exec Groups) extend the model when a single rule needs different execution platforms for different actions — one tool on macOS, another on remote Linux. Each exec group has its own toolchain dependencies and exec_compatible_with constraints. Tool attributes and actions opt into that group with cfg = config.exec(exec_group = "...") and ctx.actions.run(exec_group = "...").23
  • Custom Starlark transitions (4.7.2 Starlark Transitions) are the escape hatch for everything else — flipping --compilation_mode, instrumenting a subtree, switching CPU per artifact. They make the build graph larger and harder to reason about. Reach for them only when exec / toolchain / exec-group can't express what you need.24

Most production rules will only ever need the first two. The exec transition is the answer to "my rule needs to invoke a tool during the build," and most of the time, that is the whole question.

key takeaway

cfg = "exec" on a dependency attribute marks that dep as a build-time tool: Bazel builds it for the execution platform (where actions run) rather than the target platform (where the final artifact runs).

It is the smallest, default tool-routing transition — not a custom Starlark transition. Set executable = True and cfg = "exec" on private _tool attrs, and pass ctx.executable._tool to ctx.actions.run to invoke them.

Check your understanding · 4 questions

1.A Starlark rule has a private attribute pointing at a code generator that its actions invoke. What does adding cfg = "exec" to that attribute do?

Select one answer

2.Why does Bazel require cfg to be set explicitly whenever a dependency attribute has executable = True?

Select one answer

3.True or false — common claims about cfg = "exec" and the exec transition.

Choose True or False for each sentence

cfg = "target" and omitting cfg on a non-executable attribute produce the same build behavior. The explicit form is a readability hint.
cfg = "exec" is a user-defined Starlark transition that must be allowlisted with _allowlist_function_transition before a rule can use it.
cfg = "host" is the modern spelling and should be preferred over cfg = "exec" on current Bazel.
When a cfg = "exec" edge is taken, the tool's own transitive dependencies are also built for the execution platform, not the target platform.

4.Match each scenario to the cfg value a well-written rule should put on the dependency attribute.

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

Answers
A code generator the rule invokes via ctx.actions.run during the build.
A shared library that the final binary links against and ships with.
A test-helper binary that runs alongside the test on the target platform at test time.
0 of 4 answered

Footnotes

  1. Rules — when a dep is a build tool, the attribute should transition into an exec configuration so the tool builds for the execution platform. ↩

  2. Toolchains — target platform vs execution platform as the two platforms Bazel reasons about per rule. ↩

  3. Action configuration in Bazel — host platform vs execution platform distinction, motivated by remote execution moving actions off the host. ↩

  4. Writing Bazel rules: platforms and toolchains — host vs execution vs target platforms and what each one means for the build. ↩

  5. Rules — private executable tool attributes use default, executable = True, and cfg = "exec". ↩

  6. Rules — cfg must be set explicitly when executable = True, to guard against tools accidentally built for the wrong configuration. ↩

  7. Rules — cfg = "exec" on a dep attribute causes the tool and all of its own dependencies to be built for the execution platform. ↩

  8. Writing Bazel rules: moving logic to execution — _builder implicit dep with cfg = "exec" is built for the execution platform even when cross-compiling for a different target platform. ↩

  9. Build like a Google engineer: modern Bazel with modules, Go and Typst — bazel aquery 'mnemonic(Gallery, //server:index)' output with the tool under darwin_arm64-opt-exec and outputs under darwin_arm64-fastbuild, and the --platforms=@rules_go//go/toolchain:linux_amd64 cross-build with its action cache hits. ↩1 ↩2

  10. output-dir-path-mapping snippet — verify_output_paths.py prints and asserts the output paths (//:arm64_linux uses @platforms//cpu:aarch64), and the zero-action rebuild of //:plain after adding --platforms=//:alpha_board, on Bazel 9.1.0 on x86_64 Linux. ↩1 ↩2 ↩3 ↩4 ↩5

  11. Command line reference — --experimental_platform_in_output_dir (default Auto): "If true, a shortname for the target platform is used in the output directory name instead of the CPU." Taming Configurations Explosion — each transitioned configuration gets its own -ST- hashed output tree in which the same library is built again. ↩1 ↩2

  12. Bazel 10.0.0rc1 release notes — --experimental_platform_in_output_dir listed among the flags that are now no-ops. Bazel 10.0.0rc4 release notes — platforms whose names contain / no longer fail analysis when used in output directory names. The alpha_board-fastbuild path and the rerun come from the snippet's cquery and build commands run by hand with USE_BAZEL_VERSION=10.0.0rc4. ↩

  13. configuration — is_tool_configuration() returns whether the current target is being analyzed in the tool configuration. ↩

  14. Action configuration in Bazel — simplest rule shape: a tool attribute with cfg = "exec", ctx.actions.run(executable = ...), and Bazel configures the tool to run on the platform selected for the action that invokes it. ↩

  15. rules_py repository map — pep517-frontend-exec-group verifies that the build frontend executes on the appropriate execution platform while native wheel compilation selects the transitioned target toolchain. ↩

  16. rules_wine — Running Windows build tools (windows_actions guide) — Runner and payload configuration: a prebuilt .exe on a cfg = "exec" attribute, and the warning that cfg = "exec" on a cc_binary builds a Linux executable on a Linux executor, so source-built payloads need a Windows compilation transition matching the executor CPU. ↩

  17. Rules — cfg selects between same-configuration deps and exec-configured tools on each dependency attribute. ↩

  18. Rules — cfg = "target" is a readability hint with no behavior change. Set it only when it clarifies intent. ↩

  19. Rules — cfg also accepts a user-defined transition object for custom configuration changes. ↩

  20. Action configuration in Bazel — Bazel previously assumed the execution platform equaled the host platform, which is wrong when actions execute remotely on another platform and can lead to action failures. ↩

  21. Rules — Bazel versions before 6.0 used a distinct "host" configuration. Current Bazel uses the explicit execution-platform model. ↩

  22. Action configuration in Bazel — inside a toolchain rule, cfg = "exec" means the execution platform of the rule consuming the toolchain, not the toolchain rule itself. ↩

  23. Action configuration in Bazel — exec groups attach distinct constraints to different actions within one rule and pair with cfg = config.exec(exec_group = ...) tool attributes. ↩

  24. Rules — cfg = my_transition invokes a user-defined transition, trading flexibility for graph size and analyzability. ↩