如何在 WORKSPACE 中引用环境变量?

How to reference environmental variables in WORKSPACE?

如果项目对本地安装的软件具有外部依赖性,并且由于许可或大小问题导致无法下载而无法使用 new_http_archivenew_git_repository 进行管理,有没有办法要让 new_local_repository 指定的路径使用环境变量?

查看 comments on Issue #746,这似乎已以某种方式解决。但是我找不到任何关于如何操作的文档。

bazel 中的首选方法是将工具也放在源代码存储库中,因为这是获得可重现构建的唯一方法。

不过,我觉得you can use environment variables as part of an action and you can reference environment variables in rules

我没有测试过,但我认为规则应该是这样的proprietary_tool.bzl:

def _proprietary_tool_impl(ctx):
    env = ctx.configuration.default_shell_env
    ctx.actions.run(
      inputs=ctx.attr.srcs,
      outputs=ctx.attr.out,
      executable=env['PROPRIETARY_TOOL_PATH'] + "/bin/tool",
    )

proprietary_tool = rule(
    implementation=_proprietary_tool_impl,
    attrs={}, # Add attributes needed by the proprietary tool
)

Looking at the comments on Issue #746, it looks like this was solved in some way. But I can't find any documentation on how to do it.

感谢您检查现有错误!不过,该错误已有近 2 年历史了。从那时起,Bazel 发生了翻天覆地的变化。

对于这个问题,我认为你应该在 .bzl 文件中编写你自己的存储库规则。

允许存储库规则执行非密封操作、读取环境、查看整个文件系统等。普通构建规则不允许,也不能访问环境或完整文件系统。

您的存储库规则应检查 envvar 的值并编写一个 BUILD 文件,Bazel 将在您的规则创建的外部存储库中使用该文件。

在这里查看我的回答示例: