多个 Bazel BUILD 文件出错:"Target 'bar' is not visible from target 'foo'"

Error with multiple Bazel BUILD files: "Target 'bar' is not visible from target 'foo'"

我的项目结构如下:

$ tree
.
├── bar
│   ├── bar.cpp
│   └── BUILD
├── BUILD
├── foo.cpp
└── WORKSPACE

./BUILD的内容:

cc_binary(
    name = "foo",
    srcs = [ "foo.cpp" ],
    deps = [ "//bar" ],
)

bar/BUILD的内容:

cc_library(
    name = "bar",
    srcs = ["bar.cpp"],
)

如果我构建 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.

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

来自Bazel docs

However, by default, build rules are private. This means that they can only be referred to by rules in the same BUILD file. [...] You can make a rule visibile to rules in other BUILD files by adding a visibility = level attribute.

在这种情况下,bar/BUILD 应如下所示:

cc_library(
    name = "bar",
    srcs = ["bar.cpp"],
    visibility = ["//__pkg__"],
)

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

visibility = ["//__pkg__"] 对我不起作用。 但我设法通过添加

使其工作
package(default_visibility = ["//visibility:public"])

作为 bar/BUILD 文件的第一行。