Skip to content

Commit

Permalink
Use getDisplayForm in more output formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Oct 19, 2022
1 parent dba373e commit 566113c
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ private static ImmutableMap<String, BuildConfigurationValue> getTransitiveConfig
out,
skyframeExecutor,
accessor,
kct -> getFwdDeps(ImmutableList.of(kct))),
kct -> getFwdDeps(ImmutableList.of(kct)),
getMainRepoMapping()),
new StarlarkOutputFormatterCallback(
eventHandler, cqueryOptions, out, skyframeExecutor, accessor),
new FilesOutputFormatterCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.graph.Digraph;
import com.google.devtools.build.lib.graph.Node;
Expand Down Expand Up @@ -65,12 +66,14 @@ Iterable<KeyedConfiguredTarget> getDirectDeps(KeyedConfiguredTarget target)
};

@Override
public String getLabel(Node<KeyedConfiguredTarget> node) {
public String getLabel(Node<KeyedConfiguredTarget> node,
RepositoryMapping mainRepositoryMapping) {
// Node payloads are ConfiguredTargets. Output node labels are target labels + config
// hashes.
KeyedConfiguredTarget kct = node.getLabel();
return String.format(
"%s (%s)", kct.getLabel(), shortId(getConfiguration(kct.getConfigurationKey())));
"%s (%s)", kct.getLabel().getDisplayForm(mainRepositoryMapping),
shortId(getConfiguration(kct.getConfigurationKey())));
}

@Override
Expand All @@ -79,15 +82,19 @@ public Comparator<KeyedConfiguredTarget> comparator() {
}
};

private final RepositoryMapping mainRepoMapping;

GraphOutputFormatterCallback(
ExtendedEventHandler eventHandler,
CqueryOptions options,
OutputStream out,
SkyframeExecutor skyframeExecutor,
TargetAccessor<KeyedConfiguredTarget> accessor,
DepsRetriever depsRetriever) {
DepsRetriever depsRetriever,
RepositoryMapping mainRepoMapping) {
super(eventHandler, options, out, skyframeExecutor, accessor, /*uniquifyResults=*/ false);
this.depsRetriever = depsRetriever;
this.mainRepoMapping = mainRepoMapping;
}

@Override
Expand Down Expand Up @@ -117,7 +124,8 @@ public void processOutput(Iterable<KeyedConfiguredTarget> partialResult)
// select() conditions don't matter for cquery because cquery operates post-analysis
// phase, when select()s have been resolved and removed from the graph.
/*maxConditionalEdges=*/ 0,
options.graphFactored);
options.graphFactored,
mainRepoMapping);
graphWriter.write(graph, /*conditionalEdges=*/ null, outputStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.devtools.build.lib.query2.query.output;

import com.google.common.hash.HashFunction;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.graph.Digraph;
import com.google.devtools.build.lib.graph.Node;
Expand Down Expand Up @@ -41,9 +42,9 @@ public String getName() {
private final TargetOrdering targetOrdering = new FormatUtils.TargetOrdering();

@Override
public String getLabel(Node<Target> node) {
public String getLabel(Node<Target> node, RepositoryMapping mainRepositoryMapping) {
// Node payloads are Targets. Output node labels are target labels.
return node.getLabel().getLabel().toString();
return node.getLabel().getLabel().getDisplayForm(mainRepositoryMapping);
}

@Override
Expand All @@ -69,7 +70,8 @@ public void output(
sortLabels,
options.graphNodeStringLimit,
options.graphConditionalEdgesLimit,
options.graphFactored);
options.graphFactored,
env.getMainRepoMapping());
graphWriter.write(result, new ConditionalEdges(result), out);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.collect.CollectionUtils;
import com.google.devtools.build.lib.collect.EquivalenceRelation;
import com.google.devtools.build.lib.graph.Digraph;
Expand Down Expand Up @@ -54,7 +55,7 @@ public interface NodeReader<T> {
* <p>This is not the same as a build {@link Label}. This is just the text associated with a
* node in a GraphViz graph.
*/
String getLabel(Node<T> node);
String getLabel(Node<T> node, RepositoryMapping mainRepositoryMapping);

/** Returns a comparator for the build graph nodes that form the payloads of GraphViz nodes. */
Comparator<T> comparator();
Expand All @@ -67,6 +68,7 @@ public interface NodeReader<T> {
private final int maxConditionalEdges;
private final boolean mergeEquivalentNodes;
private final Ordering<Node<T>> nodeComparator;
private final RepositoryMapping mainRepoMapping;

private static final int RESERVED_LABEL_CHARS = "\\n...and 9999999 more items".length();

Expand All @@ -90,13 +92,15 @@ public GraphOutputWriter(
boolean sortLabels,
int maxLabelSize,
int maxConditionalEdges,
boolean mergeEquivalentNodes) {
boolean mergeEquivalentNodes,
RepositoryMapping mainRepoMapping) {
this.nodeReader = nodeReader;
this.lineTerminator = lineTerminator;
this.sortLabels = sortLabels;
this.maxLabelSize = maxLabelSize;
this.maxConditionalEdges = maxConditionalEdges;
this.mergeEquivalentNodes = mergeEquivalentNodes;
this.mainRepoMapping = mainRepoMapping;
nodeComparator = Ordering.from(nodeReader.comparator()).onResultOf(Node::getLabel);
}

Expand All @@ -120,7 +124,7 @@ public void write(
private void outputUnfactored(
Digraph<T> graph, @Nullable ConditionalEdges conditionalEdges, PrintWriter out) {
graph.visitNodesBeforeEdges(
new DotOutputVisitor<T>(out, nodeReader::getLabel) {
new DotOutputVisitor<T>(out, node -> nodeReader.getLabel(node, mainRepoMapping)) {
@Override
public void beginVisit() {
super.beginVisit();
Expand Down Expand Up @@ -180,7 +184,7 @@ private void outputFactored(
StringBuilder buf = new StringBuilder();
int count = 0;
for (Node<T> eqNode : node.getLabel()) {
String labelString = nodeReader.getLabel(eqNode);
String labelString = nodeReader.getLabel(eqNode, mainRepoMapping);
if (!firstItem) {
buf.append("\\n");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void processOutput(Iterable<Target> partialResult) throws IOException {
.append(": ")
.append(target.getTargetKind())
.append(" ")
.append(target.getLabel().getCanonicalForm())
.append(target.getLabel().getDisplayForm(env.getMainRepoMapping()))
.append(lineTerm);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int rank(Node<Set<Node<Target>>> node) {
final String lineTerm = options.getLineTerminator();
PrintStream printStream = new PrintStream(out);
for (RankAndLabel item : output) {
printStream.print(item + lineTerm);
printStream.print(item.toDisplayString(env.getMainRepoMapping()) + lineTerm);
}
flushAndCheckError(printStream);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void output(
if (outputToOrder != null) {
Collections.sort(outputToOrder);
for (RankAndLabel item : outputToOrder) {
printStream.print(item + lineTerm);
printStream.print(item.toDisplayString(env.getMainRepoMapping()) + lineTerm);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.devtools.build.lib.query2.query.output;

import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;

class RankAndLabel implements Comparable<RankAndLabel> {
private final int rank;
Expand All @@ -39,6 +40,10 @@ public int compareTo(RankAndLabel o) {

@Override
public String toString() {
return rank + " " + label.getCanonicalForm();
return toDisplayString(RepositoryMapping.ALWAYS_FALLBACK);
}

public String toDisplayString(RepositoryMapping mainRepoMapping) {
return rank + " " + label.getDisplayForm(mainRepoMapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ java_test(
deps = [
":configured_target_query_helper",
":configured_target_query_test",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/query2",
"//src/main/java/com/google/devtools/build/lib/query2/engine",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.eventbus.EventBus;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.query2.PostAnalysisQueryEnvironment;
Expand Down Expand Up @@ -80,7 +81,8 @@ private List<String> getOutput(String queryExpression) throws Exception {
new PrintStream(output),
getHelper().getSkyframeExecutor(),
env.getAccessor(),
ct -> env.getFwdDeps(ImmutableList.of(ct)));
ct -> env.getFwdDeps(ImmutableList.of(ct)),
RepositoryMapping.ALWAYS_FALLBACK);
env.evaluateQuery(expression, callback);
return Arrays.asList(output.toString().split(System.lineSeparator()));
}
Expand Down

0 comments on commit 566113c

Please sign in to comment.