简体   繁体   English

如何从 Bazel 规则查询兄弟规则

[英]How to query sibling rules from a Bazel rule

I would like to be able to do the following in a Bazel BUILD file:我希望能够在 Bazel BUILD 文件中执行以下操作:

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

beta(
  name = "hello again"
)

Where alpha and beta are custom rules.其中alphabeta是自定义规则。 I want beta to be able to access the color attribute of the alpha rule, without adding a label attribute .我希望beta能够访问alpha规则的color属性,而无需添加 label 属性 In Bazel query, I can do something like this:在 Bazel 查询中,我可以这样做:

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

which gives me the beta which is side by side to alpha .这给了我与alpha并排的beta Can I achieve the same somehow from within the implementation function of the beta rule?我能以某种方式从beta规则的实施 function 中实现相同的目标吗?

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我已经看到这是用这样的 label 完成的

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.但我觉得这有点冗长,特别是因为有兄弟查询支持。

The way the question is formulated, the answer is no.问题的表述方式,答案是否定的。 It is not possible.这不可能。

Bazel design philosophy is to be explicit about target dependencies. Bazel 的设计理念是明确目标依赖关系。 Providers mechanism is meant to provide the access to the dependency graph information during the analysis phase. 提供者机制旨在在分析阶段提供对依赖图信息的访问。

It is difficult to tell what is the actual use case is.很难说实际用例是什么。 Using Aspects might be the answer.使用 方面可能是答案。

In my scenario, I'm trying to get a genrule to call a test rule before proceeding:在我的场景中,我试图在继续之前获得一个genrule来调用test规则:

genrule(
    name = "generate_buf_image",
    srcs = [":protos", "cookie"],
    outs = ["buf-image.json"],
    cmd = "$(location //third_party/buf:cas_buf_image) //example-grpc/proto/v1:proto_backwards_compatibility_check $(SRCS) >$(OUTS)",
    tools = [
        "//third_party/buf:cas_buf_image",
        "@buf",
    ],
)

If cas_buf_image.sh has ls -l "example-grpc/proto/v1" >&2 , it shows:如果cas_buf_image.shls -l "example-grpc/proto/v1" >&2 ,它显示:

… cookie -> …/example-grpc/proto/v1/cookie
… example.proto -> …/example-grpc/proto/v1/example.proto

IOW, examining what example-grpc/proto/v1/cookie is linked to and cd ing to its directory then performing the git commands should work. IOW,检查example-grpc/proto/v1/cookie链接到什么并cd到它的目录然后执行git命令应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM