Skip to content

Commit

Permalink
Make ShowIncludesFilter compatible with the sibling repository layout…
Browse files Browse the repository at this point in the history
… by grabbing everything under the execroot base and prepend the collected paths with "..\".

PiperOrigin-RevId: 356735700
  • Loading branch information
Googler authored and copybara-github committed Feb 10, 2021
1 parent 81a5a12 commit 24c980b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ public static class FilterShowIncludesOutputStream extends FilterOutputStream {
StandardCharsets.UTF_8) // Spanish
);
private final String sourceFileName;
private static final Pattern EXECROOT_HEADER_PATTERN =
Pattern.compile(".*execroot\\\\[^\\\\]*\\\\(?<headerPath>.*)");
// Grab everything under the execroot base so that external repository header files are covered
// in the sibling repository layout.
private static final Pattern EXECROOT_BASE_HEADER_PATTERN =
Pattern.compile(".*execroot\\\\(?<headerPath>.*)");

public FilterShowIncludesOutputStream(OutputStream out, String sourceFileName) {
super(out);
Expand All @@ -165,9 +167,13 @@ public void write(int b) throws IOException {
for (String prefix : SHOW_INCLUDES_PREFIXES) {
if (line.startsWith(prefix)) {
line = line.substring(prefix.length()).trim();
Matcher m = EXECROOT_HEADER_PATTERN.matcher(line);
Matcher m = EXECROOT_BASE_HEADER_PATTERN.matcher(line);
if (m.matches()) {
line = m.group("headerPath");
// Prefix the matched header path with "..\". This way, external repo header paths are
// resolved to "<execroot>\..\<repo name>\<path>", and main repo file paths are
// resolved to "<execroot>\..\<main repo>\<path>", which is nicely normalized to
// "<execroot>\<path>".
line = "..\\" + m.group("headerPath");
}
dependencies.add(line);
prefixMatched = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testMatchAllOfNotePrefix() throws IOException {

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

@Test
Expand Down

0 comments on commit 24c980b

Please sign in to comment.