Skip to content

Commit ca8674c

Browse files
authored
Include source files with cquery --output=files (#16826)
RELNOTES[INC]: `cquery --output=files` also outputs source files. Closes #16602. PiperOrigin-RevId: 490403247 Change-Id: I3279b72a5b8dea93b58e22ecc2fbc24a40d9ae8b
1 parent 845077f commit ca8674c

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

site/en/query/cquery.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ This option prints a list of the output files produced by each target matched
367367
by the query similar to the list printed at the end of a `bazel build`
368368
invocation. The output contains only the files advertised in the requested
369369
output groups as determined by the
370-
[`--output_groups`](/reference/command-line-reference#flag--output_groups) flag
371-
and never contains source files.
370+
[`--output_groups`](/reference/command-line-reference#flag--output_groups) flag.
371+
It does include source files.
372372

373373
Note: The output of `bazel cquery --output=files //pkg:foo` contains the output
374374
files of `//pkg:foo` in *all* configurations that occur in the build (also see

src/main/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallback.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
1818
import com.google.devtools.build.lib.analysis.TopLevelArtifactContext;
1919
import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper;
20+
import com.google.devtools.build.lib.analysis.configuredtargets.InputFileConfiguredTarget;
2021
import com.google.devtools.build.lib.events.ExtendedEventHandler;
2122
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetAccessor;
2223
import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
@@ -53,14 +54,17 @@ public void processOutput(Iterable<KeyedConfiguredTarget> partialResult)
5354
throws IOException, InterruptedException {
5455
for (KeyedConfiguredTarget keyedTarget : partialResult) {
5556
ConfiguredTarget target = keyedTarget.getConfiguredTarget();
56-
if (!TopLevelArtifactHelper.shouldConsiderForDisplay(target)) {
57+
if (!TopLevelArtifactHelper.shouldConsiderForDisplay(target)
58+
&& !(target instanceof InputFileConfiguredTarget)) {
5759
continue;
5860
}
5961
TopLevelArtifactHelper.getAllArtifactsToBuild(target, topLevelArtifactContext)
6062
.getImportantArtifacts()
6163
.toList()
6264
.stream()
63-
.filter(TopLevelArtifactHelper::shouldDisplay)
65+
.filter(
66+
artifact ->
67+
TopLevelArtifactHelper.shouldDisplay(artifact) || artifact.isSourceArtifact())
6468
.map(Artifact::getExecPathString)
6569
.forEach(this::addResult);
6670
}

src/test/java/com/google/devtools/build/lib/query2/cquery/FilesOutputFormatterCallbackTest.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.LinkedHashSet;
3434
import java.util.List;
3535
import java.util.Set;
36+
import java.util.stream.Collectors;
3637
import org.junit.Before;
3738
import org.junit.Test;
3839

@@ -71,7 +72,7 @@ public final void defineSimpleRule() throws Exception {
7172
" runfiles = ctx.runfiles([runfile]),",
7273
" ),",
7374
" OutputGroupInfo(",
74-
" foobar = [output_group_only],",
75+
" foobar = [output_group_only, ctx.file.explicit_source_dep],",
7576
" ),",
7677
" ]",
7778
"r = rule(",
@@ -134,21 +135,34 @@ private List<String> getOutput(String queryExpression, List<String> outputGroups
134135
@Test
135136
public void basicQuery_defaultOutputGroup() throws Exception {
136137
List<String> output = getOutput("//pkg:all", ImmutableList.of());
138+
var sourceAndGeneratedFiles =
139+
output.stream()
140+
.collect(Collectors.<String>partitioningBy(path -> path.matches("^[^/]*-out/.*")));
141+
assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD", "defs/rules.bzl");
137142
assertContainsExactlyWithBinDirPrefix(
138-
output, "pkg/main_default_file", "pkg/other_default_file");
143+
sourceAndGeneratedFiles.get(true), "pkg/main_default_file", "pkg/other_default_file");
139144
}
140145

141146
@Test
142147
public void basicQuery_defaultAndCustomOutputGroup() throws Exception {
143148
List<String> output = getOutput("//pkg:main", ImmutableList.of("+foobar"));
149+
var sourceAndGeneratedFiles =
150+
output.stream()
151+
.collect(Collectors.<String>partitioningBy(path -> path.matches("^[^/]*-out/.*")));
152+
assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD", "defs/rules.bzl");
144153
assertContainsExactlyWithBinDirPrefix(
145-
output, "pkg/main_default_file", "pkg/main_output_group_only");
154+
sourceAndGeneratedFiles.get(true), "pkg/main_default_file", "pkg/main_output_group_only");
146155
}
147156

148157
@Test
149158
public void basicQuery_customOutputGroupOnly() throws Exception {
150159
List<String> output = getOutput("//pkg:other", ImmutableList.of("foobar"));
151-
assertContainsExactlyWithBinDirPrefix(output, "pkg/other_output_group_only");
160+
var sourceAndGeneratedFiles =
161+
output.stream()
162+
.collect(Collectors.<String>partitioningBy(path -> path.matches("^[^/]*-out/.*")));
163+
assertThat(sourceAndGeneratedFiles.get(false)).containsExactly("pkg/BUILD");
164+
assertContainsExactlyWithBinDirPrefix(
165+
sourceAndGeneratedFiles.get(true), "pkg/other_output_group_only");
152166
}
153167

154168
private void assertContainsExactlyWithBinDirPrefix(

src/test/shell/integration/configured_query_test.sh

+14
Original file line numberDiff line numberDiff line change
@@ -1430,4 +1430,18 @@ EOF
14301430
expect_not_log "QueryException"
14311431
}
14321432

1433+
function test_files_include_source_files() {
1434+
local -r pkg=$FUNCNAME
1435+
mkdir -p $pkg
1436+
cat > $pkg/BUILD <<'EOF'
1437+
filegroup(name="files", srcs=["BUILD"])
1438+
alias(name="alias", actual="single_file")
1439+
EOF
1440+
touch $pkg/single_file
1441+
1442+
bazel cquery --output=files //$pkg:all > output 2>"$TEST_log" || fail "Unexpected failure"
1443+
assert_contains "$pkg/BUILD" output
1444+
assert_contains "$pkg/single_file" output
1445+
}
1446+
14331447
run_suite "${PRODUCT_NAME} configured query tests"

0 commit comments

Comments
 (0)