Cquery's proto output is now sorted by attribute name.
Now when comparing query outputs, they can be more easily diffed.
Closes
@@ -15,7 +15,7 @@ package com.google.devtools.build.lib.query2.cquery;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
+import com.google.common.collect.Ordering;
import com.google.devtools.build.lib.analysis.AnalysisProtos;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
@@ -38,7 +38,8 @@ import com.google.protobuf.TextFormat;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.io.OutputStream;
-import java.util.Map;
+import java.util.Comparator;
+import java.util.List;
/** Proto output formatter for cquery results. */
class ProtoOutputFormatterCallback extends CqueryThreadsafeCallback {
@@ -174,8 +175,7 @@ class ProtoOutputFormatterCallback extends CqueryThreadsafeCallback {
}
ConfiguredAttributeMapper attributeMapper =
ConfiguredAttributeMapper.of(rule, configConditions);
- Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
- for (Attribute attr : rule.getAttributes()) {
+ for (Attribute attr : sortAttributes(rule.getAttributes())) {
if (!shouldIncludeAttribute(rule, attr)) {
continue;
}
@@ -186,9 +186,12 @@ class ProtoOutputFormatterCallback extends CqueryThreadsafeCallback {
attributeValue,
rule.isAttributeValueExplicitlySpecified(attr),
/*encodeBooleanAndTriStateAsIntegerAndString=*/ true);
- serializedAttributes.put(attr, serializedAttribute);
+ rulePb.addAttribute(serializedAttribute);
}
- rulePb.addAllAttribute(serializedAttributes.values());
}
}
+
+ static List<Attribute> sortAttributes(Iterable<Attribute> attributes) {
+ return Ordering.from(Comparator.comparing(Attribute::getName)).sortedCopy(attributes);
+ }
}
