14

My project as the following structure:

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

Content of ./BUILD:

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

Content of bar/BUILD:

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

If I build foo, I get the following error:

Target '//bar:bar' is not visible from target '//:foo'. Check the visibility declaration of the former target if you think the dependency is legitimate.

What do I need to do so the dependency can be resolved and foo is built successfully?

morxa
  • 3,221
  • 3
  • 27
  • 43

2 Answers2

11

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.

Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137
10

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.

morxa
  • 3,221
  • 3
  • 27
  • 43