How to query sibling rules from a Bazel rule

I would like to be able to do the following in a Bazel BUILD file:

alpha(
  name = "hello world",
  color = "blue"
)

beta(
  name = "hello again"
)

Where alpha and beta are custom rules. I want beta to be able to access the color attribute of the alpha rule, without adding a label attribute. In Bazel query, I can do something like this:

bazel query 'kind(beta, siblings(kind(alpha, //...)))'

which gives me the beta which is side by side to alpha. Can I achieve the same somehow from within the implementation function of the beta rule?

def _beta_rule_impl(ctx):
  # This does not exist, I wish it did: ctx.siblings(kind='alpha')

I've seen this been done with a label like this

beta(
  name = "hello again",
  alpha_link = ":hello world" # explicitly linking
)

but I find this a bit verbose, especially since there is a sibling query support.



Read more here: https://stackoverflow.com/questions/67006196/how-to-query-sibling-rules-from-a-bazel-rule

Content Attribution

This content was originally published by Nikolaos Georgiou at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: