Skip to content

select() is not resolved when used in a list comprehension or dict.values() #8171

@mexlez

Description

@mexlez

Description of the problem / feature request:

If a dictionary or list contains a select(), and that dict/list is used in a list comprehension (i.e. when constructing a string of all keys in the dict), the select() is not resolved.

The select() is also not resolved when accessing the values in a dict using dict.values().

Using a for loop to construct a string in a macro instead of a list comprehension expands the select() as expected. However, constructing a list in a macro does not expand the select().

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

test.bzl:

# select() resolved
def test_stringify(d):
  s = ""
  for key in d:
    s += d[key] + "\n"
  return s

# select() NOT resolved
def test_listify(d):
  l = []
  for key in d:
    l.append(d[key])
  return l

BUILD.bazel:

load("//:test.bzl", "test_stringify", "test_listify")
d = {
  "1": "dict_constant",
  "2": select({
      "//conditions:default": "dict_selected"
    })
}
key_in_variable = "2"

l = [
  "list_constant",
  select({
    "//conditions:default": "list_selected"
    })
]

genrule (
  name = "test",
  outs = ["test"],
  cmd = "echo '" +
      "#### Select() is *NOT* expanded by these incantations:\n" +
      "\n".join([d[x] for x in d]) + "\n\n" +
      "\n".join(d.values()) + "\n\n" +
      "{}".format(d.values()) + "\n\n" +
      "\n".join([d[k] for (k, x) in zip(d.keys(), d.values())]) + "\n\n" +
      "\n".join(l) + "\n\n" +
      "\n".join([l[i] for i in range(len(l))]) + "\n\n" +
      str(test_listify(d)) + "\n\n" +
      "#### Select() *IS* expanded by these incantations:\n" +
      l[1] + "\n\n" +
      d["2"] + "\n\n" +
      d[key_in_variable] + "\n\n" +
      test_stringify(d) +
  "' > $(location test)"
)
$ bazel build //:test && cat bazel-genfiles/test
#### Select() is *NOT* expanded by these incantations:
dict_constant
select({"//conditions:default": "dict_selected"})

dict_constant
select({"//conditions:default": "dict_selected"})

["dict_constant", select({"//conditions:default": "dict_selected"})]

dict_constant
select({"//conditions:default": "dict_selected"})

list_constant
select({"//conditions:default": "list_selected"})

list_constant
select({"//conditions:default": "list_selected"})

["dict_constant", select({"//conditions:default": "dict_selected"})]

#### Select() *IS* expanded by these incantations:
list_selected

dict_selected

dict_selected

dict_constant
dict_selected

What operating system are you running Bazel on?

Ubuntu 18.04

What's the output of bazel info release?

release 0.22.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3We're not considering working on this, but happy to review a PR. (No assignee)staleIssues or PRs that are stale (no activity for 30 days)team-DocumentationDocumentation improvements that cannot be directly linked to other team labelstype: documentation (cleanup)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions