From ca9d6b815a06da87b1644ad1f09edad2f1c8ac2c Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 28 Oct 2022 13:06:49 +0200 Subject: [PATCH] Optionally output source files with `cquery --output=files` RELNOTES: `cquery --output=files` can be configured to additionally output source files with the new `--files:include_source_files` flag. --- site/en/query/cquery.md | 3 +- .../lib/query2/cquery/CqueryOptions.java | 8 ++++ .../cquery/FilesOutputFormatterCallback.java | 3 +- .../FilesOutputFormatterCallbackTest.java | 43 ++++++++++++++++--- .../integration/configured_query_test.sh | 16 +++++++ 5 files changed, 66 insertions(+), 7 deletions(-) diff --git a/site/en/query/cquery.md b/site/en/query/cquery.md index bd571871655c81..7628d91c82f82c 100644 --- a/site/en/query/cquery.md +++ b/site/en/query/cquery.md @@ -368,7 +368,8 @@ by the query similar to the list printed at the end of a `bazel build` invocation. The output contains only the files advertised in the requested output groups as determined by the [`--output_groups`](/reference/command-line-reference#flag--output_groups) flag -and never contains source files. +and does not contain source files unless `--files:include_source_files` is +specified. Note: The output of `bazel cquery --output=files //pkg:foo` contains the output files of `//pkg:foo` in *all* configurations that occur in the build (also see diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/CqueryOptions.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/CqueryOptions.java index 0d85d0db680ea7..c2e670780f6522 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/CqueryOptions.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/CqueryOptions.java @@ -107,4 +107,12 @@ public enum Transitions { + " error to specify both --starlark:expr and --starlark:file. See help for" + " --output=starlark for additional detail.") public String file; + + @Option( + name = "files:include_source_files", + defaultValue = "false", + documentationCategory = OptionDocumentationCategory.QUERY, + effectTags = {OptionEffectTag.TERMINAL_OUTPUT}, + help = "If true, source files are included in the output of cquery with --output=files.") + public boolean includeSourceFiles; } diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallback.java index d0ceeeaef5b088..b401aba46caf64 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallback.java @@ -60,7 +60,8 @@ public void processOutput(Iterable partialResult) .getImportantArtifacts() .toList() .stream() - .filter(TopLevelArtifactHelper::shouldDisplay) + .filter(artifact -> TopLevelArtifactHelper.shouldDisplay(artifact) || ( + artifact.isSourceArtifact() && options.includeSourceFiles)) .map(Artifact::getExecPathString) .forEach(this::addResult); } diff --git a/src/test/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallbackTest.java b/src/test/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallbackTest.java index 5aeb5dd7d0f664..295bf048522a5c 100644 --- a/src/test/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallbackTest.java +++ b/src/test/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallbackTest.java @@ -33,6 +33,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; @@ -71,7 +72,7 @@ public final void defineSimpleRule() throws Exception { " runfiles = ctx.runfiles([runfile]),", " ),", " OutputGroupInfo(", - " foobar = [output_group_only],", + " foobar = [output_group_only, ctx.file.explicit_source_dep],", " ),", " ]", "r = rule(", @@ -105,7 +106,8 @@ public final void setUpCqueryOptions() { this.reporter = new Reporter(new EventBus(), events::add); } - private List getOutput(String queryExpression, List outputGroups) + private List getOutput(String queryExpression, List outputGroups, + boolean includeSourceFiles) throws Exception { QueryExpression expression = QueryParser.parse(queryExpression, getDefaultFunctions()); Set targetPatternSet = new LinkedHashSet<>(); @@ -113,6 +115,7 @@ private List getOutput(String queryExpression, List outputGroups PostAnalysisQueryEnvironment env = ((ConfiguredTargetQueryHelper) helper).getPostAnalysisQueryEnvironment(targetPatternSet); + options.includeSourceFiles = includeSourceFiles; ByteArrayOutputStream output = new ByteArrayOutputStream(); FilesOutputFormatterCallback callback = new FilesOutputFormatterCallback( @@ -133,24 +136,54 @@ private List getOutput(String queryExpression, List outputGroups @Test public void basicQuery_defaultOutputGroup() throws Exception { - List output = getOutput("//pkg:all", ImmutableList.of()); + List output = getOutput("//pkg:all", ImmutableList.of(), false); assertContainsExactlyWithBinDirPrefix( output, "pkg/main_default_file", "pkg/other_default_file"); } + @Test + public void basicQuery_defaultOutputGroup_includeSourceFiles() throws Exception { + List output = getOutput("//pkg:all", ImmutableList.of(), true); + var sourceAndGeneratedFiles = output.stream() + .collect(Collectors.partitioningBy(path -> path.startsWith("bazel-out/"))); + assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD", "defs/rules.bzl"); + assertContainsExactlyWithBinDirPrefix(sourceAndGeneratedFiles.get(true), + "pkg/main_default_file", "pkg/other_default_file"); + } + @Test public void basicQuery_defaultAndCustomOutputGroup() throws Exception { - List output = getOutput("//pkg:main", ImmutableList.of("+foobar")); + List output = getOutput("//pkg:main", ImmutableList.of("+foobar"), false); assertContainsExactlyWithBinDirPrefix( output, "pkg/main_default_file", "pkg/main_output_group_only"); } + @Test + public void basicQuery_defaultAndCustomOutputGroup_includeSourceFiles() throws Exception { + List output = getOutput("//pkg:main", ImmutableList.of("+foobar"), true); + var sourceAndGeneratedFiles = output.stream() + .collect(Collectors.partitioningBy(path -> path.startsWith("bazel-out/"))); + assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD", "defs/rules.bzl"); + assertContainsExactlyWithBinDirPrefix( + sourceAndGeneratedFiles.get(true), "pkg/main_default_file", "pkg/main_output_group_only"); + } + @Test public void basicQuery_customOutputGroupOnly() throws Exception { - List output = getOutput("//pkg:other", ImmutableList.of("foobar")); + List output = getOutput("//pkg:other", ImmutableList.of("foobar"), false); assertContainsExactlyWithBinDirPrefix(output, "pkg/other_output_group_only"); } + @Test + public void basicQuery_customOutputGroupOnly_includeSourceFiles() throws Exception { + List output = getOutput("//pkg:other", ImmutableList.of("foobar"), true); + var sourceAndGeneratedFiles = output.stream() + .collect(Collectors.partitioningBy(path -> path.startsWith("bazel-out/"))); + assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD"); + assertContainsExactlyWithBinDirPrefix(sourceAndGeneratedFiles.get(true), + "pkg/other_output_group_only"); + } + private void assertContainsExactlyWithBinDirPrefix( List output, String... binDirRelativePaths) { if (binDirRelativePaths.length == 0) { diff --git a/src/test/shell/integration/configured_query_test.sh b/src/test/shell/integration/configured_query_test.sh index ff1180c7afd269..c8cbc807fefb65 100755 --- a/src/test/shell/integration/configured_query_test.sh +++ b/src/test/shell/integration/configured_query_test.sh @@ -1430,4 +1430,20 @@ EOF expect_not_log "QueryException" } +function test_files_include_source_files() { + local -r pkg=$FUNCNAME + mkdir -p $pkg + cat > $pkg/BUILD <<'EOF' +filegroup(name="files", srcs=["BUILD"]) +EOF + + bazel cquery --output=files //$pkg:files \ + > output 2>"$TEST_log" || fail "Unexpected failure" + assert_not_contains "$pkg/BUILD" output + + bazel cquery --output=files --files:include_source_files //$pkg:files \ + > output 2>"$TEST_log" || fail "Unexpected failure" + assert_contains "$pkg/BUILD" output +} + run_suite "${PRODUCT_NAME} configured query tests"