Skip to content

Commit 273d3f7

Browse files
keertkzhengwei143
andauthored
Fix query --output=proto --order_output=deps not returning targets in 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]>
1 parent 9483baf commit 273d3f7

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/main/java/com/google/devtools/build/lib/query2/query/output/AbstractUnorderedFormatter.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ public void output(
5757
}
5858

5959
protected Iterable<Target> getOrderedTargets(Digraph<Target> result, QueryOptions options) {
60-
Iterable<Node<Target>> orderedResult =
61-
options.orderOutput == OrderOutput.DEPS
62-
? result.getTopologicalOrder()
63-
: result.getTopologicalOrder(new FormatUtils.TargetOrdering());
64-
return Iterables.transform(orderedResult, Node::getLabel);
60+
if (options.orderOutput == OrderOutput.FULL) {
61+
// Get targets in total order, the difference here from topological ordering is the sorting of
62+
// nodes before post-order visitation (which ensures determinism at a time cost).
63+
return Iterables.transform(
64+
result.getTopologicalOrder(new FormatUtils.TargetOrdering()), Node::getLabel);
65+
} else if (options.orderOutput == OrderOutput.DEPS) {
66+
// Get targets in topological order.
67+
return Iterables.transform(result.getTopologicalOrder(), Node::getLabel);
68+
}
69+
return result.getLabels();
6570
}
6671
}

src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
3535
import com.google.devtools.build.lib.events.Event;
3636
import com.google.devtools.build.lib.events.EventHandler;
37-
import com.google.devtools.build.lib.graph.Digraph;
38-
import com.google.devtools.build.lib.graph.Node;
3937
import com.google.devtools.build.lib.packages.AggregatingAttributeMapper;
4038
import com.google.devtools.build.lib.packages.Attribute;
4139
import com.google.devtools.build.lib.packages.AttributeFormatter;
@@ -61,7 +59,6 @@
6159
import com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult;
6260
import com.google.devtools.build.lib.query2.proto.proto2api.Build.SourceFile;
6361
import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver;
64-
import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput;
6562
import com.google.protobuf.CodedOutputStream;
6663
import java.io.IOException;
6764
import java.io.OutputStream;
@@ -169,16 +166,6 @@ public ThreadSafeOutputFormatterCallback<Target> createStreamCallback(
169166
createPostFactoStreamCallback(out, options, env.getMainRepoMapping()));
170167
}
171168

172-
private static Iterable<Target> getSortedLabels(Digraph<Target> result) {
173-
return Iterables.transform(
174-
result.getTopologicalOrder(new FormatUtils.TargetOrdering()), Node::getLabel);
175-
}
176-
177-
@Override
178-
protected Iterable<Target> getOrderedTargets(Digraph<Target> result, QueryOptions options) {
179-
return options.orderOutput == OrderOutput.FULL ? getSortedLabels(result) : result.getLabels();
180-
}
181-
182169
/** Converts a logical {@link Target} object into a {@link Build.Target} protobuffer. */
183170
public Build.Target toTargetProtoBuffer(Target target) throws InterruptedException {
184171
return toTargetProtoBuffer(target, /*extraDataForAttrHash=*/ "");

0 commit comments

Comments
 (0)