diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java index 42507f20d4ee8c..a7d75ee57a4956 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java @@ -416,9 +416,19 @@ public String getCanonicalForm() { * Label.parse*(x.getUnambiguousCanonicalForm(), ...).equals(x)}). */ public String getUnambiguousCanonicalForm() { - return String.format( - "@@%s//%s:%s", - packageIdentifier.getRepository().getName(), packageIdentifier.getPackageFragment(), name); + return packageIdentifier.getUnambiguousCanonicalForm() + ":" + name; + } + + /** + * Returns a label string that is suitable for display, i.e., it resolves to this label when + * parsed in the context of the main repository and has a repository part that is as simple as + * possible. + * + * @param mainRepositoryMapping the {@link RepositoryMapping} of the main repository + * @return analogous to {@link PackageIdentifier#getDisplayForm(RepositoryMapping)} + */ + public String getDisplayForm(RepositoryMapping mainRepositoryMapping) { + return packageIdentifier.getDisplayForm(mainRepositoryMapping) + ":" + name; } /** Return the name of the repository label refers to without the leading `at` symbol. */ diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java index 1d623bb19c1fd7..24ff1197e9e74e 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java @@ -39,6 +39,7 @@ import java.util.Iterator; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.regex.Pattern; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; diff --git a/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java index b240fc9a0d687a..36e25748b37723 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java @@ -28,6 +28,7 @@ import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.cmdline.TargetParsingException; import com.google.devtools.build.lib.cmdline.TargetPattern; import com.google.devtools.build.lib.collect.compacthashset.CompactHashSet; @@ -251,6 +252,11 @@ protected TargetPattern getPattern(String pattern) throws TargetParsingException return mainRepoTargetParser.parse(pattern); } + @Override + public RepositoryMapping getMainRepoMapping() { + return mainRepoTargetParser.getRepoMapping(); + } + public ThreadSafeMutableSet getFwdDeps(Iterable targets) throws InterruptedException { Map targetsByKey = Maps.newHashMapWithExpectedSize(Iterables.size(targets)); for (T target : targets) { diff --git a/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java index 70fdf7ea556687..744d0357b278c8 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/SkyQueryEnvironment.java @@ -42,6 +42,7 @@ import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.PackageIdentifier; import com.google.devtools.build.lib.cmdline.ParallelVisitor.VisitTaskStatusCallback; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.cmdline.SignedTargetPattern; import com.google.devtools.build.lib.cmdline.TargetParsingException; import com.google.devtools.build.lib.cmdline.TargetPattern; @@ -958,6 +959,12 @@ public TargetAccessor getAccessor() { return accessor; } + @Override + @ThreadSafe + public RepositoryMapping getMainRepoMapping() { + return mainRepoTargetParser.getRepoMapping(); + } + @ThreadSafe private Package getPackage(PackageIdentifier packageIdentifier) throws InterruptedException, QueryException, NoSuchPackageException { diff --git a/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryEnvironment.java index b324910870bbec..f75fc8ddb5429f 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryEnvironment.java @@ -170,7 +170,8 @@ public ConfiguredTargetValueAccessor getAccessor() { StreamedOutputHandler.OutputType.JSON, actionFilters), new ActionGraphTextOutputFormatterCallback( - eventHandler, aqueryOptions, out, skyframeExecutor, accessor, actionFilters), + eventHandler, aqueryOptions, out, skyframeExecutor, accessor, actionFilters, + getMainRepoMapping()), new ActionGraphSummaryOutputFormatterCallback( eventHandler, aqueryOptions, out, skyframeExecutor, accessor, actionFilters)); } diff --git a/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphTextOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphTextOutputFormatterCallback.java index e9d8c4c84847c2..b98ecc31dab930 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphTextOutputFormatterCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphTextOutputFormatterCallback.java @@ -38,6 +38,7 @@ import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction; import com.google.devtools.build.lib.buildeventstream.BuildEvent; import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.events.ExtendedEventHandler; import com.google.devtools.build.lib.packages.AspectDescriptor; import com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetAccessor; @@ -62,6 +63,7 @@ class ActionGraphTextOutputFormatterCallback extends AqueryThreadsafeCallback { private final ActionKeyContext actionKeyContext = new ActionKeyContext(); private final AqueryActionFilter actionFilters; + private final RepositoryMapping mainRepoMapping; private Map paramFileNameToContentMap; ActionGraphTextOutputFormatterCallback( @@ -70,9 +72,11 @@ class ActionGraphTextOutputFormatterCallback extends AqueryThreadsafeCallback { OutputStream out, SkyframeExecutor skyframeExecutor, TargetAccessor accessor, - AqueryActionFilter actionFilters) { + AqueryActionFilter actionFilters, + RepositoryMapping mainRepoMapping) { super(eventHandler, options, out, skyframeExecutor, accessor); this.actionFilters = actionFilters; + this.mainRepoMapping = mainRepoMapping; } @Override @@ -145,7 +149,7 @@ private void writeAction(ActionAnalysisMetadata action, PrintStream printStream) stringBuilder .append(" Target: ") - .append(actionOwner.getLabel()) + .append(actionOwner.getLabel().getDisplayForm(mainRepoMapping)) .append('\n') .append(" Configuration: ") .append(configProto.getMnemonic()) @@ -153,7 +157,7 @@ private void writeAction(ActionAnalysisMetadata action, PrintStream printStream) if (actionOwner.getExecutionPlatform() != null) { stringBuilder .append(" Execution platform: ") - .append(actionOwner.getExecutionPlatform().label().toString()) + .append(actionOwner.getExecutionPlatform().label().getDisplayForm(mainRepoMapping)) .append("\n"); } diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java index 649f9ca54176fd..41ca55ef63b445 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java @@ -30,6 +30,7 @@ import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.cmdline.TargetParsingException; import com.google.devtools.build.lib.cmdline.TargetPattern; import com.google.devtools.build.lib.events.ExtendedEventHandler; @@ -205,9 +206,11 @@ private static ImmutableMap getTransitiveConfig cqueryOptions.aspectDeps.createResolver(packageManager, eventHandler); return ImmutableList.of( new LabelAndConfigurationOutputFormatterCallback( - eventHandler, cqueryOptions, out, skyframeExecutor, accessor, true), + eventHandler, cqueryOptions, out, skyframeExecutor, accessor, true, + getMainRepoMapping()), new LabelAndConfigurationOutputFormatterCallback( - eventHandler, cqueryOptions, out, skyframeExecutor, accessor, false), + eventHandler, cqueryOptions, out, skyframeExecutor, accessor, false, + getMainRepoMapping()), new TransitionsOutputFormatterCallback( eventHandler, cqueryOptions, @@ -215,7 +218,8 @@ private static ImmutableMap getTransitiveConfig skyframeExecutor, accessor, hostConfiguration, - trimmingTransitionFactory), + trimmingTransitionFactory, + getMainRepoMapping()), new ProtoOutputFormatterCallback( eventHandler, cqueryOptions, @@ -251,7 +255,8 @@ private static ImmutableMap getTransitiveConfig out, skyframeExecutor, accessor, - kct -> getFwdDeps(ImmutableList.of(kct))), + kct -> getFwdDeps(ImmutableList.of(kct)), + getMainRepoMapping()), new StarlarkOutputFormatterCallback( eventHandler, cqueryOptions, out, skyframeExecutor, accessor), new FilesOutputFormatterCallback( diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallback.java index 7630ee31036905..1cb2c5373f97de 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallback.java @@ -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; @@ -65,12 +66,14 @@ Iterable getDirectDeps(KeyedConfiguredTarget target) }; @Override - public String getLabel(Node node) { + public String getLabel(Node 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 @@ -79,15 +82,19 @@ public Comparator comparator() { } }; + private final RepositoryMapping mainRepoMapping; + GraphOutputFormatterCallback( ExtendedEventHandler eventHandler, CqueryOptions options, OutputStream out, SkyframeExecutor skyframeExecutor, TargetAccessor accessor, - DepsRetriever depsRetriever) { + DepsRetriever depsRetriever, + RepositoryMapping mainRepoMapping) { super(eventHandler, options, out, skyframeExecutor, accessor, /*uniquifyResults=*/ false); this.depsRetriever = depsRetriever; + this.mainRepoMapping = mainRepoMapping; } @Override @@ -117,7 +124,8 @@ public void processOutput(Iterable 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); } } diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/LabelAndConfigurationOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/LabelAndConfigurationOutputFormatterCallback.java index cffca9a3f12c05..73bfd9cfc0bd67 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/LabelAndConfigurationOutputFormatterCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/LabelAndConfigurationOutputFormatterCallback.java @@ -18,6 +18,7 @@ import com.google.devtools.build.lib.analysis.RequiredConfigFragmentsProvider; import com.google.devtools.build.lib.analysis.config.CoreOptions.IncludeConfigFragmentsEnum; 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.packages.Target; import com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetAccessor; @@ -28,6 +29,7 @@ /** Default Output callback for cquery. Prints a label and configuration pair per result. */ public class LabelAndConfigurationOutputFormatterCallback extends CqueryThreadsafeCallback { private final boolean showKind; + private final RepositoryMapping mainRepoMapping; LabelAndConfigurationOutputFormatterCallback( ExtendedEventHandler eventHandler, @@ -35,9 +37,11 @@ public class LabelAndConfigurationOutputFormatterCallback extends CqueryThreadsa OutputStream out, SkyframeExecutor skyframeExecutor, TargetAccessor accessor, - boolean showKind) { + boolean showKind, + RepositoryMapping mainRepoMapping) { super(eventHandler, options, out, skyframeExecutor, accessor, /*uniquifyResults=*/ false); this.showKind = showKind; + this.mainRepoMapping = mainRepoMapping; } @Override @@ -55,7 +59,7 @@ public void processOutput(Iterable partialResult) { } output = output - .append(keyedConfiguredTarget.getLabel()) + .append(keyedConfiguredTarget.getLabel().getDisplayForm(mainRepoMapping)) .append(" (") .append(shortId(getConfiguration(keyedConfiguredTarget.getConfigurationKey()))) .append(")"); diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/TransitionsOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/TransitionsOutputFormatterCallback.java index 65034298b6c6cf..73cd4486774eac 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/TransitionsOutputFormatterCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/TransitionsOutputFormatterCallback.java @@ -27,6 +27,7 @@ import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.ExtendedEventHandler; import com.google.devtools.build.lib.packages.RuleTransitionData; @@ -47,7 +48,9 @@ class TransitionsOutputFormatterCallback extends CqueryThreadsafeCallback { protected final BuildConfigurationValue hostConfiguration; private final HashMap partialResultMap; - @Nullable private final TransitionFactory trimmingTransitionFactory; + @Nullable + private final TransitionFactory trimmingTransitionFactory; + private final RepositoryMapping mainRepoMapping; @Override public String getName() { @@ -65,11 +68,13 @@ public String getName() { SkyframeExecutor skyframeExecutor, TargetAccessor accessor, BuildConfigurationValue hostConfiguration, - @Nullable TransitionFactory trimmingTransitionFactory) { + @Nullable TransitionFactory trimmingTransitionFactory, + RepositoryMapping mainRepoMapping) { super(eventHandler, options, out, skyframeExecutor, accessor, /*uniquifyResults=*/ false); this.hostConfiguration = hostConfiguration; this.trimmingTransitionFactory = trimmingTransitionFactory; this.partialResultMap = Maps.newHashMap(); + this.mainRepoMapping = mainRepoMapping; } @Override @@ -91,8 +96,9 @@ public void processOutput(Iterable partialResult) addResult( getRuleClassTransition(keyedConfiguredTarget.getConfiguredTarget(), target) + String.format( - "%s (%s)", - keyedConfiguredTarget.getConfiguredTarget().getOriginalLabel(), shortId(config))); + "%s (%s)", + keyedConfiguredTarget.getConfiguredTarget().getOriginalLabel() + .getDisplayForm(mainRepoMapping), shortId(config))); KnownTargetsDependencyResolver knownTargetsDependencyResolver = new KnownTargetsDependencyResolver(partialResultMap); ImmutableSet dependencies; @@ -117,7 +123,7 @@ public void processOutput(Iterable partialResult) " " .concat(dep.attributeName()) .concat("#") - .concat(dep.label().toString()) + .concat(dep.label().getDisplayForm(mainRepoMapping)) .concat("#") .concat(dep.transitionName()) .concat(" -> ") diff --git a/src/main/java/com/google/devtools/build/lib/query2/engine/QueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/engine/QueryEnvironment.java index 4368c25549ae9a..3aa9ca4e48d5a1 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/engine/QueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/engine/QueryEnvironment.java @@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.util.concurrent.ListenableFuture; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.util.DetailedExitCode; @@ -536,6 +537,13 @@ ThreadSafeMutableSet getBuildFiles( */ TargetAccessor getAccessor(); + /** + * Returns the {@link RepositoryMapping} of the main repository so that output formatters can + * resolve canonical repository names in labels back to the more readable local names used by the + * main repository. + */ + RepositoryMapping getMainRepoMapping(); + /** * Whether the given setting is enabled. The code should default to return {@code false} for all * unknown settings. The enum is used rather than a method for each setting so that adding more diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/BlazeQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/query/BlazeQueryEnvironment.java index 295d76e40ef640..16165de1af3ab2 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/BlazeQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/BlazeQueryEnvironment.java @@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.PackageIdentifier; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.cmdline.TargetParsingException; import com.google.devtools.build.lib.cmdline.TargetPattern; import com.google.devtools.build.lib.cmdline.TargetPattern.Parser; @@ -500,6 +501,11 @@ public TargetAccessor getAccessor() { return accessor; } + @Override + public RepositoryMapping getMainRepoMapping() { + return mainRepoTargetParser.getRepoMapping(); + } + /** Given a set of target nodes, returns the targets. */ private ThreadSafeMutableSet getTargetsFromNodes(Iterable> input) { ThreadSafeMutableSet result = createThreadSafeMutableSet(); diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/GraphlessBlazeQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/query/GraphlessBlazeQueryEnvironment.java index f99657e134dfa8..a83a8aa6182a74 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/GraphlessBlazeQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/GraphlessBlazeQueryEnvironment.java @@ -17,6 +17,7 @@ import com.google.common.collect.Sets; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.PackageIdentifier; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.cmdline.TargetParsingException; import com.google.devtools.build.lib.cmdline.TargetPattern; import com.google.devtools.build.lib.cmdline.TargetPattern.Parser; @@ -507,4 +508,9 @@ protected void preloadOrThrow(QueryExpression caller, Collection pattern public TargetAccessor getAccessor() { return accessor; } + + @Override + public RepositoryMapping getMainRepoMapping() { + return mainRepoTargetParser.getRepoMapping(); + } } diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/AbstractUnorderedFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/AbstractUnorderedFormatter.java index 993a9453c69132..6bf272325bcc05 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/AbstractUnorderedFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/AbstractUnorderedFormatter.java @@ -22,6 +22,7 @@ import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.query2.common.CommonQueryOptions; import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment; import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver; import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput; import java.io.IOException; @@ -40,6 +41,7 @@ public void setEventHandler(@Nullable EventHandler eventHandler) {} @Override public void output( + QueryEnvironment env, QueryOptions options, Digraph result, OutputStream out, @@ -50,7 +52,7 @@ public void output( setOptions(options, aspectResolver, hashFunction); setEventHandler(eventHandler); OutputFormatterCallback.processAllTargets( - createPostFactoStreamCallback(out, options), getOrderedTargets(result, options)); + createPostFactoStreamCallback(out, options, env), getOrderedTargets(result, options)); } protected Iterable getOrderedTargets(Digraph result, QueryOptions options) { diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/BuildOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/BuildOutputFormatter.java index bb956481d60f38..bff92419f9bbd4 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/BuildOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/BuildOutputFormatter.java @@ -221,7 +221,7 @@ private String reconstructSelect(Rule rule, Attribute attr) { /** Query's implementation. */ @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, final QueryOptions options) { + OutputStream out, final QueryOptions options, QueryEnvironment env) { return new BuildOutputFormatterCallback(out, options.getLineTerminator()); } @@ -229,7 +229,7 @@ public OutputFormatterCallback createPostFactoStreamCallback( public ThreadSafeOutputFormatterCallback createStreamCallback( OutputStream out, QueryOptions options, QueryEnvironment env) { return new SynchronizedDelegatingOutputFormatterCallback<>( - createPostFactoStreamCallback(out, options)); + createPostFactoStreamCallback(out, options, env)); } private static class BuildOutputFormatterCallback extends TextOutputFormatterCallback { diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputFormatter.java index d27b2375c25891..024f106cbd3c73 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputFormatter.java @@ -14,10 +14,12 @@ 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; import com.google.devtools.build.lib.packages.Target; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment; import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver; import com.google.devtools.build.lib.query2.query.output.FormatUtils.TargetOrdering; import com.google.devtools.build.lib.query2.query.output.GraphOutputWriter.NodeReader; @@ -40,9 +42,9 @@ public String getName() { private final TargetOrdering targetOrdering = new FormatUtils.TargetOrdering(); @Override - public String getLabel(Node node) { + public String getLabel(Node 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 @@ -53,6 +55,7 @@ public Comparator comparator() { @Override public void output( + QueryEnvironment env, QueryOptions options, Digraph result, OutputStream out, @@ -67,7 +70,8 @@ public void output( sortLabels, options.graphNodeStringLimit, options.graphConditionalEdgesLimit, - options.graphFactored); + options.graphFactored, + env.getMainRepoMapping()); graphWriter.write(result, new ConditionalEdges(result), out); } } diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputWriter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputWriter.java index 5ab5514ef05e5b..f80336290fdbbf 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputWriter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/GraphOutputWriter.java @@ -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; @@ -54,7 +55,7 @@ public interface NodeReader { *

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 node); + String getLabel(Node node, RepositoryMapping mainRepositoryMapping); /** Returns a comparator for the build graph nodes that form the payloads of GraphViz nodes. */ Comparator comparator(); @@ -67,6 +68,7 @@ public interface NodeReader { private final int maxConditionalEdges; private final boolean mergeEquivalentNodes; private final Ordering> nodeComparator; + private final RepositoryMapping mainRepoMapping; private static final int RESERVED_LABEL_CHARS = "\\n...and 9999999 more items".length(); @@ -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); } @@ -120,7 +124,7 @@ public void write( private void outputUnfactored( Digraph graph, @Nullable ConditionalEdges conditionalEdges, PrintWriter out) { graph.visitNodesBeforeEdges( - new DotOutputVisitor(out, nodeReader::getLabel) { + new DotOutputVisitor(out, node -> nodeReader.getLabel(node, mainRepoMapping)) { @Override public void beginVisit() { super.beginVisit(); @@ -180,7 +184,7 @@ private void outputFactored( StringBuilder buf = new StringBuilder(); int count = 0; for (Node eqNode : node.getLabel()) { - String labelString = nodeReader.getLabel(eqNode); + String labelString = nodeReader.getLabel(eqNode, mainRepoMapping); if (!firstItem) { buf.append("\\n"); diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/LabelOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/LabelOutputFormatter.java index 1f7ee14e7a6e85..22acd99089ef7e 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/LabelOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/LabelOutputFormatter.java @@ -42,7 +42,7 @@ public String getName() { @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, final QueryOptions options) { + OutputStream out, final QueryOptions options, QueryEnvironment env) { return new TextOutputFormatterCallback(out) { @Override public void processOutput(Iterable partialResult) throws IOException { @@ -53,7 +53,7 @@ public void processOutput(Iterable partialResult) throws IOException { writer.append(' '); } Label label = target.getLabel(); - writer.append(label.getCanonicalForm()).append(lineTerm); + writer.append(label.getDisplayForm(env.getMainRepoMapping())).append(lineTerm); } } }; @@ -63,6 +63,6 @@ public void processOutput(Iterable partialResult) throws IOException { public ThreadSafeOutputFormatterCallback createStreamCallback( OutputStream out, QueryOptions options, QueryEnvironment env) { return new SynchronizedDelegatingOutputFormatterCallback<>( - createPostFactoStreamCallback(out, options)); + createPostFactoStreamCallback(out, options, env)); } } \ No newline at end of file diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/LocationOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/LocationOutputFormatter.java index c120b178842de6..b6cb2487e36969 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/LocationOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/LocationOutputFormatter.java @@ -75,7 +75,7 @@ public void verifyCompatible(QueryEnvironment env, QueryExpression expr) @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, final QueryOptions options) { + OutputStream out, final QueryOptions options, QueryEnvironment env) { return new TextOutputFormatterCallback(out) { @Override @@ -87,7 +87,7 @@ public void processOutput(Iterable partialResult) throws IOException { .append(": ") .append(target.getTargetKind()) .append(" ") - .append(target.getLabel().getCanonicalForm()) + .append(target.getLabel().getDisplayForm(env.getMainRepoMapping())) .append(lineTerm); } } @@ -98,6 +98,6 @@ public void processOutput(Iterable partialResult) throws IOException { public ThreadSafeOutputFormatterCallback createStreamCallback( OutputStream out, QueryOptions options, QueryEnvironment env) { return new SynchronizedDelegatingOutputFormatterCallback<>( - createPostFactoStreamCallback(out, options)); + createPostFactoStreamCallback(out, options, env)); } } diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/MaxrankOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/MaxrankOutputFormatter.java index ecf87b119340ac..e808297f8093a2 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/MaxrankOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/MaxrankOutputFormatter.java @@ -21,6 +21,7 @@ import com.google.devtools.build.lib.graph.Digraph; import com.google.devtools.build.lib.graph.Node; import com.google.devtools.build.lib.packages.Target; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment; import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver; import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput; import java.io.IOException; @@ -53,6 +54,7 @@ public String getName() { @Override public void output( + QueryEnvironment env, QueryOptions options, Digraph result, OutputStream out, @@ -102,7 +104,7 @@ int rank(Node>> 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); } diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/MinrankOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/MinrankOutputFormatter.java index 2159dc6ff1df78..575c15b45325a2 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/MinrankOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/MinrankOutputFormatter.java @@ -20,6 +20,7 @@ import com.google.devtools.build.lib.graph.Digraph; import com.google.devtools.build.lib.graph.Node; import com.google.devtools.build.lib.packages.Target; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment; import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver; import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput; import java.io.IOException; @@ -65,6 +66,7 @@ private static void outputToStreamOrSave( @Override public void output( + QueryEnvironment env, QueryOptions options, Digraph result, OutputStream out, @@ -108,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); } } diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatter.java index 480e1b09250944..f6558bec0e3625 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/OutputFormatter.java @@ -53,6 +53,7 @@ public void verifyCompatible(QueryEnvironment env, QueryExpression expr) * by the QueryEnvironment), and print it to "out". */ public abstract void output( + QueryEnvironment env, QueryOptions options, Digraph result, OutputStream out, diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/PackageOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/PackageOutputFormatter.java index 12170d2c13a4a0..17840194c7ebd5 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/PackageOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/PackageOutputFormatter.java @@ -25,8 +25,8 @@ import java.util.Set; /** - * An output formatter that prints the names of the packages of the target - * set, in lexicographical order without duplicates. + * An output formatter that prints the names of the packages of the target set, in lexicographical + * order without duplicates. */ class PackageOutputFormatter extends AbstractUnorderedFormatter { @@ -37,7 +37,7 @@ public String getName() { @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, final QueryOptions options) { + OutputStream out, final QueryOptions options, QueryEnvironment env) { return new TextOutputFormatterCallback(out) { private final Set packageNames = Sets.newTreeSet(); @@ -45,7 +45,8 @@ public OutputFormatterCallback createPostFactoStreamCallback( public void processOutput(Iterable partialResult) { for (Target target : partialResult) { - packageNames.add(target.getLabel().getPackageIdentifier().toString()); + packageNames.add( + target.getLabel().getPackageIdentifier().getDisplayForm(env.getMainRepoMapping())); } } @@ -66,6 +67,6 @@ public void close(boolean failFast) throws IOException { public ThreadSafeOutputFormatterCallback createStreamCallback( OutputStream out, QueryOptions options, QueryEnvironment env) { return new SynchronizedDelegatingOutputFormatterCallback<>( - createPostFactoStreamCallback(out, options)); + createPostFactoStreamCallback(out, options, env)); } } \ No newline at end of file diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java index 3c1eb27e152b7a..e5e405503d239d 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/ProtoOutputFormatter.java @@ -158,21 +158,15 @@ private static Predicate newAttributePredicate(List outputAttrib @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, QueryOptions options) { + OutputStream out, QueryOptions options, QueryEnvironment env) { return new StreamedQueryResultFormatter(out); } @Override public ThreadSafeOutputFormatterCallback createStreamCallback( OutputStream out, QueryOptions options, QueryEnvironment env) { - return createStreamCallback(out, options); - } - - @VisibleForTesting - public ThreadSafeOutputFormatterCallback createStreamCallback( - OutputStream out, QueryOptions options) { return new SynchronizedDelegatingOutputFormatterCallback<>( - createPostFactoStreamCallback(out, options)); + createPostFactoStreamCallback(out, options, env)); } private static Iterable getSortedLabels(Digraph result) { diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOutputUtils.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOutputUtils.java index 671f199f1b32a6..c04e30822fae13 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOutputUtils.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/QueryOutputUtils.java @@ -21,6 +21,7 @@ import com.google.devtools.build.lib.profiler.SilentCloseable; import com.google.devtools.build.lib.query2.engine.DigraphQueryEvalResult; import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment; import com.google.devtools.build.lib.query2.engine.QueryEvalResult; import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver; import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput; @@ -52,6 +53,7 @@ public static boolean shouldStreamResults(QueryOptions queryOptions, OutputForma } public static void output( + QueryEnvironment env, QueryOptions queryOptions, QueryEvalResult result, Set targetsResult, @@ -71,7 +73,7 @@ public static void output( streamedFormatter.setOptions(queryOptions, aspectResolver, hashFunction); streamedFormatter.setEventHandler(eventHandler); OutputFormatterCallback.processAllTargets( - streamedFormatter.createPostFactoStreamCallback(outputStream, queryOptions), + streamedFormatter.createPostFactoStreamCallback(outputStream, queryOptions, env), targetsResult); } else { @SuppressWarnings("unchecked") @@ -85,7 +87,7 @@ public static void output( try (SilentCloseable closeable = Profiler.instance().profile("formatter.output")) { formatter.output( - queryOptions, subgraph, outputStream, aspectResolver, eventHandler, hashFunction); + env, queryOptions, subgraph, outputStream, aspectResolver, eventHandler, hashFunction); } } } diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/RankAndLabel.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/RankAndLabel.java index 516b930926e723..3b957829231527 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/RankAndLabel.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/RankAndLabel.java @@ -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 { private final int rank; @@ -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); } } \ No newline at end of file diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedFormatter.java index feb38541728f71..7d4818a28ecb01 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedFormatter.java @@ -62,5 +62,5 @@ ThreadSafeOutputFormatterCallback createStreamCallback( * already-computed result of a query. */ OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, QueryOptions options); + OutputStream out, QueryOptions options, QueryEnvironment env); } \ No newline at end of file diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java index f3550fcb435fd7..26126ad2aff102 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/StreamedProtoOutputFormatter.java @@ -15,6 +15,7 @@ import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; +import com.google.devtools.build.lib.query2.engine.QueryEnvironment; import java.io.IOException; import java.io.OutputStream; @@ -31,7 +32,7 @@ public String getName() { @Override public OutputFormatterCallback createPostFactoStreamCallback( - final OutputStream out, final QueryOptions options) { + final OutputStream out, final QueryOptions options, QueryEnvironment env) { return new OutputFormatterCallback() { @Override public void processOutput(Iterable partialResult) diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java index 1fff552a1a61ce..aac57c223dcce7 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/output/XmlOutputFormatter.java @@ -75,7 +75,7 @@ public String getName() { public ThreadSafeOutputFormatterCallback createStreamCallback( OutputStream out, QueryOptions options, QueryEnvironment env) { return new SynchronizedDelegatingOutputFormatterCallback<>( - createPostFactoStreamCallback(out, options)); + createPostFactoStreamCallback(out, options, env)); } @Override @@ -94,7 +94,7 @@ public void setOptions( @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, QueryOptions options) { + OutputStream out, QueryOptions options, QueryEnvironment env) { return new OutputFormatterCallback() { private Document doc; diff --git a/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java b/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java index 8efac5ec987e3a..9f049930ed4607 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java +++ b/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java @@ -268,6 +268,7 @@ private static GenQueryResult doQuery( OutputFormatter formatter; AggregateAllOutputFormatterCallback targets; boolean graphlessQuery; + AbstractBlazeQueryEnvironment queryEnvironment; try { Set settings = queryOptions.toSettings(); @@ -303,7 +304,7 @@ private static GenQueryResult doQuery( RepositoryMappingResolutionException.class); Preconditions.checkNotNull(repositoryMappingValue); - AbstractBlazeQueryEnvironment queryEnvironment = + queryEnvironment = QUERY_ENVIRONMENT_FACTORY.create( /* queryTransitivePackagePreloader= */ null, /* graphFactory= */ null, @@ -360,14 +361,14 @@ private static GenQueryResult doQuery( Set result = targets.getResult(); try { QueryOutputUtils.output( + queryEnvironment, queryOptions, queryResult, result, formatter, outputStream, queryOptions.aspectDeps.createResolver(packageProvider, getEventHandler(ruleContext)), - getEventHandler(ruleContext), - hashFunction); + getEventHandler(ruleContext), hashFunction); outputStream.close(); } catch (ClosedByInterruptException e) { throw new InterruptedException(e.getMessage()); diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java index ff882e4dba1ee2..c115ea7ed9610d 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/QueryCommand.java @@ -178,14 +178,14 @@ protected Either doQuery( Set targets = ((AggregateAllOutputFormatterCallback) callback).getResult(); QueryOutputUtils.output( + queryEnv, queryOptions, result, targets, formatter, out, queryOptions.aspectDeps.createResolver(env.getPackageManager(), env.getReporter()), - env.getReporter(), - hashFunction); + env.getReporter(), hashFunction); } catch (ClosedByInterruptException | InterruptedException e) { return reportAndCreateInterruptedResult(env); } catch (IOException e) { diff --git a/src/test/java/com/google/devtools/build/lib/query2/cquery/BUILD b/src/test/java/com/google/devtools/build/lib/query2/cquery/BUILD index 27afb36bc23d09..f89a548b4f0d8f 100644 --- a/src/test/java/com/google/devtools/build/lib/query2/cquery/BUILD +++ b/src/test/java/com/google/devtools/build/lib/query2/cquery/BUILD @@ -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", @@ -150,6 +151,7 @@ java_test( "//src/main/java/com/google/devtools/build/lib/analysis:config/transition_factories", "//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/no_transition", "//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/transition_factory", + "//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/packages", "//src/main/java/com/google/devtools/build/lib/query2", diff --git a/src/test/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallbackTest.java b/src/test/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallbackTest.java index f10fc441d284fa..e90a83170e593e 100644 --- a/src/test/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallbackTest.java +++ b/src/test/java/com/google/devtools/build/lib/query2/cquery/GraphOutputFormatterCallbackTest.java @@ -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; @@ -80,7 +81,8 @@ private List 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())); } diff --git a/src/test/java/com/google/devtools/build/lib/query2/cquery/TransitionsOutputFormatterTest.java b/src/test/java/com/google/devtools/build/lib/query2/cquery/TransitionsOutputFormatterTest.java index fd5772ef3a420b..802073ecf116d4 100644 --- a/src/test/java/com/google/devtools/build/lib/query2/cquery/TransitionsOutputFormatterTest.java +++ b/src/test/java/com/google/devtools/build/lib/query2/cquery/TransitionsOutputFormatterTest.java @@ -24,6 +24,7 @@ import com.google.devtools.build.lib.analysis.config.transitions.NoTransition; import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory; import com.google.devtools.build.lib.analysis.util.MockRule; +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.packages.RuleTransitionData; @@ -245,7 +246,8 @@ private List getOutput(String queryExpression, CqueryOptions.Transitions getHelper().getSkyframeExecutor(), env.getAccessor(), env.getHostConfiguration(), - trimmingTransitionFactory); + trimmingTransitionFactory, + RepositoryMapping.ALWAYS_FALLBACK); env.evaluateQuery(env.transformParsedQuery(QueryParser.parse(queryExpression, env)), callback); return callback.getResult(); } diff --git a/src/test/py/bazel/bzlmod/bzlmod_query_test.py b/src/test/py/bazel/bzlmod/bzlmod_query_test.py index bef662c3e3347b..c8fbbceeca1d77 100644 --- a/src/test/py/bazel/bzlmod/bzlmod_query_test.py +++ b/src/test/py/bazel/bzlmod/bzlmod_query_test.py @@ -30,9 +30,11 @@ def setUp(self): self.registries_work_dir = tempfile.mkdtemp(dir=self._test_cwd) self.main_registry = BazelRegistry( os.path.join(self.registries_work_dir, 'main')) - self.main_registry.createCcModule('aaa', '1.0') \ + self.main_registry.createCcModule('aaa', '1.0', {'ccc': '1.2'}) \ .createCcModule('aaa', '1.1') \ - .createCcModule('bbb', '1.0', {'aaa': '1.0'}, {'aaa': 'com_foo_bar_aaa'}) + .createCcModule('bbb', '1.0', {'aaa': '1.0'}, {'aaa': 'com_foo_bar_aaa'}) \ + .createCcModule('ccc', '1.2') + self.ScratchFile( '.bazelrc', [ @@ -58,7 +60,29 @@ def testQueryModuleRepoTargetsBelow(self): ]) _, stdout, _ = self.RunBazel(['query', '@my_repo//...'], allow_failure=False) - self.assertListEqual(['@aaa~1.0//:lib_aaa'], stdout) + self.assertListEqual(['@my_repo//:lib_aaa'], stdout) + + def testQueryModuleRepoTransitiveDeps(self): + self.ScratchFile('MODULE.bazel', [ + 'bazel_dep(name = "aaa", version = "1.0", repo_name = "my_repo")', + ]) + self.ScratchFile('BUILD', [ + 'cc_binary(', + ' name = "main",', + ' srcs = ["main.cc"],', + ' deps = ["@my_repo//:lib_aaa"],', + ')', + ]) + _, stdout, _ = self.RunBazel( + [ + 'query', + 'kind("cc_.* rule", deps(//:main))', + '--noimplicit_deps', + '--notool_deps', + ], + allow_failure=False) + self.assertListEqual( + ['//:main', '@my_repo//:lib_aaa', '@@ccc~1.2//:lib_ccc'], stdout) def testAqueryModuleRepoTargetsBelow(self): self.ScratchFile('MODULE.bazel', [ @@ -67,7 +91,33 @@ def testAqueryModuleRepoTargetsBelow(self): ]) _, stdout, _ = self.RunBazel(['aquery', '@my_repo//...'], allow_failure=False) + # This label is stringified into a "purpose" in some action before it + # reaches aquery code, so can't decanonicalize it. self.assertEqual(stdout[0], 'cc_library-compile for @aaa~1.0//:lib_aaa') + self.assertIn('Target: @my_repo//:lib_aaa', stdout) + + def testAqueryModuleRepoTransitiveDeps(self): + self.ScratchFile('MODULE.bazel', [ + 'bazel_dep(name = "aaa", version = "1.0", repo_name = "my_repo")', + ]) + self.ScratchFile('BUILD', [ + 'cc_binary(', + ' name = "main",', + ' srcs = ["main.cc"],', + ' deps = ["@my_repo//:lib_aaa"],', + ')', + ]) + _, stdout, _ = self.RunBazel( + [ + 'aquery', + 'kind("cc_.* rule", deps(//:main))', + '--noimplicit_deps', + '--notool_deps', + ], + allow_failure=False) + self.assertIn('Target: //:main', stdout) + self.assertIn('Target: @my_repo//:lib_aaa', stdout) + self.assertIn('Target: @@ccc~1.2//:lib_ccc', stdout) def testCqueryModuleRepoTargetsBelow(self): self.ScratchFile('MODULE.bazel', [ @@ -76,7 +126,31 @@ def testCqueryModuleRepoTargetsBelow(self): ]) _, stdout, _ = self.RunBazel(['cquery', '@my_repo//...'], allow_failure=False) - self.assertRegex(stdout[0], r'@aaa~1.0//:lib_aaa \([\w\d]+\)') + self.assertRegex(stdout[0], r'@my_repo//:lib_aaa \([\w\d]+\)') + + def testCqueryModuleRepoTransitiveDeps(self): + self.ScratchFile('MODULE.bazel', [ + 'bazel_dep(name = "aaa", version = "1.0", repo_name = "my_repo")', + ]) + self.ScratchFile('BUILD', [ + 'cc_binary(', + ' name = "main",', + ' srcs = ["main.cc"],', + ' deps = ["@my_repo//:lib_aaa"],', + ')', + ]) + _, stdout, _ = self.RunBazel( + [ + 'cquery', + 'kind("cc_.* rule", deps(//:main))', + '--noimplicit_deps', + '--notool_deps', + ], + allow_failure=False) + self.assertRegex(stdout[0], r'^//:main \([\w\d]+\)$') + self.assertRegex(stdout[1], r'^@my_repo//:lib_aaa \([\w\d]+\)$') + self.assertRegex(stdout[2], r'^@@ccc~1.2//:lib_ccc \([\w\d]+\)$') + self.assertEqual(len(stdout), 3) def testFetchModuleRepoTargetsBelow(self): self.ScratchFile('MODULE.bazel', [ @@ -101,7 +175,7 @@ def testGenQueryTargetLiteralInGenRule(self): self.assertIsNotNone(output_file) output = output_file.readlines() output_file.close() - self.assertListEqual(['@aaa~1.0//:lib_aaa\n'], output) + self.assertListEqual(['@my_repo//:lib_aaa\n'], output) def testQueryCannotResolveRepoMapping_malformedModuleFile(self): self.ScratchFile('MODULE.bazel', [