4.12.1 Dynamic Actions with ctx.actions.map_directory()
extraOrdinary rule actions are planned during analysis: the rule implementation declares outputs, registers actions, and Bazel executes only the needed actions later. ctx.actions.map_directory() is an experimental Bazel 9+ exception at the edge of that model, so treat it as version-sensitive design vocabulary rather than a stable default. It lets a rule declare input and output tree artifacts up front, then ask Bazel to call a top-level callback after the input directories exist so the callback can register finer-grained actions from their directory listings.1 That makes it a specialized follow-up to 4.2.2 Actions, not a replacement for the static action graph model from 2.2.3 Static Action Graph.
input directory handle
output directory handle
top-level callback
lists files under the produced input tree
registers actions that write under outs
The callback registers actions. Bazel executes those actions and each output stays under the directory declared during analysis.
What Becomes Dynamic
map_directory() creates multiple actions based on the files inside one or more input directories, and those actions write into one or more output directories.1 The dynamic part is the per-file action set: the callback can look at the expanded tree artifact and decide which child files get corresponding actions.
The boundary is still tight. The rule must pass dictionaries of input_directories, output_directories, tools, optional additional_inputs, optional additional_params, and execution settings when it calls ctx.actions.map_directory().1 The callback can receive the expanded directory listing, but the output directories themselves are already declared File handles. It is not allowed to mint arbitrary outputs somewhere else in the package.
That distinction matters because a tree artifact is normally opaque to Starlark. ctx.actions.declare_directory() gives the rule a generated directory handle, but the contents are not directly accessible from normal Starlark analysis. They can only be expanded in execution-oriented APIs such as Args.add_all() or, for this article, through map_directory() after the directory has been produced.2
The Smallest Rule-Side Path
The call has two pieces: the analysis-time declaration and the execution-time template callback. The example below shows the complete Starlark shape inside the rule: a generator tool first fills the input tree artifact, then a worker processes each file in that directory. The two tools and the config target still need ordinary executable/file targets in the surrounding package.
def _rule_impl(ctx):
generated_srcs = ctx.actions.declare_directory(ctx.label.name + "_srcs")
generated_outs = ctx.actions.declare_directory(ctx.label.name + "_outs")
ctx.actions.run(
executable = ctx.executable._generator,
outputs = [generated_srcs],
arguments = [generated_srcs.path],
mnemonic = "GenerateSources",
)
ctx.actions.map_directory(
input_directories = {"srcs": generated_srcs},
output_directories = {"outs": generated_outs},
tools = {"worker": ctx.attr._worker[DefaultInfo].files_to_run},
additional_inputs = {"config": ctx.file._config},
additional_params = {"mode": "compile"},
implementation = _map_each_generated_file,
mnemonic = "MapGeneratedSources",
)
return [DefaultInfo(files = depset([generated_outs]))]
mapped_sources = rule(
implementation = _rule_impl,
attrs = {
"_config": attr.label(
default = "//tools:map_config",
allow_single_file = True,
),
"_generator": attr.label(
default = "//tools:source_generator",
executable = True,
cfg = "exec",
),
"_worker": attr.label(
default = "//tools:source_worker",
executable = True,
cfg = "exec",
),
},
)
The callback is a top-level function. The official API says nested functions and lambdas are not allowed for implementation.1 Its first argument is template_ctx, and the rest are keyword-only dictionaries that mirror the maps passed to map_directory(). 4.1.7 Starlark Callback Contexts & Types locates this experimental callback in Bazel's phase model and summarizes its parameter types, return, and action-registration effects.
def _map_each_generated_file(
template_ctx,
*,
input_directories,
output_directories,
tools,
additional_inputs,
additional_params):
srcs = input_directories["srcs"]
outs = output_directories["outs"]
for child in srcs.children:
# This compact example assumes the generator emits a flat directory.
out = template_ctx.declare_file(
child.basename + ".out",
directory = outs,
)
args = template_ctx.args()
args.add(child.path)
args.add(out.path)
args.add(additional_params["mode"])
template_ctx.run(
executable = tools["worker"],
inputs = [child, additional_inputs["config"]],
outputs = [out],
arguments = [args],
)
The flat-directory assumption keeps the first implementation small and makes
output naming unambiguous. A production callback that preserves subdirectories
must derive a relative path under srcs.directory and pass that path to
template_ctx.declare_file() within the output tree. It must not collapse two
equal basenames onto one output.3
Once //tools:source_generator, //tools:source_worker, and
//tools:map_config exist, the BUILD-facing invocation stays small:
load("//rules:mapped_sources.bzl", "mapped_sources")
mapped_sources(name = "generated")
With matching tool targets on Bazel 9 or newer, bazel build //demo:generated
first produces the input tree artifact and then expands the scoped per-file
actions. The listing is still schematic about its surrounding generator, worker,
config target, and production naming policy. The core tree-to-tree callback is
also runnable on Bazel 9.1.0:
bazel build //:mapped_files creates a generated input tree and one copied
child in the declared output tree. Its
map_directory.bzl
is deliberately smaller than this listing, so it proves the callback boundary
without pretending to exercise every optional map or execution setting.
input_directories["srcs"] is an ExpandedDirectory: it has a directory field for the original input directory and a children field for the files inside it.4 template_ctx is intentionally much smaller than ctx. It can build command lines with args(), declare files inside a declared directory with declare_file(), which accepts nested relative filenames, and register actions with run(). The current reference also lists declare_subdirectory(), but Bazel 9.1.0 does not provide it, so check the reference for the Bazel version you pin.3
The execution settings still live on the original map_directory() call. Its parameters include exec_group, toolchain, use_default_shell_env, env, execution_requirements, and mnemonic, so the generated actions should be routed the same way you would route ordinary ctx.actions.run() actions.1 If the callback invokes a tool from a non-default execution group, pair the tool attribute's cfg = config.exec("group") with the matching exec_group on map_directory(). 4.6.5 Execution Groups & Auto Exec Groups covers why those names should line up.
What The Callback Cannot Do
The callback sees the directory listing, not an open-ended analysis context. The API enforces three constraints: the callback is scoped to output directories handed to map_directory(), data must be passed through the map_directory() arguments, and the current API does not read generated file contents just because it can list generated files.5
That is why map_directory() is useful but not a general dynamic build graph escape hatch. If a generated file contains a JSON build graph, the current documented API does not let the callback read that JSON and synthesize a whole arbitrary graph from its contents. An experimental Bazel patch added a read() helper to explore that idea, but the reliable teaching point is the built-in boundary: directory listing in, scoped actions out.5
Where It Fits
The best fit is a generator or compiler pipeline where an earlier action produces a tree artifact and the next step should operate on each produced file separately. A typical example is one transpilation action per file in an opaque generated directory. Native C++ rules have historically had internal abilities around tree artifacts that ordinary Starlark rules could not express.5
This is also why dynamic-action APIs show up in language-rule conversations. The pain is visible from the other side: Bazel's ordinary static action graph means a rule cannot inspect generated module-dependency information during analysis, so rules_haskell exposes module graphs through explicit haskell_module targets and a Gazelle extension instead.6 Buck2's broader dynamic-dependency model can read a generated dependency graph and wire module actions from it, but map_directory() is narrower: it helps when filenames in a declared tree artifact determine scoped follow-up actions, not when arbitrary file contents must reshape target dependencies.
Mutation Testing: Two Actions Per Candidate
A test suite can pass while testing very little. Suppose add(a, b) returns a + b and its only test asserts that add(1, 4) is positive. Replace + with * and the test still passes. Line coverage reports the function as covered, so it cannot reveal the gap. Mutation testing makes such deliberate changes, called mutations, and reruns the tests. If a test fails, the mutation is killed. If all tests still pass, it survives: the tests are blind to that change.7
The obvious Bazel shape is one test action that loops over every mutation. That throws away what Bazel offers. The mutations run one after another in a single action, one changed input reruns all of them, one survivor turns the whole action red and hides the others, and every result lands in one log. One action per mutation avoids those problems, but the number of mutations is known only after an enumeration tool runs during Execution. Analysis cannot declare actions for them in advance. That is the gap map_directory() fills.7
Separate enumeration from per-candidate work:
- An ordinary action enumerates mutations into a declared input tree, with one
mNNNN/mutation.jsonfile per candidate. - The top-level callback selects children whose relative filenames end in
/mutation.json. It does not read their JSON. The filenames determine the fan-out, while the tools read the manifests later. - For each selected child, the callback registers a patch action. That action consumes the original source and the mutation manifest, then writes
mNNNN/mutated.miniinside the declared results tree. - A second action consumes that patched source and manifest, runs verification, and writes
mNNNN/test.xmlinto the same results tree. Listing the patched source in its inputs creates an ordinary action dependency on the patch action. The callback need not run the patch tool itself or wait for its output.7
The verification tool deliberately reverses the usual test-success condition: it succeeds when a test detects the mutation and fails when the mutation survives. The aspect returns the declared results tree through OutputGroupInfo(_validation = depset([results_dir])), so requesting validation outputs brings in the per-mutation work. Validations run by default, so applying the aspect is enough, without naming an output group:
bazel build --aspects=//:defs.bzl%mini_mutation_testing_aspect //:example_test
A survivor then fails an ordinary action and, with validations enabled, the build.7,8 4.2.9 OutputGroupInfo & Output Groups explains validation scheduling and its exceptions. 4.8.3 Validation Actions vs Aspects explains how an aspect can add checks while _validation supplies the enforcement path.
This gives Bazel separate patch and verification actions to schedule and cache rather than one monolithic mutation loop. Successful-action reuse still depends on each action's inputs and key. In this design, every patch action consumes the original source, so editing that source can invalidate many candidates. Dynamic fan-out does not promise cache reuse across arbitrary edits.7,8
Plan when to run it. The number of actions grows with every candidate, semantically equivalent mutations such as a + b to b + a survive without revealing a weak test, and the outputs take storage. Run mutation testing periodically, for example before a release, rather than on every build.7
These verification actions are not newly discovered Bazel test targets. Writing XML does not give them bazel test discovery, test-result reporting, or test sharding. Those require the test-rule contract in 4.2.6 Executable & Test Rules. The existing tree-copy probe demonstrates the same expansion boundary, but it does not exercise mutation semantics or validation enforcement.
Related Directory Expansion
DirectoryExpander is easy to confuse with map_directory(). It expands a declare_directory() tree artifact inside an Args.add_all() map_each callback, returning the files recursively under the directory.9 That helps construct a command line for an action that is already being registered.
map_directory() goes one step later in the pipeline: after the input directory exists, its callback can register new template_ctx.run() actions that write into declared output directories. Use DirectoryExpander when one action needs a command line over tree contents. Reach for map_directory() only when the directory listing should create several scoped actions.
ctx.actions.map_directory() bends Bazel's static action graph only at a very specific point: declared tree artifacts are still the boundary, declared output directories are still the destination, and the callback gets a small template context rather than the rule's full ctx.
For production rule design, start with ordinary analysis-time actions. Use map_directory() only when a generated directory's filenames genuinely decide the follow-up action set. Mutation testing illustrates that boundary: filenames select candidate pairs of patch and verification actions, declared artifacts connect those actions, and _validation requests their results without turning them into Bazel test targets.
Check your understanding · 3 questions
1.What is the dynamic part of ctx.actions.map_directory()?
Select one answer
2.Which statements describe the map_directory() callback boundary?
Select all that apply
3.Match each API object to its role in this pattern.
Drag each answer onto the matching prompt, or click an answer and then click a prompt
ExpandedDirectorytemplate_ctxDirectoryExpanderctx.actions.declare_directory()Footnotes
-
actions -
map_directory()creates actions from declared input directories to declared output directories. Parameter maps, callback signature, and top-level implementation requirement. ↩1 ↩2 ↩3 ↩4 ↩5 -
actions -
declare_directory()creates tree artifacts whose contents are not directly accessible from normal Starlark analysis. ↩ -
template_ctx — callback context methods and relative output filenames. The Bazel 9.1.0 reference lists
args(),declare_file(), andrun(), but not the unversioned reference'sdeclare_subdirectory(). ↩1 ↩2 -
ExpandedDirectory -
childrenanddirectoryfields passed to themap_directory()callback. ↩ -
Bazel Dynamic Actions - callback constraints, listing-versus-reading distinction, motivating examples, and caution around the patched
read()demo. ↩1 ↩2 ↩3 -
Haskell Builds at Scale: Comparing Bazel and Buck2 - Andreas Herrmann, Tweag by Modus Create - static action graph pressure,
haskell_moduleplus Gazelle workaround, and Buck2 dynamic-dependency comparison. ↩ -
Advanced Bazel: Rules Authoring, Performance Tuning and Remote Execution - A Stefano & Y Bonenberger — mutation-testing motivation, the rejected single-test-action design,
_validationinvocation, advice against continuous runs, and Exercise 4. The solution'sdefs.bzlat commit5b38c343shows filename filtering, patch/verification inputs and outputs, and the results tree in_validation. ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 -
Rules — validation outputs, ordinary action failures, caching of successful checks, and validation scheduling controls. ↩1 ↩2
-
DirectoryExpander - expands
declare_directory()tree artifacts forArgs.add_all()map_eachcallbacks. ↩