'How to resolve bazel "undeclared inclusion(s)" error?

I'm new to bazel, and I'm getting a failure to build my C++ package with

ERROR: /path/to/package/BUILD:linenumber:1 undeclared inclusion(s) in rule '//path/to/package:name': this rule is missing dependency declarations for the following files included by 'path/to/package/source_file.cpp'

...followed by a list of header files in a different directory. These files are not part of the package that is being built, but are being pulled in from elsewhere.

My question is how to properly add the declaration to the BUILD file to resolve the error?

According to the online Bazel docu here I should add each header to the srcs list. (To be clear, these are headers that are used internally by the library I'm building and not part of the public interface, so they don't belong in hdrs.) But If I try that,

  srcs = [ ..., "path/to/dependent/headers/header.h",]

I get an error message

ERROR: ... crosses boundary of subpackage ... (perhaps you meant to put the colon here: ...?)

because the directory with the headers isn't a Bazel package.

If I try changing the final / to a colon as that error message suggests,

  srcs = [ ..., "path/to/dependent/headers:header.h",]

then

ERROR: ... target names may not contain ':'.

The Bazel C++ tutorial here, in the section "Additonal Include Paths" says that external include directories should be declared via copts:

cc_library(
    name = "some_lib",
    srcs = ["some_lib.cc"],
    hdrs = ["some_lib.h"],
    copts = ["-Ithird_party/some_lib"],
)

But adding that -I flag does not git rid of the "undeclared inclusion(s)" error!

$ bazel version
Build label: 0.4.3
Build target: bazel-out/local-fastbuild/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Thu Dec 22 12:31:25 2016 (1482409885)
Build timestamp: 1482409885
Build timestamp as int: 1482409885


Solution 1:[1]

I met similar prolblem "undeclared inclusion(s) in rule", I solved by removing the bazel cache files in /root/.cache/bazel/. Hope helps

Solution 2:[2]

try out this, this worked for me

bazel clean --expunge

this command will clean up all disk and memory traces of a Bazel instance. reference : https://docs.bazel.build/versions/main/user-manual.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bidai
Solution 2 desertnaut