Skip to content

Commit

Permalink
Fix query --output=proto --order_output=deps not returning targets in…
Browse files Browse the repository at this point in the history
… topological order. (#18870)

Fixes #17087.

RELNOTES[INC]: query --output=proto --order_output=deps now returns targets in topological order (previously there was no ordering).

PiperOrigin-RevId: 501548057
Change-Id: Ied86694fc6c7f9fc2243af1ce43bc778509cbe44

Co-authored-by: Googler <[email protected]>
  • Loading branch information
keertk and zhengwei143 authored Jul 10, 2023
1 parent 9483baf commit 273d3f7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ public void output(
}

protected Iterable<Target> getOrderedTargets(Digraph<Target> result, QueryOptions options) {
Iterable<Node<Target>> orderedResult =
options.orderOutput == OrderOutput.DEPS
? result.getTopologicalOrder()
: result.getTopologicalOrder(new FormatUtils.TargetOrdering());
return Iterables.transform(orderedResult, Node::getLabel);
if (options.orderOutput == OrderOutput.FULL) {
// Get targets in total order, the difference here from topological ordering is the sorting of
// nodes before post-order visitation (which ensures determinism at a time cost).
return Iterables.transform(
result.getTopologicalOrder(new FormatUtils.TargetOrdering()), Node::getLabel);
} else if (options.orderOutput == OrderOutput.DEPS) {
// Get targets in topological order.
return Iterables.transform(result.getTopologicalOrder(), Node::getLabel);
}
return result.getLabels();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.graph.Digraph;
import com.google.devtools.build.lib.graph.Node;
import com.google.devtools.build.lib.packages.AggregatingAttributeMapper;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.AttributeFormatter;
Expand All @@ -61,7 +59,6 @@
import com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult;
import com.google.devtools.build.lib.query2.proto.proto2api.Build.SourceFile;
import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput;
import com.google.protobuf.CodedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -169,16 +166,6 @@ public ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
createPostFactoStreamCallback(out, options, env.getMainRepoMapping()));
}

private static Iterable<Target> getSortedLabels(Digraph<Target> result) {
return Iterables.transform(
result.getTopologicalOrder(new FormatUtils.TargetOrdering()), Node::getLabel);
}

@Override
protected Iterable<Target> getOrderedTargets(Digraph<Target> result, QueryOptions options) {
return options.orderOutput == OrderOutput.FULL ? getSortedLabels(result) : result.getLabels();
}

/** Converts a logical {@link Target} object into a {@link Build.Target} protobuffer. */
public Build.Target toTargetProtoBuffer(Target target) throws InterruptedException {
return toTargetProtoBuffer(target, /*extraDataForAttrHash=*/ "");
Expand Down

0 comments on commit 273d3f7

Please sign in to comment.