From 47b1cad5a6026c220002d56ae3c25e5c20d0cef7 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Wed, 2 Nov 2022 23:33:18 +0100 Subject: [PATCH] [6.0.0]Decanonicalize labels emitted by {a,c,}query if possible (#16638) Uses the newly added `PackageIdentifier#getDisplayForm` to turn labels in the output of query, aquery, and cquery into the most concise representation that allows them to be resolved from the context of the main repository. Closes #16483. PiperOrigin-RevId: 485584469 Change-Id: I9037bf128713af75b6741eca42b25e3beeb112f7 Co-authored-by: kshyanashree <109167932+kshyanashree@users.noreply.github.com> --- .../devtools/build/lib/cmdline/Label.java | 16 +++- .../query2/PostAnalysisQueryEnvironment.java | 6 ++ .../build/lib/query2/SkyQueryEnvironment.java | 7 ++ .../aquery/ActionGraphQueryEnvironment.java | 8 +- ...ctionGraphTextOutputFormatterCallback.java | 10 ++- .../ConfiguredTargetQueryEnvironment.java | 22 ++++- .../cquery/GraphOutputFormatterCallback.java | 17 +++- ...dConfigurationOutputFormatterCallback.java | 8 +- .../TransitionsOutputFormatterCallback.java | 14 +++- .../lib/query2/engine/QueryEnvironment.java | 8 ++ .../query2/query/BlazeQueryEnvironment.java | 6 ++ .../query/GraphlessBlazeQueryEnvironment.java | 6 ++ .../output/AbstractUnorderedFormatter.java | 7 +- .../query/output/BuildOutputFormatter.java | 5 +- .../query/output/GraphOutputFormatter.java | 11 ++- .../query/output/GraphOutputWriter.java | 12 ++- .../query/output/LabelOutputFormatter.java | 7 +- .../query/output/LocationOutputFormatter.java | 7 +- .../query/output/MaxrankOutputFormatter.java | 6 +- .../query/output/MinrankOutputFormatter.java | 6 +- .../query2/query/output/OutputFormatter.java | 4 +- .../query/output/PackageOutputFormatter.java | 17 ++-- .../query/output/ProtoOutputFormatter.java | 12 +-- .../query2/query/output/QueryOutputUtils.java | 15 +++- .../lib/query2/query/output/RankAndLabel.java | 7 +- .../query/output/StreamedFormatter.java | 3 +- .../output/StreamedProtoOutputFormatter.java | 3 +- .../query/output/XmlOutputFormatter.java | 5 +- .../build/lib/rules/genquery/GenQuery.java | 6 +- .../lib/runtime/commands/QueryCommand.java | 3 +- .../devtools/build/lib/query2/cquery/BUILD | 2 + .../GraphOutputFormatterCallbackTest.java | 4 +- .../TransitionsOutputFormatterTest.java | 4 +- src/test/py/bazel/bzlmod/bzlmod_query_test.py | 81 +++++++++++++++++-- 34 files changed, 280 insertions(+), 75 deletions(-) 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 438d1a50f1d8ec..b11ce30c63f4aa 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 @@ -419,9 +419,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/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..dd2d5ce7b04fe0 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,13 @@ 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..22fa1458106683 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 @@ -205,9 +205,21 @@ 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 +227,8 @@ private static ImmutableMap getTransitiveConfig skyframeExecutor, accessor, hostConfiguration, - trimmingTransitionFactory), + trimmingTransitionFactory, + getMainRepoMapping()), new ProtoOutputFormatterCallback( eventHandler, cqueryOptions, @@ -251,7 +264,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..009fcd4b0a974a 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,15 @@ 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 +83,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 +125,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..e3dc11941084cf 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; @@ -48,6 +49,7 @@ class TransitionsOutputFormatterCallback extends CqueryThreadsafeCallback { private final HashMap partialResultMap; @Nullable private final TransitionFactory trimmingTransitionFactory; + private final RepositoryMapping mainRepoMapping; @Override public String getName() { @@ -65,11 +67,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 @@ -92,7 +96,11 @@ public void processOutput(Iterable partialResult) getRuleClassTransition(keyedConfiguredTarget.getConfiguredTarget(), target) + String.format( "%s (%s)", - keyedConfiguredTarget.getConfiguredTarget().getOriginalLabel(), shortId(config))); + keyedConfiguredTarget + .getConfiguredTarget() + .getOriginalLabel() + .getDisplayForm(mainRepoMapping), + shortId(config))); KnownTargetsDependencyResolver knownTargetsDependencyResolver = new KnownTargetsDependencyResolver(partialResultMap); ImmutableSet dependencies; @@ -117,7 +125,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..90b58719ff9d41 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 @@ -16,6 +16,7 @@ import com.google.common.collect.Iterables; 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; @@ -45,12 +46,14 @@ public void output( OutputStream out, AspectResolver aspectResolver, @Nullable EventHandler eventHandler, - HashFunction hashFunction) + HashFunction hashFunction, + RepositoryMapping mainRepoMapping) throws IOException, InterruptedException { setOptions(options, aspectResolver, hashFunction); setEventHandler(eventHandler); OutputFormatterCallback.processAllTargets( - createPostFactoStreamCallback(out, options), getOrderedTargets(result, options)); + createPostFactoStreamCallback(out, options, mainRepoMapping), + 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..596cebd4c54361 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 @@ -17,6 +17,7 @@ import com.google.common.base.Ascii; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.collect.compacthashset.CompactHashSet; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.BuildType; @@ -221,7 +222,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, RepositoryMapping mainRepoMapping) { return new BuildOutputFormatterCallback(out, options.getLineTerminator()); } @@ -229,7 +230,7 @@ public OutputFormatterCallback createPostFactoStreamCallback( public ThreadSafeOutputFormatterCallback createStreamCallback( OutputStream out, QueryOptions options, QueryEnvironment env) { return new SynchronizedDelegatingOutputFormatterCallback<>( - createPostFactoStreamCallback(out, options)); + createPostFactoStreamCallback(out, options, env.getMainRepoMapping())); } 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..f92ddf8f9fdfa0 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,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; @@ -40,9 +41,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 @@ -58,7 +59,8 @@ public void output( OutputStream out, AspectResolver aspectProvider, EventHandler eventHandler, - HashFunction hashFunction) { + HashFunction hashFunction, + RepositoryMapping mainRepoMapping) { boolean sortLabels = options.orderOutput == OrderOutput.FULL; GraphOutputWriter graphWriter = new GraphOutputWriter<>( @@ -67,7 +69,8 @@ public void output( sortLabels, options.graphNodeStringLimit, options.graphConditionalEdgesLimit, - options.graphFactored); + options.graphFactored, + mainRepoMapping); 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..6826106bb6697c 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 @@ -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; 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; @@ -42,7 +43,7 @@ public String getName() { @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, final QueryOptions options) { + OutputStream out, final QueryOptions options, RepositoryMapping mainRepoMapping) { return new TextOutputFormatterCallback(out) { @Override public void processOutput(Iterable partialResult) throws IOException { @@ -53,7 +54,7 @@ public void processOutput(Iterable partialResult) throws IOException { writer.append(' '); } Label label = target.getLabel(); - writer.append(label.getCanonicalForm()).append(lineTerm); + writer.append(label.getDisplayForm(mainRepoMapping)).append(lineTerm); } } }; @@ -63,6 +64,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.getMainRepoMapping())); } } \ 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..6d2af48ffd22ae 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 @@ -16,6 +16,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.hash.HashFunction; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.query2.common.AbstractBlazeQueryEnvironment; import com.google.devtools.build.lib.query2.common.CommonQueryOptions; @@ -75,7 +76,7 @@ public void verifyCompatible(QueryEnvironment env, QueryExpression expr) @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, final QueryOptions options) { + OutputStream out, final QueryOptions options, RepositoryMapping mainRepoMapping) { return new TextOutputFormatterCallback(out) { @Override @@ -87,7 +88,7 @@ public void processOutput(Iterable partialResult) throws IOException { .append(": ") .append(target.getTargetKind()) .append(" ") - .append(target.getLabel().getCanonicalForm()) + .append(target.getLabel().getDisplayForm(mainRepoMapping)) .append(lineTerm); } } @@ -98,6 +99,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.getMainRepoMapping())); } } 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..0cfc33e7b6a404 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 @@ -17,6 +17,7 @@ import static java.util.Comparator.comparingInt; 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; @@ -58,7 +59,8 @@ public void output( OutputStream out, AspectResolver aspectResolver, EventHandler eventHandler, - HashFunction hashFunction) + HashFunction hashFunction, + RepositoryMapping mainRepoMapping) throws IOException { // In order to handle cycles correctly, we need work on the strong // component graph, as cycles should be treated a "clump" of nodes all on @@ -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(mainRepoMapping) + 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..b4236a8cb12525 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 @@ -16,6 +16,7 @@ import com.google.common.hash.HashFunction; import com.google.devtools.build.lib.cmdline.Label; +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; @@ -70,7 +71,8 @@ public void output( OutputStream out, AspectResolver aspectResolver, EventHandler eventHandler, - HashFunction hashFunction) + HashFunction hashFunction, + RepositoryMapping mainRepoMapping) throws IOException { PrintStream printStream = new PrintStream(out); // getRoots() isn't defined for cyclic graphs, so in order to handle @@ -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(mainRepoMapping) + 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..5b33485f3d16b7 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 @@ -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.packages.Target; @@ -58,6 +59,7 @@ public abstract void output( OutputStream out, AspectResolver aspectProvider, @Nullable EventHandler eventHandler, - HashFunction hashFunction) + HashFunction hashFunction, + RepositoryMapping mainRepoMapping) throws IOException, InterruptedException; } 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..70751a82c3a263 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 @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.query2.query.output; import com.google.common.collect.Sets; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; 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; @@ -25,8 +26,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 +38,7 @@ public String getName() { @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, final QueryOptions options) { + OutputStream out, final QueryOptions options, RepositoryMapping mainRepoMapping) { return new TextOutputFormatterCallback(out) { private final Set packageNames = Sets.newTreeSet(); @@ -45,7 +46,13 @@ public OutputFormatterCallback createPostFactoStreamCallback( public void processOutput(Iterable partialResult) { for (Target target : partialResult) { - packageNames.add(target.getLabel().getPackageIdentifier().toString()); + String packageLabel = + target.getLabel().getPackageIdentifier().getDisplayForm(mainRepoMapping); + // For backwards compatibility, emit main repo packages as "a/b" rather than "//a/b". + if (packageLabel.startsWith("//")) { + packageLabel = packageLabel.substring(2); + } + packageNames.add(packageLabel); } } @@ -66,6 +73,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.getMainRepoMapping())); } } \ 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..2071cdd7fc6a02 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 @@ -19,7 +19,6 @@ import static com.google.devtools.build.lib.query2.proto.proto2api.Build.Target.Discriminator.RULE; import static com.google.devtools.build.lib.query2.proto.proto2api.Build.Target.Discriminator.SOURCE_FILE; -import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.base.Predicates; @@ -32,6 +31,7 @@ import com.google.common.hash.HashFunction; import com.google.common.io.BaseEncoding; 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.EventHandler; import com.google.devtools.build.lib.graph.Digraph; @@ -158,21 +158,15 @@ private static Predicate newAttributePredicate(List outputAttrib @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, QueryOptions options) { + OutputStream out, QueryOptions options, RepositoryMapping mainRepoMapping) { 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.getMainRepoMapping())); } 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..fc354659e22713 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 @@ -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.packages.Target; @@ -59,7 +60,8 @@ public static void output( OutputStream outputStream, AspectResolver aspectResolver, @Nullable EventHandler eventHandler, - HashFunction hashFunction) + HashFunction hashFunction, + RepositoryMapping mainRepoMapping) throws IOException, InterruptedException { /* * This is not really streaming, but we are using the streaming interface for writing into the @@ -71,7 +73,8 @@ public static void output( streamedFormatter.setOptions(queryOptions, aspectResolver, hashFunction); streamedFormatter.setEventHandler(eventHandler); OutputFormatterCallback.processAllTargets( - streamedFormatter.createPostFactoStreamCallback(outputStream, queryOptions), + streamedFormatter.createPostFactoStreamCallback( + outputStream, queryOptions, mainRepoMapping), targetsResult); } else { @SuppressWarnings("unchecked") @@ -85,7 +88,13 @@ public static void output( try (SilentCloseable closeable = Profiler.instance().profile("formatter.output")) { formatter.output( - queryOptions, subgraph, outputStream, aspectResolver, eventHandler, hashFunction); + queryOptions, + subgraph, + outputStream, + aspectResolver, + eventHandler, + hashFunction, + mainRepoMapping); } } } 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..789c2c6b727aef 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 @@ -15,6 +15,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.packages.Target; import com.google.devtools.build.lib.query2.common.CommonQueryOptions; @@ -62,5 +63,5 @@ ThreadSafeOutputFormatterCallback createStreamCallback( * already-computed result of a query. */ OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, QueryOptions options); + OutputStream out, QueryOptions options, RepositoryMapping mainRepoMapping); } \ 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..4adb1a840778fd 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 @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.query2.query.output; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; import java.io.IOException; @@ -31,7 +32,7 @@ public String getName() { @Override public OutputFormatterCallback createPostFactoStreamCallback( - final OutputStream out, final QueryOptions options) { + final OutputStream out, final QueryOptions options, RepositoryMapping mainRepoMapping) { 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..96908362f3f317 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 @@ -18,6 +18,7 @@ import com.google.common.collect.Iterables; import com.google.common.hash.HashFunction; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.DependencyFilter; @@ -75,7 +76,7 @@ public String getName() { public ThreadSafeOutputFormatterCallback createStreamCallback( OutputStream out, QueryOptions options, QueryEnvironment env) { return new SynchronizedDelegatingOutputFormatterCallback<>( - createPostFactoStreamCallback(out, options)); + createPostFactoStreamCallback(out, options, env.getMainRepoMapping())); } @Override @@ -94,7 +95,7 @@ public void setOptions( @Override public OutputFormatterCallback createPostFactoStreamCallback( - OutputStream out, QueryOptions options) { + OutputStream out, QueryOptions options, RepositoryMapping mainRepoMapping) { 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..6bb4cf18725c42 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, @@ -367,7 +368,8 @@ private static GenQueryResult doQuery( outputStream, queryOptions.aspectDeps.createResolver(packageProvider, getEventHandler(ruleContext)), getEventHandler(ruleContext), - hashFunction); + hashFunction, + queryEnvironment.getMainRepoMapping()); 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..c7d62060981a61 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 @@ -185,7 +185,8 @@ protected Either doQuery( out, queryOptions.aspectDeps.createResolver(env.getPackageManager(), env.getReporter()), env.getReporter(), - hashFunction); + hashFunction, + queryEnv.getMainRepoMapping()); } 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..3b3e351a773d83 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,28 @@ 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 +90,32 @@ 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 +124,30 @@ 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 +172,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', [