Bazel: Feature request: allow attrs accept selects.

Created on 29 Aug 2016  路  8Comments  路  Source: bazelbuild/bazel

I tried to put a select of lists of strings to a rule's attr expecting a string_list or a label_list, but that didn't work. This seems an important feature missing, since without selects we have absolutely no flexibility to write BUILDs that work under different configurations.

Thanks.

P3 team-Configurability feature request

Most helpful comment

Thanks for the feature request, that makes sense.

The workaround for now is to wrap the rule with a macro, and use the select in the macro.

All 8 comments

When I think about it, shouldn't rules and aspects also have access to user configuration inputs, like the values of --cpu, --compiler and -c?

The built-in rules indeed behave differently when these configuration inputs change, so I can't see this being a technical impossibility. Supporting this would make rules closer to their built-in counterparts.

FYI:

Currently one can work around the problem where attrs cannot accept selects by adding a level of indirection: attrs can use labels and labels can point to filegroups or genrules that uses selects.

Similarly, rules and aspects can access various config_settings if you create some dummy files and a filegroup that uses select to conditionally depend on different dummy files. You can use an implicit dependency to depend on such a dummy filegroup and parse its filename to figure out whatever you need in config_settings.

These are tedious, but before the official Bazel team fixes this issue, you can use these tricks to work around the limits.

Do you have an example that doesn't work and Bazel output?

@ulfjack Thanks. That's a good question. I revisited this feature request and tested and can confirm that said rules can indeed accept selects.

My original intention, however, was to set the default value of an attr to a select. For example:

def _cat_impl(ctx):
    srcs = ctx.files.srcs
    output = ctx.outputs.cat

    ctx.action(
        command = "cat %s > %s" % (" ".join([x.path for x in srcs]), output.path),
        inputs = srcs,
        outputs = [output],
        )

cat = rule(
    implementation = _cat_impl,
    attrs = {
        "srcs": attr.label_list(allow_files=True, default=select({"//conditions:default": []})),
        },
    outputs = {
        "cat": "%{name}.cat",
        },
)

This fails:

ERROR: /home/<redacted-but-thats-me>/test/cat/cat.bzl:14:17: Traceback (most recent call last):
        File "/home/<redacted-but-thats-me>/test/cat/cat.bzl", line 11
                rule(implementation = _cat_impl, attrs ...": []}))}, ..."})
        File "/home/<redacted-but-thats-me>/test/cat/cat.bzl", line 14, in rule
                attr.label_list(allow_files = True, default = sele...": []}))
expected sequence of Labels or sequence of Labels-returning function for 'default' while calling label_list but got select of list instead: selector({"//conditions:default": []}).
ERROR: error loading package 'cat': Extension file 'cat/cat.bzl' has errors.

But I wish that could be accepted.

@laurentlb

Thanks for the feature request, that makes sense.

The workaround for now is to wrap the rule with a macro, and use the select in the macro.

This is the same issue as #287.

Duplicate of #287

Was this page helpful?
0 / 5 - 0 ratings