Get rid of the last reference to getAttributeMapper by keeping packages which
are referenced by TopLevelTargets when we discard the analysis cache.

PiperOrigin-RevId: 185574670
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/TargetCompleteEvent.java b/src/main/java/com/google/devtools/build/lib/analysis/TargetCompleteEvent.java
index 1a59721..753ce70 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/TargetCompleteEvent.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/TargetCompleteEvent.java
@@ -43,6 +43,9 @@
 import com.google.devtools.build.lib.collect.nestedset.NestedSetView;
 import com.google.devtools.build.lib.collect.nestedset.Order;
 import com.google.devtools.build.lib.packages.AttributeMap;
+import com.google.devtools.build.lib.packages.ConfiguredAttributeMapper;
+import com.google.devtools.build.lib.packages.Rule;
+import com.google.devtools.build.lib.packages.Target;
 import com.google.devtools.build.lib.packages.TestSize;
 import com.google.devtools.build.lib.rules.AliasConfiguredTarget;
 import com.google.devtools.build.lib.syntax.Type;
@@ -56,6 +59,7 @@
         EventReportingArtifacts,
         BuildEventWithConfiguration {
   private final ConfiguredTarget target;
+  private final Target actualTarget;
   private final NestedSet<Cause> rootCauses;
   private final ImmutableList<BuildEventId> postedAfter;
   private final Iterable<ArtifactsInOutputGroup> outputs;
@@ -64,10 +68,12 @@
 
   private TargetCompleteEvent(
       ConfiguredTarget target,
+      Target actualTarget,
       NestedSet<Cause> rootCauses,
       Iterable<ArtifactsInOutputGroup> outputs,
       boolean isTest) {
     this.target = target;
+    this.actualTarget = actualTarget;
     this.rootCauses =
         (rootCauses == null) ? NestedSetBuilder.<Cause>emptySet(Order.STABLE_ORDER) : rootCauses;
 
@@ -100,35 +106,33 @@
 
   /** Construct a successful target completion event. */
   public static TargetCompleteEvent successfulBuild(
-      ConfiguredTarget ct, NestedSet<ArtifactsInOutputGroup> outputs) {
-    return new TargetCompleteEvent(ct, null, outputs, false);
+      ConfiguredTarget ct, Target target, NestedSet<ArtifactsInOutputGroup> outputs) {
+    return new TargetCompleteEvent(ct, target, null, outputs, false);
   }
 
   /** Construct a successful target completion event for a target that will be tested. */
-  public static TargetCompleteEvent successfulBuildSchedulingTest(ConfiguredTarget ct) {
-    return new TargetCompleteEvent(ct, null, ImmutableList.<ArtifactsInOutputGroup>of(), true);
+  public static TargetCompleteEvent successfulBuildSchedulingTest(
+      ConfiguredTarget ct, Target target) {
+    return new TargetCompleteEvent(
+        ct, target, null, ImmutableList.<ArtifactsInOutputGroup>of(), true);
   }
 
-
   /**
    * Construct a target completion event for a failed target, with the given non-empty root causes.
    */
-  public static TargetCompleteEvent createFailed(ConfiguredTarget ct, NestedSet<Cause> rootCauses) {
+  public static TargetCompleteEvent createFailed(
+      ConfiguredTarget ct, Target target, NestedSet<Cause> rootCauses) {
     Preconditions.checkArgument(!Iterables.isEmpty(rootCauses));
     return new TargetCompleteEvent(
-        ct, rootCauses, ImmutableList.<ArtifactsInOutputGroup>of(), false);
+        ct, target, rootCauses, ImmutableList.<ArtifactsInOutputGroup>of(), false);
   }
 
-  /**
-   * Returns the target associated with the event.
-   */
+  /** Returns the target associated with the event. */
   public ConfiguredTarget getTarget() {
     return target;
   }
 
-  /**
-   * Determines whether the target has failed or succeeded.
-   */
+  /** Determines whether the target has failed or succeeded. */
   public boolean failed() {
     return !rootCauses.isEmpty();
   }
@@ -160,7 +164,7 @@
       // For tests, announce all the test actions that will minimally happen (except for
       // interruption). If after the result of a test action another attempt is necessary,
       // it will be announced with the action that made the new attempt necessary.
-      Label label = target.getTarget().getLabel();
+      Label label = target.getLabel();
       TestProvider.TestParams params = target.getProvider(TestProvider.class).getTestParams();
       for (int run = 0; run < Math.max(params.getRuns(), 1); run++) {
         for (int shard = 0; shard < Math.max(params.getShards(), 1); shard++) {
@@ -192,14 +196,14 @@
         BuildEventStreamProtos.TargetComplete.newBuilder();
 
     builder.setSuccess(!failed());
-    builder.setTargetKind(target.getTarget().getTargetKind());
+    builder.setTargetKind(actualTarget.getTargetKind());
     builder.addAllTag(getTags());
     builder.addAllOutputGroup(getOutputFilesByGroup(converters.artifactGroupNamer()));
 
     if (isTest) {
       builder.setTestSize(
           TargetConfiguredEvent.bepTestSize(
-              TestSize.getTestSize(target.getTarget().getAssociatedRule())));
+              TestSize.getTestSize(actualTarget.getAssociatedRule())));
     }
 
     // TODO(aehlig): remove direct reporting of artifacts as soon as clients no longer
@@ -250,7 +254,9 @@
     if (!(target instanceof RuleConfiguredTarget)) {
       return ImmutableList.<String>of();
     }
-    AttributeMap attributes = ((RuleConfiguredTarget) target).getAttributeMapper();
+    AttributeMap attributes =
+        ConfiguredAttributeMapper.of(
+            (Rule) actualTarget, ((RuleConfiguredTarget) target).getConfigConditions());
     // Every rule (implicitly) has a "tags" attribute.
     return attributes.get("tags", Type.STRING_LIST);
   }