-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
attr.string_list_dict
doesn't resolve select()
calls to values before type checking.
#3902
Comments
@laurentlb can you take a look please? |
Hi, Unfortunately At core, In your example, the While your request makes sense as a user, it requires decomposing Bazel's type system more deeply than that system supports. So we'd basically have to refactor the type system to make this work. We could theoretically add ad hoc support for dictionary values to make this work (similar to how foo(
my_attr = select({
"//conditions:default": {"hello": ["world"]}
}),
) In this modified example, the So these are potential paths forward but I suspect it'd be messy. |
Most importantly, do you have workarounds with the status quo? |
The workaround is to copy-paste the non-selected part of the dict into multiple select cases. It's not pretty, but is tolerable at our config sizes. Given that the |
I think the challenge is that the part of the type system in the Another concern is nesting selects across multiple types, i.e.: foo(
my_attr = select({
"//conditions:default": {
"hello": select({
"//conditions:default": ["world"]}}
}),
) So resolving this attribute would require evaluating two selects over different types. This would effectively defer type resolution to a far later phase in Blaze's build process - a phase that's not equipped for that kind of thing now. But I'm not sure how strong the call is for mixing selects this way, so maybe we can ignore this concern. |
We ran into this, wanted to have a
I ended up on a solution that does provide this interface, but complicates the underlying implementation. The approach is to add a macro layer above the rule (which generally wouldn't be used directly). The macro destructures the dictionary into a list of keys, and a list of values. It takes advantage of For example, this dict:
is transformed in into:
The macro then calls the rule, which has an attr for the keys, and an attr for the values. The rules's implementation can then easily recompose the dictionary from list of keys and the (now resolved) list of values, using |
Also FYI I prototyped nested selects in #1623 earlier this year. I can't remember if it handled different types though, as requested here. |
I think the most practical solution remains https://github.com/bazelbuild/bazel-skylib/blob/master/lib/selects.bzl, since it doesn't deeply mess with Starlark's type system. Also see https://docs.bazel.build/versions/master/configurable-attributes.html#or-chaining and the following section. Going forward, I suggest criteria for opening new bugs includes demonstrating how the skylib approach is insufficient and really evaluating what kind of organization with existing mechanisms is possible before considering enhancements. We really want to lean away from direct support for syntax like described here, even though I get the intuition from the user side (enough that I even prototyped it!). |
Given a rule with an attribute defined by
attr.string_list_dict()
, I'd expect the following two stanzas to be equivalent:However, Bazel rejects the second with a type error:
The text was updated successfully, but these errors were encountered: