4.4.6 Advanced Action Parameters

extra

Most custom rules never need the advanced action parameters. Once a rule already declares clean inputs, tools, outputs, command-line data, and environment, unused_inputs_list and resource_set let you refine how Bazel treats that action at production scale: one tunes cache invalidation, the other tunes local scheduling.1 They are not substitutes for the basic action contract from 4.4.3 Action Execution Contract or the scalable command-line patterns from 4.2.3 Args & Command Lines.

unused_inputs_list: Tell Bazel What Did Not Matter

An action's declared inputs are normally part of the reuse decision: if an input changes, Bazel has to assume the output may change too. That is the right default for correctness and connects directly to the cache-key model from 2.4 Caching & Incrementality. unused_inputs_list is the escape hatch for tools that must receive a broad set of files at execution time, but can report afterward which of those files did not affect the outputs.2

The parameter points at a File containing the action's unused inputs. The official API describes it as generally one of the action's outputs, and says changes in listed files must not affect the action outputs in any way.2

This is pruning after execution, not input discovery before it. The action always starts with every declared input, and the tool reports afterward. Bazel then drops the reported files from the action's effective inputs, so an edit to only those files no longer reruns the action. When a still-relevant input changes and the action must run again, Bazel first restores the complete declared input set, because a file that went unused last time may be needed this time.3 The list file itself is an ordinary output: it belongs in outputs, and the tool must create it.3

def _compile_impl(ctx):
    out = ctx.actions.declare_file(ctx.label.name + ".o")
    unused = ctx.actions.declare_file(ctx.label.name + ".unused_inputs")

    args = ctx.actions.args()
    args.add("--out", out)
    args.add("--unused-inputs", unused)
    args.add_all(ctx.files.srcs)
    args.add_all(ctx.files.headers)

    ctx.actions.run(
        executable = ctx.executable._compiler,
        inputs = ctx.files.srcs + ctx.files.headers,
        outputs = [out, unused],
        arguments = [args],
        unused_inputs_list = unused,
        mnemonic = "CustomCompile",
    )

    return [DefaultInfo(files = depset([out]))]

Bazel reads the file line by line and matches each non-empty line against the action's input exec paths. Bazel 9.x trims surrounding whitespace from each line first. Bazel 10 matches the line exactly, because file names may begin or end with whitespace.4,5 A line that matches no input is silently ignored, and the input it meant to name stays relevant. So a tool that pads or indents its report loses the pruning without any error on Bazel 10. Emit bare exec paths, one per line. The least error-prone way to get them right is to have the tool echo back exactly the path strings that Bazel passed on its command line, instead of reconstructing paths from names.3

This only works when the tool can tell the truth. If a header, interface file, generated metadata file, or config file appears in the unused-inputs list, the rule is promising that changing that file cannot change the declared outputs for this execution. A false unused-input report is a cache correctness bug, not a performance tweak.

The useful pattern is "available to the tool, irrelevant to the cache key." Recompilation avoidance in rules_haskell makes the complete path concrete:

  1. Start conservatively. A module compile action declares the full interface files of its dependencies. GHC may need those files, so omitting them would make the action contract incomplete.
  2. Expose a smaller change signal. The ruleset extracts the ABI hash nested in each interface file into a separate .abi file and also declares that file as an input. Many interface-file changes leave this downstream-facing ABI signal unchanged.6
  3. Keep the rich input available. An ABI hash cannot replace an interface file. When an ABI change causes recompilation, GHC still needs the complete interface, so the .hi file remains in the action's declared inputs.7
  4. Let the smaller signal control invalidation. The action's unused-inputs report lists the interface files. Bazel can then reuse the result when only those files change, while a changed .abi input invalidates the action. On that execution the full interfaces are already available.6,7

This separates two questions that an ordinary input list answers together: what might the tool need if it runs? and which change proves that it must run again? The conservative input set answers the first. The ABI files plus unused_inputs_list answer the second.

For the executable trail, the rules_haskell map connects the implementation in haskell/private/actions/compile.bzl to the focused rules_haskell_tests/tests/recompilation/ regression workspace.8 These are ruleset-specific evidence for the general unused_inputs_list contract, not a public API for downstream Haskell builds.

The same design can fit a compiler whose dependency artifact contains more data than its public interface signal, an analyzer that scans a conservative file set but emits the files that actually informed its report, or a generator that receives a catalog and can prove which entries contributed to its outputs. The smaller signal need not always be an ABI hash: it can be any deterministic, declared artifact that captures every change capable of affecting the output.

The safety boundary is exact: after accounting for the inputs that remain relevant, changing a file named in unused_inputs_list must not be able to change any declared output. The tool must derive that fact from the execution, not from a rule-author guess. Do not apply the pattern when an omitted detail can affect code generation, diagnostics, ordering, configuration, generated metadata, or any other declared output. When the tool reads hidden inputs, or when nondeterminism prevents it from reporting relevance reliably. In those cases, keep the input relevant and accept the invalidation until you can produce a complete smaller signal.

This pruning happens in the Bazel client and gives no shared remote cache hits. Backend-side pruning is the contrasting design: a service between Bazel and the remote executor records which inputs each action read and stores a second, pruned action-cache entry that every client can hit, which is correct only while those recorded accesses still predict the current run (6.2.2 How Remote Cache Keys Work).9

