【问题标题】:How to run an bazel binary executable from repository_ctx如何从 repository_ctx 运行 bazel 二进制可执行文件
【发布时间】:2025-12-08 15:55:02
【问题描述】:

在 Bazel 中,是否有一种等效的方法来运行如下所述的二进制可执行文件,但在 repository_rule 实现函数中使用 repository_ctx?

# def _impl
ctx.actions.run(
    ...
    executable = ctx.executable.foo_binary,
)

# Is doing the ff also possible for a repository_rule?
bar = rule(
    implementation = _impl,
    attrs = {
        "foo_binary": attr.label(
            default = Label("//actions_run:foo"),
            executable = True,
            cfg = "exec",
            allow_files = True,
        ),
    },
)

repository_ctx 的文档表明有一个 execute() 函数,但我不确定如何使用它运行另一个 bazel 构建的二进制文件。任何示例都会有所帮助。

PS:我是 Bazel 的新手。如果这不是 repository_ctx.execute 的用途,请重定向。

【问题讨论】:

    标签: bazel bazel-rules


    【解决方案1】:

    repository_ctx.execute() 相似但不一样...根本区别在于它们何时发生。

    repository_ctx可用于repository rule的实现:

    外部存储库是只能在 WORKSPACE 文件中使用的规则,并在 Bazel 的加载阶段启用非封闭操作

    ctx 用于在分析阶段 获得evaluated 的规则实现。

    换句话说。 repository_ctx.execute() 确实意味着在您的主机上运行(非封闭)工具(当然可以位于存储库中),但这仍然是在从树中构建任何东西并且可以访问执行之前。如果可执行文件在 repo 树中,您可以通过标签引用它(它将被解析),但它必须已经可用。

    【讨论】:

      最近更新 更多