我在这个场景中有一个“Parent”Bazel项目,其中包含一些shell脚本&宏。该项目必须由“儿童”项目重复使用。最小的 “工作” 的例子来重现问题定义如下:为父项目和子项目导出的shell脚本
目录结构
├── Child
│ ├── BUILD
│ └── WORKSPACE
└── Parent
├── a_command.bzl
├── a_script.sh
├── BUILD
└── WORKSPACE
父项目文件
父/ a_script.sh
#!/bin/bash
date
父/ a_command.bzl
def a_command(name):
native.genrule(
name = name,
srcs = [],
outs = [name+".txt"],
cmd = "$(location :a_script_sh) > [email protected]",
tools = [":a_script_sh"],
)
父/ BUILD
sh_binary(
name = "a_script_sh",
srcs = ["a_script.sh"],
visibility = ["//visibility:public"],
)
父/工作区
# empty file
儿童的项目文件
儿童/工作区
local_repository(
name = "Parent",
path = "../Parent/",
)
儿童/ BUILD
load("@Parent//:a_command.bzl","a_command")
# Uncomment me for a working solution
# alias(name="a_script_sh",actual="@Parent//:a_script_sh")
a_command("use_script")
现在我的问题
如果我在儿童项目目录键入
bazel build //...
我得到
INFO: Found 1 target...
ERROR: missing input file '//:a_script_sh'.
ERROR: PATH/Child/BUILD:5:1: //:use_script: missing input file '//:a_script_sh'.
Target //:use_script failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: PATH/Child/BUILD:5:1 1 input file(s) do not exist.
INFO: Elapsed time: 0.162s, Critical Path: 0.00s
现在,如果我取消对该行
alias(name="a_script_sh",actual="@Parent//:a_script_sh")
在
儿童
/BUILD文件,一切正常:
INFO: Found 2 targets...
INFO: Elapsed time: 0.191s, Critical Path: 0.03s
而文件use_script.txt已正确生成儿童/ bazel-genfiles目录。
我的问题是:
是否使用的
alias(name="a_script_sh",actual="@Parent//:a_script_sh")
是正确的方式做的事情吗?
我曾期待这是巴泽尔的工作,以解决这种依赖。
我没想到有明确重新定义其中a_script.sh文件。我认为这很尴尬,多余且容易出错。
我怀疑我没有做正确的事情,我会非常感谢任何能清楚地向我们解释如何正确做到这一点的人。
感谢您的积极反馈和解释。正如你自己写的那样:宏被扩展到使用它的地方(它毕竟只是一个宏:-)),并且我们不能在//::a_script_sh前缀,因为我们不知道Child项目中的未来边界。就像我在学习巴泽尔一样,我担心我错过了什么......再次感谢 –