For the Haskell-specific combination of module-grained actions, ABI-based recompilation avoidance, and persistent GHC workers, continue with L7.1 rules_haskell. The reusable rule-authoring idea here does not depend on those Haskell mechanics.

resource_set: Size Local Action Scheduling

resource_set is a callback for estimating CPU and memory use when the action runs locally. Bazel calls the function with two positional arguments: an OS name string such as "osx" and the number of action inputs. The function returns a dictionary whose supported keys are "cpu", "memory" in MB, and "local_test". If the parameter is None, Bazel uses defaults of 1 CPU, 250 MB memory, and 1 local test.10

def _compiler_resources(os_name, input_count):
    memory_mb = 512 + input_count * 2
    if os_name == "osx":
        memory_mb += 256
    return {
        "cpu": 1,
        "memory": memory_mb,
    }

def _compile_impl(ctx):
    # ... declare outputs, inputs, args ...
    ctx.actions.run(
        executable = ctx.executable._compiler,
        inputs = inputs,
        outputs = [out],
        arguments = [args],
        mnemonic = "CustomCompile",
        resource_set = _compiler_resources,
    )

The callback must be a top-level function. Lambdas and nested functions are not allowed.10 Treat that as part of the same discipline used for Args.map_each: action-time callbacks should not capture arbitrary analysis-phase state.

Use resource_set when the default local estimate is materially wrong. A code generator that needs several GB of memory should not be scheduled as if it needed 250 MB. A tiny formatter over one file should not force the local scheduler to reserve more capacity than it needs. The right estimate helps Bazel avoid local oversubscription and improves utilization, but it is still an estimate, not a hard memory limit. Measure whether that estimate matters with the profiling workflows in 5.4 Performance.

This parameter is scoped to local execution. The official API says it estimates resource usage "if this action is run locally."10 6.4 Remote Execution Infrastructure explains the separate remote-execution environment and scheduling model. Do not assume resource_set will provision a larger remote worker.

The callback is not the last word on the local estimate. Starting with Bazel 9.2, Bazel applies resources:cpu and resources:memory entries from the action's exec_properties as overrides on top of whatever resource_set returned.11,12 The Bazel 10 release notes list the same behavior as an incompatible change.13 An exec_properties value written for a remote executor can therefore also change local scheduling. When a local estimate looks wrong, check both the rule's callback and the target's or platform's exec_properties.

key takeaway

Reach for these parameters only after the ordinary action contract is already solid. unused_inputs_list is for tools that can accurately report cache-irrelevant declared inputs after execution. resource_set is for actions whose local CPU or memory needs are large enough that Bazel's default estimate causes bad scheduling.

Both parameters are promises to Bazel. If the promise is wrong, the result is either incorrect cache reuse or misleading local scheduling.

Check your understanding · 3 questions

1.Which conditions make unused_inputs_list appropriate for a custom action?

Select all that apply

2.What does resource_set influence?

Select one answer

3.True or false: advanced action parameters are promises to Bazel.

Choose True or False for each sentence

A false unused-input report can create incorrect cache reuse.
resource_set is a hard memory limit enforced on the process.
The resource_set callback must be a top-level function.
unused_inputs_list removes listed files from the action sandbox.
0 of 3 answered

Footnotes

  1. actions — ctx.actions.run() parameters include unused_inputs_list and resource_set. ↩

  2. actions — unused_inputs_list is a file listing inputs unused by the action. Listed inputs must not affect outputs. ↩1 ↩2

  3. Pruning Unused Action Inputs in Bazel — post-execution pruning versus input discovery, restoration of the full input set before re-execution, the list file as a declared output the tool must create, and the tool echoing back the exact candidate paths it received. ↩1 ↩2 ↩3

  4. Bazel 10.0.0rc1 release notes — Bazel no longer trims whitespace from unused_inputs_list lines before matching them against an action's inputs, as file names may begin or end with whitespace. ↩

  5. Bazel — core implementation, documentation, and regression corpus — Bazel reads the list line by line, skips empty lines, and removes matching inputs by exec path. 9.3.1 trims each line first, and 10.0.0rc2 does not. A line that matches no input has no effect. ↩

  6. Recompilation avoidance in rules_haskell — ABI-file extraction and cache-control pattern for Haskell interface files. ↩1 ↩2

  7. Recompilation avoidance in rules_haskell — interface files remain action inputs and are still available when recompilation occurs. ↩1 ↩2

  8. rules_haskell repository map — modern and legacy setup boundaries, GHC/package extensions, Nix/cross routes, and the private recompilation implementation paired with regression evidence ↩

  9. Discovering and eliminating 20% of redundant build actions by tracing file... - O Sjöberg & F Medley — Alternatives: .d File Pruning (built-in .d and unused_inputs_list pruning is local and gives no remote cache hits) and Implementation: the Action Pruner (access logs keyed by a reduced action, and a second action-cache entry for the pruned action). ↩

  10. actions — resource_set callback arguments, return keys, defaults, local-execution scope, and top-level function requirement. ↩1 ↩2 ↩3

  11. Bazel 9.2.0 release notes — exec_properties now respects resources:cpu and resources:memory for local resource estimation (#29426). ↩

  12. Bazel — core implementation, documentation, and regression corpus — Bazel applies resources:* entries from the action's execution info, then from the target's and execution platform's exec_properties, as overrides on the local resource estimate. ↩

  13. Bazel 10.0.0rc1 release notes — [Incompatible] exec_properties respects resources:cpu and resources:memory for local resource estimation. ↩