Make aspects attached to aliases work when their actual= attribute is a select.

Fixes https://github.com/bazelbuild/e4b/issues/6 .

--
MOS_MIGRATED_REVID=131698950
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 d94be28..4035242 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
@@ -44,7 +44,7 @@
 import com.google.devtools.build.lib.packages.SkylarkAspect;
 import com.google.devtools.build.lib.packages.SkylarkAspectClass;
 import com.google.devtools.build.lib.packages.Target;
-import com.google.devtools.build.lib.packages.TargetUtils;
+import com.google.devtools.build.lib.rules.AliasProvider;
 import com.google.devtools.build.lib.skyframe.AspectValue.AspectKey;
 import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ConfiguredValueCreationException;
 import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.DependencyEvaluationException;
@@ -165,6 +165,7 @@
       throw new AspectFunctionException(
           new BuildFileContainsErrorsException(key.getLabel().getPackageIdentifier()));
     }
+
     Target target;
     try {
       target = pkg.getTarget(key.getLabel().getName());
@@ -172,11 +173,6 @@
       throw new AspectFunctionException(e);
     }
 
-    Label aliasLabel = TargetUtils.getAliasTarget(target);
-    if (aliasLabel != null) {
-      return createAliasAspect(env, target, aliasLabel, aspect, key);
-    }
-    
     if (!(target instanceof Rule)) {
       env.getListener().handle(Event.error(
           target.getLocation(),
@@ -186,7 +182,7 @@
           "aspects must be attached to rules"));
     }
 
-    final ConfiguredTargetValue configuredTargetValue;
+    ConfiguredTargetValue configuredTargetValue;
     try {
       configuredTargetValue =
           (ConfiguredTargetValue) env.getValueOrThrow(
@@ -202,12 +198,18 @@
       // precomputed.
       return null;
     }
-    RuleConfiguredTarget associatedTarget =
-        (RuleConfiguredTarget) configuredTargetValue.getConfiguredTarget();
-    if (associatedTarget == null) {
+
+    if (configuredTargetValue.getConfiguredTarget() == null) {
       return null;
     }
 
+    if (configuredTargetValue.getConfiguredTarget().getProvider(AliasProvider.class) != null) {
+      return createAliasAspect(env, target, aspect, key,
+          configuredTargetValue.getConfiguredTarget());
+    }
+
+    RuleConfiguredTarget associatedTarget =
+        (RuleConfiguredTarget) configuredTargetValue.getConfiguredTarget();
     SkyframeDependencyResolver resolver = view.createDependencyResolver(env);
 
     // When getting the dependencies of this hybrid aspect+base target, use the aspect's
@@ -277,15 +279,24 @@
   private static SkyValue createAliasAspect(
       Environment env,
       Target originalTarget,
-      Label aliasLabel,
       Aspect aspect,
-      AspectKey originalKey)
-      throws InterruptedException {
-    SkyKey depKey = AspectValue.key(aliasLabel,
+      AspectKey originalKey,
+      ConfiguredTarget configuredTarget) throws InterruptedException {
+    ImmutableList<Label> aliasChain = configuredTarget.getProvider(AliasProvider.class)
+        .getAliasChain();
+    // Find the next alias in the chain: either the next alias (if there are two) or the name of
+    // the real configured target.
+    Label aliasLabel = aliasChain.size() > 1 ? aliasChain.get(1) : configuredTarget.getLabel();
+
+    SkyKey depKey = AspectValue.key(
+        aliasLabel,
         originalKey.getAspectConfiguration(),
         originalKey.getBaseConfiguration(),
         originalKey.getAspectClass(),
         originalKey.getParameters());
+
+    // Compute the AspectValue of the target the alias refers to (which can itself be either an
+    // alias or a real target)
     AspectValue real = (AspectValue) env.getValue(depKey);
     if (env.valuesMissing()) {
       return null;
@@ -295,6 +306,7 @@
         .addTransitive(real.getTransitivePackages())
         .add(originalTarget.getPackage())
         .build();
+
     return new AspectValue(
         originalKey,
         aspect,