Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bazel

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?

like image 740
morxa Avatar asked Apr 30 '16 11:04

morxa


People also ask

What bazelrc file does Bazel look for?

--bazelrc=x.rc --bazelrc=y.rc --bazelrc=/dev/null --bazelrc=z.rc x.rc and y.rc are read. z.rc is ignored due to the prior /dev/null. In addition to this optional configuration file, Bazel looks for a global rc file. For more details, see the global bazelrc section.

What is the use of the Java version flag in Bazel?

Bazel repository owners should set this flag so that Bazel and its users can reference the source code's Java version number. For more details, see Java language version flag. Bazel uses one JDK for compilation and another JVM to execute and test the code.

What versions of Java are supported by Bazel?

There are two relevant versions of Java that are set with configuration flags: Without an additional configuration, Bazel assumes all Java source files in the repository are written in a single Java version.

What is the difference between import and try-import in Bazel?

Lines that start with import or try-import are special: use these to load other "rc" files. To specify a path that is relative to the workspace root, write import %workspace%/path/to/bazelrc. The difference between import and try-import is that Bazel fails if the import 'ed file is missing (or can't be read), but not so for a try-import 'ed file.


1 Answers

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.

like image 52
morxa Avatar answered Sep 28 '22 05:09

morxa