多个Bazel BUILD文件出错:"目标'栏'在目标'foo'中不可见"

mor*_*rxa 3 bazel

我的项目如下结构:

$ tree
.
??? bar
?   ??? bar.cpp
?   ??? BUILD
??? BUILD
??? foo.cpp
??? WORKSPACE
Run Code Online (Sandbox Code Playgroud)

内容./BUILD:

cc_binary(
    name = "foo",
    srcs = [ "foo.cpp" ],
    deps = [ "//bar" ],
)
Run Code Online (Sandbox Code Playgroud)

内容bar/BUILD:

cc_library(
    name = "bar",
    srcs = ["bar.cpp"],
)
Run Code Online (Sandbox Code Playgroud)

如果我构建foo,我会收到以下错误:

Target '//bar:bar' is not visible from target '//:foo'. Check the visibility declaration of the former target if you think the dependency is legitimate.
Run Code Online (Sandbox Code Playgroud)

我需要做什么才能解决依赖关系并foo成功构建?

flo*_*olu 11

visibility = ["//__pkg__"]不适合我。但我已经成功地通过添加使其工作

package(default_visibility = ["//visibility:public"])
Run Code Online (Sandbox Code Playgroud)

作为文件的第一行bar/BUILD


mor*_*rxa 7

来自Bazel文档:

但是,默认情况下,构建规则是私有的.这意味着它们只能由同一BUILD文件中的规则引用.[...]您可以通过添加visibility = level属性使规则可以访问其他BUILD文件中的规则.

在这种情况下,bar/BUILD应该看如下:

cc_library(
    name = "bar",
    srcs = ["bar.cpp"],
    visibility = ["//__pkg__"],
)
Run Code Online (Sandbox Code Playgroud)

附加行visibility = ["//__pkg__"]允许BUILD当前WORKSPACE中的所有文件访问目标bar.