Skip to content

Commit

Permalink
Always include source files with --output=files
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Nov 16, 2022
1 parent ca94373 commit 793b295
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 48 deletions.
5 changes: 2 additions & 3 deletions site/en/query/cquery.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,8 @@ This option prints a list of the output files produced by each target matched
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 does not contain source files unless `--files:include_source_files` is
specified.
[`--output_groups`](/reference/command-line-reference#flag--output_groups) flag.
It does include source files.

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,4 @@ 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ public void processOutput(Iterable<KeyedConfiguredTarget> partialResult)
throws IOException, InterruptedException {
for (KeyedConfiguredTarget keyedTarget : partialResult) {
ConfiguredTarget target = keyedTarget.getConfiguredTarget();
if (!TopLevelArtifactHelper.shouldConsiderForDisplay(target) && !(options.includeSourceFiles
&& target instanceof InputFileConfiguredTarget)) {
if (!TopLevelArtifactHelper.shouldConsiderForDisplay(target)
&& !(target instanceof InputFileConfiguredTarget)) {
continue;
}
TopLevelArtifactHelper.getAllArtifactsToBuild(target, topLevelArtifactContext)
.getImportantArtifacts()
.toList()
.stream()
.filter(artifact -> TopLevelArtifactHelper.shouldDisplay(artifact) || (
artifact.isSourceArtifact() && options.includeSourceFiles))
.filter(artifact -> TopLevelArtifactHelper.shouldDisplay(artifact)
|| artifact.isSourceArtifact())
.map(Artifact::getExecPathString)
.forEach(this::addResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,14 @@ public final void setUpCqueryOptions() {
this.reporter = new Reporter(new EventBus(), events::add);
}

private List<String> getOutput(String queryExpression, List<String> outputGroups,
boolean includeSourceFiles)
private List<String> getOutput(String queryExpression, List<String> outputGroups)
throws Exception {
QueryExpression expression = QueryParser.parse(queryExpression, getDefaultFunctions());
Set<String> targetPatternSet = new LinkedHashSet<>();
expression.collectTargetPatterns(targetPatternSet);
PostAnalysisQueryEnvironment<KeyedConfiguredTarget> env =
((ConfiguredTargetQueryHelper) helper).getPostAnalysisQueryEnvironment(targetPatternSet);

options.includeSourceFiles = includeSourceFiles;
ByteArrayOutputStream output = new ByteArrayOutputStream();
FilesOutputFormatterCallback callback =
new FilesOutputFormatterCallback(
Expand All @@ -136,14 +134,7 @@ private List<String> getOutput(String queryExpression, List<String> outputGroups

@Test
public void basicQuery_defaultOutputGroup() throws Exception {
List<String> 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<String> output = getOutput("//pkg:all", ImmutableList.of(), true);
List<String> output = getOutput("//pkg:all", ImmutableList.of());
var sourceAndGeneratedFiles = output.stream()
.collect(Collectors.<String>partitioningBy(path -> path.startsWith("bazel-out/")));
assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD", "defs/rules.bzl");
Expand All @@ -153,14 +144,7 @@ public void basicQuery_defaultOutputGroup_includeSourceFiles() throws Exception

@Test
public void basicQuery_defaultAndCustomOutputGroup() throws Exception {
List<String> 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<String> output = getOutput("//pkg:main", ImmutableList.of("+foobar"), true);
List<String> output = getOutput("//pkg:main", ImmutableList.of("+foobar"));
var sourceAndGeneratedFiles = output.stream()
.collect(Collectors.<String>partitioningBy(path -> path.startsWith("bazel-out/")));
assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD", "defs/rules.bzl");
Expand All @@ -170,13 +154,7 @@ public void basicQuery_defaultAndCustomOutputGroup_includeSourceFiles() throws E

@Test
public void basicQuery_customOutputGroupOnly() throws Exception {
List<String> output = getOutput("//pkg:other", ImmutableList.of("foobar"), false);
assertContainsExactlyWithBinDirPrefix(output, "pkg/other_output_group_only");
}

@Test
public void basicQuery_customOutputGroupOnly_includeSourceFiles() throws Exception {
List<String> output = getOutput("//pkg:other", ImmutableList.of("foobar"), true);
List<String> output = getOutput("//pkg:other", ImmutableList.of("foobar"));
var sourceAndGeneratedFiles = output.stream()
.collect(Collectors.<String>partitioningBy(path -> path.startsWith("bazel-out/")));
assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD");
Expand Down
8 changes: 1 addition & 7 deletions src/test/shell/integration/configured_query_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1454,13 +1454,7 @@ alias(name="alias", actual="single_file")
EOF
touch $pkg/single_file

bazel cquery --output=files //$pkg:all \
> output 2>"$TEST_log" || fail "Unexpected failure"
assert_not_contains "$pkg/BUILD" output
assert_not_contains "$pkg/single_file" output

bazel cquery --output=files --files:include_source_files //$pkg:all \
> output 2>"$TEST_log" || fail "Unexpected failure"
bazel cquery --output=files //$pkg:all > output 2>"$TEST_log" || fail "Unexpected failure"
assert_contains "$pkg/BUILD" output
assert_contains "$pkg/single_file" output
}
Expand Down

0 comments on commit 793b295

Please sign in to comment.