bazelbuild/bazel

Allow `resource_set`'s callback to return custom resources

Open

#27,950 opened on 2025年12月11日

GitHub で見る
 (4 comments) (3 reactions) (0 assignees)Java (4,465 forks)batch import
P3help wantedteam-Local-Exectype: feature request

Repository metrics

Stars
 (25,384 stars)
PR merge metrics
 (平均マージ 22d 20h) (30d で 77 merged PRs)

説明

Description of the feature request:

We would like to allow resource_set arguments to return any kind of resources. In particular, those defined by --local_resources.

Currently, the resource_set attribute of ctx.actions.run (an similar) can only be set to a function returning a dictionary with a limited set of keys/resources (mem, cpu, and local_test). As such, attempting to use any kind of custom resource:

def _some_resources(os, actions):
    return {
        "potatoes": 3,
    }

def _some_rule_imp(ctx):
    ...
    ctx.actions.run_shell(
        ...
        mnemonic = "SomeAction",
        resource_set = _some_resources,
    )

Leads to an error (last_green 2025/12/11):

$ bazel build //test:build_1
...
ERROR: .../test/BUILD:3:11: SomeAction test/build_1_out.txt failed: Could not build resources for SomeAction. Illegal resource keys: (potatoes)

This limitations seems arbitrary since ResourceManager.java seems more than capable to handle custom resources. Interestingly, if one fiddles with StarlarkActionFactory.java and removes the limitation in StarlarkActionFactory.buildResourceSetit all appears to work fine 🎉

Which category does this issue belong to?

Local Execution

What underlying problem are you trying to solve with this feature?

We would like to limit the concurrency of arbitrary build actions using resource_set and --local_resources. In our particular case we are trying to stay below the number of concurrent invocations a tool's license allows. However, this can be useful in other contexts (i.e., bazelbuild/rules_rust#3101 and #10443)

コントリビューターガイド