Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include source files with cquery --output=files #16602

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions site/en/query/cquery.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +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 never contains source files.
[`--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 @@ -17,6 +17,7 @@
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.TopLevelArtifactContext;
import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper;
import com.google.devtools.build.lib.analysis.configuredtargets.InputFileConfiguredTarget;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetAccessor;
import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
Expand Down Expand Up @@ -53,14 +54,16 @@ public void processOutput(Iterable<KeyedConfiguredTarget> partialResult)
throws IOException, InterruptedException {
for (KeyedConfiguredTarget keyedTarget : partialResult) {
ConfiguredTarget target = keyedTarget.getConfiguredTarget();
if (!TopLevelArtifactHelper.shouldConsiderForDisplay(target)) {
if (!TopLevelArtifactHelper.shouldConsiderForDisplay(target)
&& !(target instanceof InputFileConfiguredTarget)) {
continue;
}
TopLevelArtifactHelper.getAllArtifactsToBuild(target, topLevelArtifactContext)
.getImportantArtifacts()
.toList()
.stream()
.filter(TopLevelArtifactHelper::shouldDisplay)
.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 @@ -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;

Expand Down Expand Up @@ -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(",
Expand Down Expand Up @@ -134,21 +135,31 @@ private List<String> getOutput(String queryExpression, List<String> outputGroups
@Test
public void basicQuery_defaultOutputGroup() throws Exception {
List<String> output = getOutput("//pkg:all", ImmutableList.of());
assertContainsExactlyWithBinDirPrefix(
output, "pkg/main_default_file", "pkg/other_default_file");
var sourceAndGeneratedFiles = output.stream()
.collect(Collectors.<String>partitioningBy(path -> path.startsWith("bazel-out/")));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get the equivalent without hard-coding "bazel-out"? I'm not sure if this path is exposed elsewhere as some test constant. Otherwise we could find some output path / artifact and parse its segments.

I'm pretty sure this literal is causing the conflicts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with a regex that should be good enough for this test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. @sgowroji can we re-import the latest version of this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay!

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<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");
assertContainsExactlyWithBinDirPrefix(
output, "pkg/main_default_file", "pkg/main_output_group_only");
sourceAndGeneratedFiles.get(true), "pkg/main_default_file", "pkg/main_output_group_only");
}

@Test
public void basicQuery_customOutputGroupOnly() throws Exception {
List<String> output = getOutput("//pkg:other", ImmutableList.of("foobar"));
assertContainsExactlyWithBinDirPrefix(output, "pkg/other_output_group_only");
var sourceAndGeneratedFiles = output.stream()
.collect(Collectors.<String>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(
Expand Down
13 changes: 13 additions & 0 deletions src/test/shell/integration/configured_query_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1445,5 +1445,18 @@ EOF
expect_log "//peach:harken"
}

function test_files_include_source_files() {
local -r pkg=$FUNCNAME
mkdir -p $pkg
cat > $pkg/BUILD <<'EOF'
filegroup(name="files", srcs=["BUILD"])
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_contains "$pkg/BUILD" output
assert_contains "$pkg/single_file" output
}

run_suite "${PRODUCT_NAME} configured query tests"