Consolidate calls to AnalysisUtils.getTopLevelTargetsWithConfigs which conveniently also makes it unnecessary to pass the entire LoadingResult when doing configured queries post analysis.

PiperOrigin-RevId: 180676481
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java b/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java
index 8beac62..6061197 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/BuildView.java
@@ -321,6 +321,7 @@
     private final ImmutableList<AspectValue> aspects;
     private final PackageRoots packageRoots;
     private final String workspaceName;
+    List<TargetAndConfiguration> topLevelTargetsWithConfigs;
 
     private AnalysisResult(
         Collection<ConfiguredTarget> targetsToBuild,
@@ -334,7 +335,8 @@
         Collection<ConfiguredTarget> exclusiveTests,
         TopLevelArtifactContext topLevelContext,
         PackageRoots packageRoots,
-        String workspaceName) {
+        String workspaceName,
+        List<TargetAndConfiguration> topLevelTargetsWithConfigs) {
       this.targetsToBuild = ImmutableSet.copyOf(targetsToBuild);
       this.aspects = ImmutableList.copyOf(aspects);
       this.targetsToTest = targetsToTest == null ? null : ImmutableList.copyOf(targetsToTest);
@@ -347,6 +349,7 @@
       this.topLevelContext = topLevelContext;
       this.packageRoots = packageRoots;
       this.workspaceName = workspaceName;
+      this.topLevelTargetsWithConfigs = topLevelTargetsWithConfigs;
     }
 
     /**
@@ -427,8 +430,11 @@
     public String getWorkspaceName() {
       return workspaceName;
     }
-  }
 
+    public List<TargetAndConfiguration> getTopLevelTargetsWithConfigs() {
+      return topLevelTargetsWithConfigs;
+    }
+  }
 
   /**
    * Returns the collection of configured targets corresponding to any of the provided targets.
@@ -595,7 +601,8 @@
             topLevelOptions,
             viewOptions,
             skyframeAnalysisResult,
-            targetsToSkip);
+            targetsToSkip,
+            topLevelTargetsWithConfigs);
     logger.info("Finished analysis");
     return result;
   }
@@ -606,7 +613,8 @@
       TopLevelArtifactContext topLevelOptions,
       BuildView.Options viewOptions,
       SkyframeAnalysisResult skyframeAnalysisResult,
-      Set<ConfiguredTarget> targetsToSkip)
+      Set<ConfiguredTarget> targetsToSkip,
+      List<TargetAndConfiguration> topLevelTargetsWithConfigs)
       throws InterruptedException {
     Collection<Target> testsToRun = loadingResult.getTestsToRun();
     Set<ConfiguredTarget> configuredTargets =
@@ -690,7 +698,8 @@
         exclusiveTests,
         topLevelOptions,
         skyframeAnalysisResult.getPackageRoots(),
-        loadingResult.getWorkspaceName());
+        loadingResult.getWorkspaceName(),
+        topLevelTargetsWithConfigs);
   }
 
   @Nullable
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
index 42f53d6..4a71650 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
@@ -21,7 +21,6 @@
 import com.google.devtools.build.lib.actions.BuildFailedException;
 import com.google.devtools.build.lib.actions.TestExecException;
 import com.google.devtools.build.lib.analysis.AnalysisPhaseCompleteEvent;
-import com.google.devtools.build.lib.analysis.AnalysisUtils;
 import com.google.devtools.build.lib.analysis.BuildInfoEvent;
 import com.google.devtools.build.lib.analysis.BuildView;
 import com.google.devtools.build.lib.analysis.BuildView.AnalysisResult;
@@ -244,7 +243,8 @@
                 "Configured query is not allowed if incrementality state is not being kept");
           }
           try {
-            doConfiguredTargetQuery(request, configurations, loadingResult);
+            doConfiguredTargetQuery(
+                request, configurations, analysisResult.getTopLevelTargetsWithConfigs());
           } catch (QueryException | IOException e) {
             if (!request.getKeepGoing()) {
               throw new ViewCreationFailedException("Error doing configured target query", e);
@@ -412,18 +412,9 @@
   private void doConfiguredTargetQuery(
       BuildRequest request,
       BuildConfigurationCollection configurations,
-      LoadingResult loadingResult)
+      List<TargetAndConfiguration> topLevelTargetsWithConfigs)
       throws InterruptedException, QueryException, IOException {
 
-    // Determine the configurations.
-    List<TargetAndConfiguration> topLevelTargetsWithConfigs =
-        AnalysisUtils.getTargetsWithConfigs(
-            configurations,
-            loadingResult.getTargets(),
-            env.getReporter(),
-            runtime.getRuleClassProvider(),
-            env.getSkyframeExecutor());
-
     String queryExpr = request.getBuildOptions().queryExpression;
 
     // Currently, CTQE assumes that all top level targets take on the same default config and we