Skip to content

Commit 24c980b

Browse files
Googlercopybara-github
Googler
authored andcommitted
Make ShowIncludesFilter compatible with the sibling repository layout by grabbing everything under the execroot base and prepend the collected paths with "..\".
PiperOrigin-RevId: 356735700
1 parent 81a5a12 commit 24c980b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/ShowIncludesFilter.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ public static class FilterShowIncludesOutputStream extends FilterOutputStream {
148148
StandardCharsets.UTF_8) // Spanish
149149
);
150150
private final String sourceFileName;
151-
private static final Pattern EXECROOT_HEADER_PATTERN =
152-
Pattern.compile(".*execroot\\\\[^\\\\]*\\\\(?<headerPath>.*)");
151+
// Grab everything under the execroot base so that external repository header files are covered
152+
// in the sibling repository layout.
153+
private static final Pattern EXECROOT_BASE_HEADER_PATTERN =
154+
Pattern.compile(".*execroot\\\\(?<headerPath>.*)");
153155

154156
public FilterShowIncludesOutputStream(OutputStream out, String sourceFileName) {
155157
super(out);
@@ -165,9 +167,13 @@ public void write(int b) throws IOException {
165167
for (String prefix : SHOW_INCLUDES_PREFIXES) {
166168
if (line.startsWith(prefix)) {
167169
line = line.substring(prefix.length()).trim();
168-
Matcher m = EXECROOT_HEADER_PATTERN.matcher(line);
170+
Matcher m = EXECROOT_BASE_HEADER_PATTERN.matcher(line);
169171
if (m.matches()) {
170-
line = m.group("headerPath");
172+
// Prefix the matched header path with "..\". This way, external repo header paths are
173+
// resolved to "<execroot>\..\<repo name>\<path>", and main repo file paths are
174+
// resolved to "<execroot>\..\<main repo>\<path>", which is nicely normalized to
175+
// "<execroot>\<path>".
176+
line = "..\\" + m.group("headerPath");
171177
}
172178
dependencies.add(line);
173179
prefixMatched = true;

src/test/java/com/google/devtools/build/lib/rules/cpp/ShowIncludesFilterTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testMatchAllOfNotePrefix() throws IOException {
9595

9696
@Test
9797
// Regression tests for https://github.com/bazelbuild/bazel/issues/9172
98-
public void testFindHeaderFromAbsolutePathUnderExecroot() throws IOException {
98+
public void testFindHeaderFromAbsolutePathUnderExecrootBase() throws IOException {
9999
// "Note: including file:" is the prefix
100100
filterOutputStream.write(
101101
getBytes("Note: including file: C:\\tmp\\xxxx\\execroot\\__main__\\foo\\bar\\bar.h"));
@@ -105,7 +105,7 @@ public void testFindHeaderFromAbsolutePathUnderExecroot() throws IOException {
105105
filterOutputStream.write(getBytes("\n"));
106106
// It's a match, output should be filtered, dependency on bar.h should be found.
107107
assertThat(output.toString()).isEmpty();
108-
assertThat(showIncludesFilter.getDependencies()).contains("foo\\bar\\bar.h");
108+
assertThat(showIncludesFilter.getDependencies()).contains("..\\__main__\\foo\\bar\\bar.h");
109109
}
110110

111111
@Test

0 commit comments

Comments
 (0)