bazelbuild/bazel

Allow `resource_set`'s callback to return custom resources

Open

#27,950 建立於 2025年12月11日

在 GitHub 查看
 (4 留言) (3 反應) (0 負責人)Java (4,465 fork)batch import
P3help wantedteam-Local-Exectype: feature request

倉庫指標

Star
 (25,384 star)
PR 合併指標
 (平均合併 22天 20小時) (30 天內合併 77 個 PR)

描述

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)

貢獻者指南