When generating a symlink in _virtual_includes, add the original header to the 'allowed to use' set too
When a cc_library 'a' makes use of strip_include_prefix, bazel creates a symlink for the hdrs at bazel-out/.../bin/_virtual_includes/a/
Later, bazel passes -I bazel-out/.../bin/_virtual_includes/a/ to the command line to tell the compiler where to look for those headers.
For each header in hdrs, bazel will either put the header artifact itself in a set of 'allowed to use' headers, or, in the case of strip_include_prefix usage, bazel will add the symlink to the header under bazel-out/.../bin/_virtual_includes/a/ as 'allowed to use'.
When searching for headers, the compiler will do the following (Taken from https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html):
1. For the quote form of the include directive, the directory of the current file is searched first.
2. For the quote form of the include directive, the directories specified by -iquote options are searched in left-to-right order, as they appear on the command line.
3. Directories specified with -I options are scanned in left-to-right order.
...
In the case of the following cc_library:
```
cc_library(
name = 'foo',
hdrs = ["lib/foo.h"],
srcs = ["lib/foo.cc"],
strip_include_prefix = 'lib',
)
```
if foo.cc includes foo.h as #include "foo.h"
the compiler will find the header in step 1 of the search, thus will use the original header and not the symlink. The compiler will note this in the .d file. Bazel however added the symlink in the list of 'allowed to use' headers, so at some point it will error out with "undeclared inclusion(s) in rule" error due to the discrepancy.
This cl tells bazel to add the original header in the 'allowed to use' headers even when it generates a symlink in the _virtual_includes directory.
Fixes #3828 #6337
RELNOTES:None.
PiperOrigin-RevId: 344240730
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
index 59c68de..c2b11c6 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
@@ -949,9 +949,9 @@
virtualToOriginalHeaders.add(
Pair.of(virtualHeader.getExecPathString(), originalHeader.getExecPathString()));
}
- } else {
- moduleHeadersBuilder.add(originalHeader);
}
+
+ moduleHeadersBuilder.add(originalHeader);
}
ImmutableList<Artifact> moduleMapHeaders = moduleHeadersBuilder.build();
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java
index 3278276..0eeefc1 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonTest.java
@@ -840,7 +840,7 @@
ConfiguredTarget lib = getConfiguredTarget("//third_party/a");
CcCompilationContext ccCompilationContext = lib.get(CcInfo.PROVIDER).getCcCompilationContext();
assertThat(ActionsTestUtil.prettyArtifactNames(ccCompilationContext.getDeclaredIncludeSrcs()))
- .containsExactly("third_party/a/_virtual_includes/a/lib/b/c.h");
+ .containsExactly("third_party/a/_virtual_includes/a/lib/b/c.h", "third_party/a/v1/b/c.h");
assertThat(ccCompilationContext.getIncludeDirs())
.containsExactly(
getTargetConfiguration()
@@ -887,9 +887,10 @@
.getCcCompilationContext();
assertThat(ActionsTestUtil.prettyArtifactNames(relative.getDeclaredIncludeSrcs()))
- .containsExactly("third_party/a/_virtual_includes/relative/b.h");
+ .containsExactly("third_party/a/_virtual_includes/relative/b.h", "third_party/a/v1/b.h");
assertThat(ActionsTestUtil.prettyArtifactNames(absolute.getDeclaredIncludeSrcs()))
- .containsExactly("third_party/a/_virtual_includes/absolute/a/v1/b.h");
+ .containsExactly(
+ "third_party/a/_virtual_includes/absolute/a/v1/b.h", "third_party/a/v1/b.h");
}
@Test
diff --git a/src/test/shell/bazel/cc_integration_test.sh b/src/test/shell/bazel/cc_integration_test.sh
index b2d3c46..7b9756a 100755
--- a/src/test/shell/bazel/cc_integration_test.sh
+++ b/src/test/shell/bazel/cc_integration_test.sh
@@ -76,8 +76,8 @@
)
cc_binary(
- name = "bad",
- srcs = ["bad.cc"],
+ name = "still_ok",
+ srcs = ["still_ok.cc"],
deps = ["@foo//foo"],
)
EOF
@@ -90,7 +90,7 @@
}
EOF
- cat > bad.cc <<EOF
+ cat > still_ok.cc <<EOF
#include <stdio.h>
#include "foo/v1/foo.h"
int main() {
@@ -98,8 +98,34 @@
}
EOF
- bazel build :bad && fail "Should not have found include at repository-relative path"
bazel build :ok || fail "Should have found include at synthetic path"
+ bazel build :still_ok \
+ || fail "Should have found include at repository-relative path"
+}
+
+
+function test_include_validation_sandbox_disabled() {
+ local workspace="${FUNCNAME[0]}"
+ mkdir -p "${workspace}"/lib
+
+ create_workspace_with_default_repos "${workspace}/WORKSPACE"
+ cat >> "${workspace}/BUILD" << EOF
+cc_library(
+ name = "foo",
+ srcs = ["lib/foo.cc"],
+ hdrs = ["lib/foo.h"],
+ strip_include_prefix = "lib",
+)
+EOF
+ cat >> "${workspace}/lib/foo.cc" << EOF
+#include "foo.h"
+EOF
+
+ touch "${workspace}/lib/foo.h"
+
+ cd "${workspace}"
+ bazel build --spawn_strategy=standalone //:foo &>"$TEST_log" \
+ || fail "Build failed but should have succeeded"
}
function test_tree_artifact_headers_are_invalidated() {