Start the process of getting Target out of ConfiguredTarget: add a new container, ConfiguredTargetAndTarget, that can be used to access Targets, and deprecate ConfiguredTarget#getTarget. ConfiguredAndTargetObjects are intended to be limited in scope, not being persisted to Skyframe.

The eventual plan is to remove the target field from ConfiguredTarget.

This CL is mostly straightforward, except for dealing with AliasConfiguredTargets, which cause some complications.

A significant cleanup is still needed before #getTarget can be removed, but I don't see any impossible blockers. We will may still need to store a Target-like object in ConfiguredTarget (that has the RuleClass, or at least a string representation of it, for instance), but that will let us avoid storing a full Target together with its associated Package.

PiperOrigin-RevId: 182371566
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/AspectFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/AspectFunction.java
index 56599c1..630830d 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/AspectFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/AspectFunction.java
@@ -45,6 +45,7 @@
 import com.google.devtools.build.lib.packages.Attribute;
 import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException;
 import com.google.devtools.build.lib.packages.NativeAspectClass;
+import com.google.devtools.build.lib.packages.NoSuchTargetException;
 import com.google.devtools.build.lib.packages.NoSuchThingException;
 import com.google.devtools.build.lib.packages.Package;
 import com.google.devtools.build.lib.packages.RuleClassProvider;
@@ -244,13 +245,24 @@
 
     ConfiguredTarget associatedTarget = configuredTargetValue.getConfiguredTarget();
 
-    Target target = associatedTarget.getTarget();
+    ConfiguredTargetAndTarget associatedConfiguredTargetAndTarget;
+    Package targetPkg =
+        ((PackageValue)
+                env.getValue(PackageValue.key(associatedTarget.getLabel().getPackageIdentifier())))
+            .getPackage();
+    try {
+      associatedConfiguredTargetAndTarget =
+          new ConfiguredTargetAndTarget(
+              associatedTarget, targetPkg.getTarget(associatedTarget.getLabel().getName()));
+    } catch (NoSuchTargetException e) {
+      throw new IllegalStateException("Name already verified", e);
+    }
 
     if (configuredTargetValue.getConfiguredTarget().getProvider(AliasProvider.class) != null) {
       return createAliasAspect(
           env,
           view.getActionKeyContext(),
-          target,
+          associatedConfiguredTargetAndTarget.getTarget(),
           aspect,
           key,
           configuredTargetValue.getConfiguredTarget());
@@ -286,6 +298,8 @@
 
       }
     }
+    associatedConfiguredTargetAndTarget =
+        associatedConfiguredTargetAndTarget.fromConfiguredTarget(associatedTarget);
     aspectPathBuilder.add(aspect);
 
     SkyframeDependencyResolver resolver = view.createDependencyResolver(env);
@@ -299,13 +313,14 @@
     // required by all dependencies (both those of the aspect and those of the base target)
     // will be present this way.
     TargetAndConfiguration originalTargetAndAspectConfiguration =
-        new TargetAndConfiguration(target, key.getAspectConfiguration());
+        new TargetAndConfiguration(
+            associatedConfiguredTargetAndTarget.getTarget(), key.getAspectConfiguration());
     ImmutableList<Aspect> aspectPath = aspectPathBuilder.build();
     try {
       // Get the configuration targets that trigger this rule's configurable attributes.
       ImmutableMap<Label, ConfigMatchingProvider> configConditions =
           ConfiguredTargetFunction.getConfigConditions(
-              target,
+              associatedConfiguredTargetAndTarget.getTarget(),
               env,
               resolver,
               originalTargetAndAspectConfiguration,
@@ -325,7 +340,8 @@
                 env,
                 String.format(
                     "aspect %s applied to %s",
-                    aspect.getDescriptor().getDescription(), target.toString()),
+                    aspect.getDescriptor().getDescription(),
+                    associatedConfiguredTargetAndTarget.getTarget().toString()),
                 requiredToolchains,
                 key.getAspectConfiguration());
       } catch (ToolchainContextException e) {
@@ -336,7 +352,7 @@
         return null;
       }
 
-      OrderedSetMultimap<Attribute, ConfiguredTarget> depValueMap;
+      OrderedSetMultimap<Attribute, ConfiguredTargetAndTarget> depValueMap;
       try {
         depValueMap =
             ConfiguredTargetFunction.computeDependencies(
@@ -368,7 +384,7 @@
           aspectPath,
           aspect,
           aspectFactory,
-          associatedTarget,
+          associatedConfiguredTargetAndTarget,
           key.getAspectConfiguration(),
           configConditions,
           toolchainContext,
@@ -496,11 +512,11 @@
       ImmutableList<Aspect> aspectPath,
       Aspect aspect,
       ConfiguredAspectFactory aspectFactory,
-      ConfiguredTarget associatedTarget,
+      ConfiguredTargetAndTarget associatedTarget,
       BuildConfiguration aspectConfiguration,
       ImmutableMap<Label, ConfigMatchingProvider> configConditions,
       ToolchainContext toolchainContext,
-      OrderedSetMultimap<Attribute, ConfiguredTarget> directDeps,
+      OrderedSetMultimap<Attribute, ConfiguredTargetAndTarget> directDeps,
       @Nullable NestedSetBuilder<Package> transitivePackagesForPackageRootResolution)
       throws AspectFunctionException, InterruptedException {
 
@@ -540,8 +556,11 @@
     events.replayOn(env.getListener());
     if (events.hasErrors()) {
       analysisEnvironment.disable(associatedTarget.getTarget());
-      throw new AspectFunctionException(new AspectCreationException(
-          "Analysis of target '" + associatedTarget.getLabel() + "' failed; build aborted"));
+      throw new AspectFunctionException(
+          new AspectCreationException(
+              "Analysis of target '"
+                  + associatedTarget.getTarget().getLabel()
+                  + "' failed; build aborted"));
     }
     Preconditions.checkState(!analysisEnvironment.hasErrors(),
         "Analysis environment hasError() but no errors reported");
@@ -556,7 +575,7 @@
     return new AspectValue(
         key,
         aspect,
-        associatedTarget.getLabel(),
+        associatedTarget.getTarget().getLabel(),
         associatedTarget.getTarget().getLocation(),
         configuredAspect,
         actionKeyContext,