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

From the 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.

In this case, bar/BUILD should look as follows:

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

The additional line visibility = ["//__pkg__"] allows all BUILD files in the current WORKSPACE to access the target bar.


visibility = ["//__pkg__"] didn't work for me. But I've managed to make it work by adding

package(default_visibility = ["//visibility:public"])

as the first line of the bar/BUILD file.

Tags:

Bazel