feature request: new bazel query function to help query macros
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 72
Description
### Description of the problem / feature request:
Bazel query today operates at a target level. It helpfully also includes generator_name, generator_function, and generator_location attributes for targets generated from a macro; but there's no way to effectively filter rules generated by macros.
In particular, I was writing a query to find dead targets in our codebase, which was roughly (vastly simplified version) `kind(rule, //...) - deps(kind(.*_binary, //...) + tests(//...))`; the assumption here being that every binary and test target were used, and anything not transitive deps of those, is not. Those assumptions aren't perfect, but they are good enough for now.
One of the problems I ran into, however, is that I didn't want to consider some targets generated by a macro dead if others weren't. For example, we have a spark macro that generates a binary and some metadata targets, and we deploy all of these together; but the binary it generates doesn't depend on the metadata. Whether this setup is ideal, I didn't dig in to give an opinion on that. What I wanted was for my query to ignore the metadata targets, since one of the targets generated from the macro was considered used (the binary).
### Feature requests: what underlying problem are you trying to solve with this feature?
For this particular use case, I think a new bazel query operator would make sense: `same_package_same_generator_name` (name could probably use work). I.e. if there's a build file like
```
# package //foo
whatever_macro( # generates ":bar", "bar_foo", "bar_baz"
name = "bar"
...
)
```
then `bazel query "same_package_same_generator_name(//foo:bar)"`, `bazel query "same_package_same_generator_name(//foo:bar_foo)"`, and `bazel query "same_package_same_generator_name(//foo:bar_baz)"` would all evaluate to
```
//foo:bar
//foo:bar_baz
//foo:bar_foo
```
For targets without a `generator_name`, this function would instead behave as the identity function.
Then my query for dead targets could be
```
kind(rule, //...) - same_package_same_generator_function(deps(kind(.*_binary, //...) + tests(//...)))
```
and this way it would consider all targets of a macro "live" if any of the targets generated by the macro are used.
### What operating system are you running Bazel on?
Linux
### What's the output of `bazel info release`?
release 3.3.0-5dd95469d237592d70e55fcc0e029f24bc07516b
### What's the output of `git remote get-url origin ; git rev-parse master ; git rev-parse HEAD` ?
not relevant
### Have you found anything relevant by searching the web?
I read through the bazel query how-to and reference, but couldn't find anything useful for this use case.
### Any other information, logs, or outputs that you want to share?
nope
Contributor guide
Research direction
Start with the Bazel query how-to and query reference mentioned in the issue, then trace the existing generator_name and generator_function query behavior. Define the new operator's behavior for macro-generated targets and identity behavior for targets without generator_name, and verify it against the //foo examples and the dead-target query shown.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100