From cb269bbac30b45c97fa87bc766e541e0f40591b7 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Sun, 16 Oct 2022 13:36:45 +0200 Subject: [PATCH] WIP: Decanonicalize repo names in query output if possible --- .../google/devtools/build/lib/cmdline/Label.java | 4 ++++ .../build/lib/cmdline/PackageIdentifier.java | 9 +++++++++ .../build/lib/cmdline/RepositoryMapping.java | 15 +++++++++++++++ .../build/lib/cmdline/TargetPattern.java | 1 + .../lib/query2/PostAnalysisQueryEnvironment.java | 6 ++++++ .../build/lib/query2/SkyQueryEnvironment.java | 7 +++++++ .../aquery/ActionGraphQueryEnvironment.java | 3 ++- .../ActionGraphTextOutputFormatterCallback.java | 10 +++++++--- .../cquery/ConfiguredTargetQueryEnvironment.java | 10 +++++++--- ...lAndConfigurationOutputFormatterCallback.java | 8 ++++++-- .../TransitionsOutputFormatterCallback.java | 16 +++++++++++----- .../lib/query2/engine/QueryEnvironment.java | 5 +++++ .../lib/query2/query/BlazeQueryEnvironment.java | 6 ++++++ .../query/GraphlessBlazeQueryEnvironment.java | 6 ++++++ .../query/output/AbstractUnorderedFormatter.java | 4 +++- .../query/output/BuildOutputFormatter.java | 4 ++-- .../query/output/GraphOutputFormatter.java | 2 ++ .../query/output/LabelOutputFormatter.java | 6 +++--- .../query/output/LocationOutputFormatter.java | 4 ++-- .../query/output/MaxrankOutputFormatter.java | 2 ++ .../query/output/MinrankOutputFormatter.java | 2 ++ .../lib/query2/query/output/OutputFormatter.java | 1 + .../query/output/PackageOutputFormatter.java | 11 ++++++----- .../query/output/ProtoOutputFormatter.java | 10 ++-------- .../query2/query/output/QueryOutputUtils.java | 6 ++++-- .../query2/query/output/StreamedFormatter.java | 2 +- .../output/StreamedProtoOutputFormatter.java | 3 ++- .../query2/query/output/XmlOutputFormatter.java | 4 ++-- .../build/lib/rules/genquery/GenQuery.java | 7 ++++--- .../build/lib/runtime/commands/QueryCommand.java | 4 ++-- .../devtools/build/lib/query2/cquery/BUILD | 1 + .../cquery/TransitionsOutputFormatterTest.java | 4 +++- src/test/py/bazel/bzlmod/bzlmod_query_test.py | 7 ++++--- 33 files changed, 140 insertions(+), 50 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 8fb40e0d39da1c..df1738f0d1ecee 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,6 +419,10 @@ public String getUnambiguousCanonicalForm() { return packageIdentifier.getUnambiguousCanonicalForm() + ":" + name; } + public String getDisplayForm(RepositoryMapping mainRepoMapping) { + return packageIdentifier.getDisplayForm(mainRepoMapping) + ":" + name; + } + /** Return the name of the repository label refers to without the leading `at` symbol. */ @StarlarkMethod( name = "workspace_name", diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java b/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java index 5f6046c13d8d75..3e096f8278847c 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java @@ -186,6 +186,15 @@ public String getUnambiguousCanonicalForm() { return String.format("@@%s//%s", getRepository().getName(), getPackageFragment()); } + public String getDisplayForm(RepositoryMapping mainRepoMapping) { + if (repository.isMain()) { + return getCanonicalForm(); + } + return mainRepoMapping.getLocalNameFromMainRepo(repository) + .map(localName -> String.format("@%s//%s", localName, getPackageFragment())) + .orElseGet(this::getUnambiguousCanonicalForm); + } + /** * Returns the package path, possibly qualified with a repository name. * diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryMapping.java b/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryMapping.java index e504ff95f39d8c..30b60cc18e09b9 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryMapping.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryMapping.java @@ -19,6 +19,8 @@ import com.google.common.collect.ImmutableMap; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; import javax.annotation.Nullable; /** @@ -93,4 +95,17 @@ public RepositoryName get(String preMappingName) { return RepositoryName.createUnvalidated(preMappingName).toNonVisible(ownerRepo()); } } + + public Optional getLocalNameFromMainRepo(RepositoryName postMappingName) { + if (ownerRepo() == null) { + // This can only happen if Bzlmod is not enabled. In this case, assume no repo mappings are + // in use, so that canonical and local names can be used interchangeably. + return Optional.of(postMappingName.getName()); + } + Preconditions.checkArgument(ownerRepo().isMain()); + return repositoryMapping().entrySet().stream() + .filter(e -> e.getValue().equals(postMappingName)) + .map(Entry::getKey) + .findFirst(); + } } 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 4bce0a299c3ace..9472c3a301c6d0 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..6935ffbfb3c60a 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, 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..bfea2cf9fc60f0 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,10 @@ ThreadSafeMutableSet getBuildFiles( */ TargetAccessor getAccessor(); + default RepositoryMapping getMainRepoMapping() { + return RepositoryMapping.ALWAYS_FALLBACK; + } + /** * 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..ab8a5a4046b1f5 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 @@ -18,6 +18,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.FormatUtils.TargetOrdering; import com.google.devtools.build.lib.query2.query.output.GraphOutputWriter.NodeReader; @@ -53,6 +54,7 @@ public Comparator comparator() { @Override public void output( + QueryEnvironment env, QueryOptions options, Digraph result, OutputStream out, 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..347ab273f4027c 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 @@ -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..8394109ecbc11f 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, 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..7500ca36866ff8 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, 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/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..3cf5b2ad9801a2 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 @@ -150,6 +150,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/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 0827902f54c547..b8155ad175a6c4 100644 --- a/src/test/py/bazel/bzlmod/bzlmod_query_test.py +++ b/src/test/py/bazel/bzlmod/bzlmod_query_test.py @@ -56,7 +56,7 @@ 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 testAqueryModuleRepoTargetsBelow(self): self.ScratchFile('MODULE.bazel', [ @@ -66,6 +66,7 @@ def testAqueryModuleRepoTargetsBelow(self): _, stdout, _ = self.RunBazel(['aquery', '@my_repo//...'], allow_failure=False) self.assertEqual(stdout[0], 'cc_library-compile for @aaa~1.0//:lib_aaa') + self.assertIn('Target: @my_repo//:lib_aaa', stdout) def testCqueryModuleRepoTargetsBelow(self): self.ScratchFile('MODULE.bazel', [ @@ -74,7 +75,7 @@ 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 testFetchModuleRepoTargetsBelow(self): self.ScratchFile('MODULE.bazel', [ @@ -99,7 +100,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', [