Symbolic macros using attr.string_list
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 75
Description
### Page link:
https://bazel.build/rules/lib/toplevel/attr
### Problem description (include actual vs expected text, if applicable):
I'm trying to convert a legacy macro to symbolic macro, but have trouble using `attr.string_list`.
We have `oci_push` rules for pushing dev and prod oci images, defined as targets like `//serviceA:docker_dev`, `//serviceA:docker_prod`, etc., and a macro using [multirun](https://registry.bazel.build/modules/rules_multirun) to push batches of images in CI systems. the macro is defined like this:
```bzl
load("@rules_multirun//:defs.bzl", "multirun")
_rule_prefixes = [
"//serviceA:docker_",
...,
]
def multirun_rule(name, visibility, suffixes):
"""This macro generates multirun rules.
Args:
name: The name of the rule.
visibility: The visibility of the rule.
suffixes: Suffixes of the combination of "dev" and "prod".
"""
commands = []
for prefix in _rule_prefixes:
for suffix in suffixes:
commands.append(prefix + suffix)
multirun(
name = name,
commands = commands,
jobs = 0, # Set to 0 to run in parallel, defaults to sequential
visibility = visibility,
)
```
and then in top-level `BUILD.bazel` we just define 3 rules to be used in different CI pipelines:
```bzl
multirun_rule(
name = "docker_dev",
suffixes = ["dev"],
visibility = ["//visibility:public"],
)
multirun_rule(
name = "docker_prod",
suffixes = ["prod"],
visibility = ["//visibility:public"],
)
multirun_rule(
name = "docker_all",
suffixes = [
"dev",
"prod",
],
visibility = ["//visibility:public"],
)
```
in order to convert it to symbolic macro, I just renamed `multirun_rule` to `_multirun_rule_impl`, then added:
```bzl
multirun_rule = macro(
attrs = {
"suffixes": attr.string_list(mandatory = True, doc = 'Suffixes of the combination of "dev" and "prod"'),
},
implementation = _multirun_rule_impl,
)
```
but this causes it to complain about:
```
for suffix in suffixes:
Error: type 'select' is not iterable
```
### Where do you see this issue? (include link to specific section of the page, if applicable)
the doc for `attr.string_list` doesn't really explain how to access the actual string list. For how to use them it points to https://bazel.build/extending/rules#implementation_function, which only explains how to use them in rules, but not how to use them in symbolic macros (in rules they were accessed via `ctx`, which is not available in symbolic macros)
### Any other information you'd like to share?
_No response_
Contributor guide
Research direction
Read the attr.string_list documentation at https://bazel.build/rules/lib/toplevel/attr and the linked symbolic-macro material, then compare it with the provided _multirun_rule_impl example. The documentation is done when it explains how a symbolic macro implementation accesses the declared string list and avoids implying that rule implementation-function access through ctx applies there.
Written by the indexing model from the issue text.
Assessment
- Domain
- build-system, documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- Half a day
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100