Thread a path resolver through target/aspect complete event.

At the moment, an identity path resolver is passed. This will later be replaced by a contextual path resolver.

RELNOTES: None
PiperOrigin-RevId: 207138772
diff --git a/src/main/java/com/google/devtools/build/lib/actions/ArtifactPathResolver.java b/src/main/java/com/google/devtools/build/lib/actions/ArtifactPathResolver.java
index 3aabfcab..d9f5e3d 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/ArtifactPathResolver.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/ArtifactPathResolver.java
@@ -22,7 +22,7 @@
 /**
  * An indirection layer on Path resolution of {@link Artifact} and {@link Root}.
  *
- * Serves as converter interface primarily for switching the {@link FileSystem} underyling the
+ * <p>Serves as converter interface primarily for switching the {@link FileSystem} underlying the
  * values.
  */
 public interface ArtifactPathResolver {
diff --git a/src/main/java/com/google/devtools/build/lib/actions/EventReportingArtifacts.java b/src/main/java/com/google/devtools/build/lib/actions/EventReportingArtifacts.java
index a81fc00..1df2987 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/EventReportingArtifacts.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/EventReportingArtifacts.java
@@ -20,6 +20,19 @@
 
 /** Interface for {@link BuildEvent}s reporting artifacts as named sets */
 public interface EventReportingArtifacts extends BuildEvent {
-  /** The sets of artifacts this build event asumes already known in the build event stream. */
-  Collection<NestedSet<Artifact>> reportedArtifacts();
+
+  /** Pair of artifacts and a path resolver. */
+  class ReportedArtifacts {
+    public final Collection<NestedSet<Artifact>> artifacts;
+    public final ArtifactPathResolver pathResolver;
+
+    public ReportedArtifacts(
+        Collection<NestedSet<Artifact>> artifacts, ArtifactPathResolver pathResolver) {
+      this.artifacts = artifacts;
+      this.pathResolver = pathResolver;
+    }
+  }
+
+  /** The sets of artifacts this build event assumes already known in the build event stream. */
+  ReportedArtifacts reportedArtifacts();
 }
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/AspectCompleteEvent.java b/src/main/java/com/google/devtools/build/lib/analysis/AspectCompleteEvent.java
index 6cd519f..f7d0b70 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/AspectCompleteEvent.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/AspectCompleteEvent.java
@@ -18,6 +18,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.actions.ArtifactPathResolver;
 import com.google.devtools.build.lib.actions.EventReportingArtifacts;
 import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper.ArtifactsInOutputGroup;
 import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper.ArtifactsToBuild;
@@ -43,10 +44,14 @@
   private final AspectValue aspectValue;
   private final NestedSet<Cause> rootCauses;
   private final Collection<BuildEventId> postedAfter;
+  private final ArtifactPathResolver pathResolver;
   private final ArtifactsToBuild artifacts;
 
   private AspectCompleteEvent(
-      AspectValue aspectValue, NestedSet<Cause> rootCauses, ArtifactsToBuild artifacts) {
+      AspectValue aspectValue,
+      NestedSet<Cause> rootCauses,
+      ArtifactPathResolver pathResolver,
+      ArtifactsToBuild artifacts) {
     this.aspectValue = aspectValue;
     this.rootCauses =
         (rootCauses == null) ? NestedSetBuilder.<Cause>emptySet(Order.STABLE_ORDER) : rootCauses;
@@ -55,13 +60,14 @@
       postedAfterBuilder.add(BuildEventId.fromCause(cause));
     }
     this.postedAfter = postedAfterBuilder.build();
+    this.pathResolver = pathResolver;
     this.artifacts = artifacts;
   }
 
   /** Construct a successful target completion event. */
   public static AspectCompleteEvent createSuccessful(
-      AspectValue value, ArtifactsToBuild artifacts) {
-    return new AspectCompleteEvent(value, null, artifacts);
+      AspectValue value, ArtifactPathResolver pathResolver, ArtifactsToBuild artifacts) {
+    return new AspectCompleteEvent(value, null, pathResolver, artifacts);
   }
 
   /**
@@ -69,7 +75,7 @@
    */
   public static AspectCompleteEvent createFailed(AspectValue value, NestedSet<Cause> rootCauses) {
     Preconditions.checkArgument(!Iterables.isEmpty(rootCauses));
-    return new AspectCompleteEvent(value, rootCauses, null);
+    return new AspectCompleteEvent(value, rootCauses, ArtifactPathResolver.IDENTITY, null);
   }
 
   /**
@@ -108,15 +114,14 @@
   }
 
   @Override
-  public Collection<NestedSet<Artifact>> reportedArtifacts() {
-    ImmutableSet.Builder<NestedSet<Artifact>> builder =
-        new ImmutableSet.Builder<NestedSet<Artifact>>();
+  public ReportedArtifacts reportedArtifacts() {
+    ImmutableSet.Builder<NestedSet<Artifact>> builder = ImmutableSet.builder();
     if (artifacts != null) {
       for (ArtifactsInOutputGroup artifactsInGroup : artifacts.getAllArtifactsByOutputGroup()) {
         builder.add(artifactsInGroup.getArtifacts());
       }
     }
-    return builder.build();
+    return new ReportedArtifacts(builder.build(), pathResolver);
   }
 
   @Override
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 53a3f16..e1dfa5e 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
@@ -21,6 +21,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.actions.ArtifactPathResolver;
 import com.google.devtools.build.lib.actions.EventReportingArtifacts;
 import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper.ArtifactsInOutputGroup;
 import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
@@ -101,6 +102,7 @@
   private final ConfiguredTargetKey configuredTargetKey;
   private final NestedSet<Cause> rootCauses;
   private final ImmutableList<BuildEventId> postedAfter;
+  private final ArtifactPathResolver pathResolver;
   private final NestedSet<ArtifactsInOutputGroup> outputs;
   private final NestedSet<Artifact> baselineCoverageArtifacts;
   private final Label aliasLabel;
@@ -115,6 +117,7 @@
   private TargetCompleteEvent(
       ConfiguredTargetAndData targetAndData,
       NestedSet<Cause> rootCauses,
+      ArtifactPathResolver pathResolver,
       NestedSet<ArtifactsInOutputGroup> outputs,
       boolean isTest) {
     this.rootCauses =
@@ -136,6 +139,7 @@
       postedAfterBuilder.add(BuildEventId.fromCause(cause));
     }
     this.postedAfter = postedAfterBuilder.build();
+    this.pathResolver = pathResolver;
     this.outputs = outputs;
     this.isTest = isTest;
     this.testTimeoutSeconds = isTest ? getTestTimeoutSeconds(targetAndData) : null;
@@ -175,14 +179,18 @@
 
   /** Construct a successful target completion event. */
   public static TargetCompleteEvent successfulBuild(
-      ConfiguredTargetAndData ct, NestedSet<ArtifactsInOutputGroup> outputs) {
-    return new TargetCompleteEvent(ct, null, outputs, false);
+      ConfiguredTargetAndData ct,
+      ArtifactPathResolver pathResolver,
+      NestedSet<ArtifactsInOutputGroup> outputs) {
+    return new TargetCompleteEvent(ct, null, pathResolver, outputs, false);
   }
 
   /** Construct a successful target completion event for a target that will be tested. */
   public static TargetCompleteEvent successfulBuildSchedulingTest(
-      ConfiguredTargetAndData ct, NestedSet<ArtifactsInOutputGroup> outputs) {
-    return new TargetCompleteEvent(ct, null, outputs, true);
+      ConfiguredTargetAndData ct,
+      ArtifactPathResolver pathResolver,
+      NestedSet<ArtifactsInOutputGroup> outputs) {
+    return new TargetCompleteEvent(ct, null, pathResolver, outputs, true);
   }
 
   /**
@@ -192,7 +200,11 @@
       ConfiguredTargetAndData ct, NestedSet<Cause> rootCauses) {
     Preconditions.checkArgument(!Iterables.isEmpty(rootCauses));
     return new TargetCompleteEvent(
-        ct, rootCauses, NestedSetBuilder.emptySet(Order.STABLE_ORDER), false);
+        ct,
+        rootCauses,
+        ArtifactPathResolver.IDENTITY,
+        NestedSetBuilder.emptySet(Order.STABLE_ORDER),
+        false);
   }
 
   /** Returns the label of the target associated with the event. */
@@ -260,20 +272,23 @@
   // TODO(aehlig): remove as soon as we managed to get rid of the deprecated "important_output"
   // field.
   private static void addImportantOutputs(
+      ArtifactPathResolver pathResolver,
       BuildEventStreamProtos.TargetComplete.Builder builder,
       BuildEventContext converters,
       Iterable<Artifact> artifacts) {
-    addImportantOutputs(builder, Artifact::getRootRelativePathString, converters, artifacts);
+    addImportantOutputs(
+        pathResolver, builder, Artifact::getRootRelativePathString, converters, artifacts);
   }
 
   private static void addImportantOutputs(
+      ArtifactPathResolver pathResolver,
       BuildEventStreamProtos.TargetComplete.Builder builder,
       Function<Artifact, String> artifactNameFunction,
       BuildEventContext converters,
       Iterable<Artifact> artifacts) {
     for (Artifact artifact : artifacts) {
       String name = artifactNameFunction.apply(artifact);
-      String uri = converters.pathConverter().apply(artifact.getPath());
+      String uri = converters.pathConverter().apply(pathResolver.toPath(artifact));
       if (uri != null) {
         builder.addImportantOutput(File.newBuilder().setName(name).setUri(uri).build());
       }
@@ -288,7 +303,7 @@
         for (Artifact artifact : group.getArtifacts()) {
           builder.add(
               new LocalFile(
-                  artifact.getPath(),
+                  pathResolver.toPath(artifact),
                   artifact.isSourceArtifact() ? LocalFileType.SOURCE : LocalFileType.OUTPUT));
         }
       }
@@ -297,7 +312,7 @@
       for (Artifact artifact : baselineCoverageArtifacts) {
         builder.add(
             new LocalFile(
-                artifact.getPath(),
+                pathResolver.toPath(artifact),
                 artifact.isSourceArtifact() ? LocalFileType.SOURCE : LocalFileType.OUTPUT));
       }
     }
@@ -320,10 +335,14 @@
     // TODO(aehlig): remove direct reporting of artifacts as soon as clients no longer
     // need it.
     if (converters.getOptions().legacyImportantOutputs) {
-      addImportantOutputs(builder, converters, getLegacyFilteredImportantArtifacts());
+      addImportantOutputs(pathResolver, builder, converters, getLegacyFilteredImportantArtifacts());
       if (baselineCoverageArtifacts != null) {
         addImportantOutputs(
-            builder, (artifact -> BASELINE_COVERAGE), converters, baselineCoverageArtifacts);
+            pathResolver,
+            builder,
+            (artifact -> BASELINE_COVERAGE),
+            converters,
+            baselineCoverageArtifacts);
       }
     }
 
@@ -337,16 +356,15 @@
   }
 
   @Override
-  public Collection<NestedSet<Artifact>> reportedArtifacts() {
-    ImmutableSet.Builder<NestedSet<Artifact>> builder =
-        new ImmutableSet.Builder<NestedSet<Artifact>>();
+  public ReportedArtifacts reportedArtifacts() {
+    ImmutableSet.Builder<NestedSet<Artifact>> builder = ImmutableSet.builder();
     for (ArtifactsInOutputGroup artifactsInGroup : outputs) {
       builder.add(artifactsInGroup.getArtifacts());
     }
     if (baselineCoverageArtifacts != null) {
       builder.add(baselineCoverageArtifacts);
     }
-    return builder.build();
+    return new ReportedArtifacts(builder.build(), pathResolver);
   }
 
   @Override
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BuildEventStreamer.java b/src/main/java/com/google/devtools/build/lib/runtime/BuildEventStreamer.java
index 6af8254..5292976 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BuildEventStreamer.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BuildEventStreamer.java
@@ -30,7 +30,9 @@
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.devtools.build.lib.actions.ActionExecutedEvent;
 import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.actions.ArtifactPathResolver;
 import com.google.devtools.build.lib.actions.EventReportingArtifacts;
+import com.google.devtools.build.lib.actions.EventReportingArtifacts.ReportedArtifacts;
 import com.google.devtools.build.lib.analysis.BuildInfoEvent;
 import com.google.devtools.build.lib.analysis.NoBuildEvent;
 import com.google.devtools.build.lib.analysis.extra.ExtraAction;
@@ -397,7 +399,8 @@
     }
   }
 
-  private void maybeReportArtifactSet(NestedSetView<Artifact> view) {
+  private void maybeReportArtifactSet(
+      ArtifactPathResolver pathResolver, NestedSetView<Artifact> view) {
     String name = artifactGroupNamer.maybeName(view);
     if (name == null) {
       return;
@@ -411,13 +414,13 @@
       view = view.splitIfExceedsMaximumSize(options.maxNamedSetEntries);
     }
     for (NestedSetView<Artifact> transitive : view.transitives()) {
-      maybeReportArtifactSet(transitive);
+      maybeReportArtifactSet(pathResolver, transitive);
     }
-    post(new NamedArtifactGroup(name, view));
+    post(new NamedArtifactGroup(name, pathResolver, view));
   }
 
-  private void maybeReportArtifactSet(NestedSet<Artifact> set) {
-    maybeReportArtifactSet(new NestedSetView<Artifact>(set));
+  private void maybeReportArtifactSet(ArtifactPathResolver pathResolver, NestedSet<Artifact> set) {
+    maybeReportArtifactSet(pathResolver, new NestedSetView<Artifact>(set));
   }
 
   private void maybeReportConfiguration(BuildEvent configuration) {
@@ -483,9 +486,9 @@
     }
 
     if (event instanceof EventReportingArtifacts) {
-      for (NestedSet<Artifact> artifactSet :
-          ((EventReportingArtifacts) event).reportedArtifacts()) {
-        maybeReportArtifactSet(artifactSet);
+      ReportedArtifacts reportedArtifacts = ((EventReportingArtifacts) event).reportedArtifacts();
+      for (NestedSet<Artifact> artifactSet : reportedArtifacts.artifacts) {
+        maybeReportArtifactSet(reportedArtifacts.pathResolver, artifactSet);
       }
     }
 
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/NamedArtifactGroup.java b/src/main/java/com/google/devtools/build/lib/runtime/NamedArtifactGroup.java
index 8624658..9bf0f23 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/NamedArtifactGroup.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/NamedArtifactGroup.java
@@ -17,6 +17,7 @@
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.actions.ArtifactPathResolver;
 import com.google.devtools.build.lib.actions.EventReportingArtifacts;
 import com.google.devtools.build.lib.buildeventstream.ArtifactGroupNamer;
 import com.google.devtools.build.lib.buildeventstream.BuildEvent;
@@ -36,10 +37,12 @@
  */
 class NamedArtifactGroup implements BuildEvent {
   private final String name;
+  private final ArtifactPathResolver pathResolver;
   private final NestedSetView<Artifact> view;
 
-  NamedArtifactGroup(String name, NestedSetView<Artifact> view) {
+  NamedArtifactGroup(String name, ArtifactPathResolver pathResolver, NestedSetView<Artifact> view) {
     this.name = name;
+    this.pathResolver = pathResolver;
     this.view = view;
   }
 
@@ -63,7 +66,7 @@
       }
       artifacts.add(
           new LocalFile(
-              artifact.getPath(),
+              pathResolver.toPath(artifact),
               artifact.isSourceArtifact() ? LocalFileType.SOURCE : LocalFileType.OUTPUT));
     }
     return artifacts.build();
@@ -82,7 +85,7 @@
         continue;
       }
       String name = artifact.getRootRelativePathString();
-      String uri = pathConverter.apply(artifact.getPath());
+      String uri = pathConverter.apply(pathResolver.toPath(artifact));
       if (uri != null) {
         builder.addFiles(BuildEventStreamProtos.File.newBuilder().setName(name).setUri(uri));
       }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/CompletionFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/CompletionFunction.java
index 0f07d7e..5d1fa1c 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/CompletionFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/CompletionFunction.java
@@ -15,6 +15,7 @@
 
 import com.google.devtools.build.lib.actions.ActionExecutionException;
 import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.actions.ArtifactPathResolver;
 import com.google.devtools.build.lib.actions.ArtifactSkyKey;
 import com.google.devtools.build.lib.actions.MissingInputFileException;
 import com.google.devtools.build.lib.analysis.AspectCompleteEvent;
@@ -91,6 +92,7 @@
     ExtendedEventHandler.Postable createSucceeded(
         SkyKey skyKey,
         TValue value,
+        ArtifactPathResolver pathResolver,
         TopLevelArtifactContext topLevelArtifactContext,
         Environment env)
         throws InterruptedException;
@@ -184,6 +186,7 @@
     public ExtendedEventHandler.Postable createSucceeded(
         SkyKey skyKey,
         ConfiguredTargetValue value,
+        ArtifactPathResolver pathResolver,
         TopLevelArtifactContext topLevelArtifactContext,
         Environment env)
         throws InterruptedException {
@@ -197,10 +200,10 @@
           TopLevelArtifactHelper.getAllArtifactsToBuild(target, topLevelArtifactContext);
       if (((TargetCompletionKey) skyKey.argument()).willTest()) {
         return TargetCompleteEvent.successfulBuildSchedulingTest(
-            configuredTargetAndData, artifactsToBuild.getAllArtifactsByOutputGroup());
+            configuredTargetAndData, pathResolver, artifactsToBuild.getAllArtifactsByOutputGroup());
       } else {
         return TargetCompleteEvent.successfulBuild(
-            configuredTargetAndData, artifactsToBuild.getAllArtifactsByOutputGroup());
+            configuredTargetAndData, pathResolver, artifactsToBuild.getAllArtifactsByOutputGroup());
       }
     }
   }
@@ -269,11 +272,12 @@
     public ExtendedEventHandler.Postable createSucceeded(
         SkyKey skyKey,
         AspectValue value,
+        ArtifactPathResolver pathResolver,
         TopLevelArtifactContext topLevelArtifactContext,
         Environment env) {
       ArtifactsToBuild artifacts =
           TopLevelArtifactHelper.getAllArtifactsToBuild(value, topLevelArtifactContext);
-      return AspectCompleteEvent.createSuccessful(value, artifacts);
+      return AspectCompleteEvent.createSuccessful(value, pathResolver, artifacts);
     }
   }
 
@@ -362,7 +366,8 @@
       return null;
     }
     ExtendedEventHandler.Postable postable =
-        completor.createSucceeded(skyKey, value, topLevelContext, env);
+        completor.createSucceeded(
+            skyKey, value, ArtifactPathResolver.IDENTITY, topLevelContext, env);
     if (postable == null) {
       return null;
     }
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/TargetCompleteEventTest.java b/src/test/java/com/google/devtools/build/lib/analysis/TargetCompleteEventTest.java
index 4d16414..ea48bb3 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/TargetCompleteEventTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/TargetCompleteEventTest.java
@@ -17,6 +17,7 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import com.google.common.collect.Iterables;
+import com.google.devtools.build.lib.actions.ArtifactPathResolver;
 import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper.ArtifactsToBuild;
 import com.google.devtools.build.lib.analysis.util.AnalysisTestCase;
 import com.google.devtools.build.lib.buildeventstream.BuildEvent;
@@ -47,7 +48,9 @@
         TopLevelArtifactHelper.getAllArtifactsToBuild(ct, context);
     TargetCompleteEvent event =
         TargetCompleteEvent.successfulBuild(
-            ctAndData, artifactsToBuild.getAllArtifactsByOutputGroup());
+            ctAndData,
+            ArtifactPathResolver.IDENTITY,
+            artifactsToBuild.getAllArtifactsByOutputGroup());
     assertThat(event.referencedLocalFiles())
         .contains(
             new BuildEvent.LocalFile(
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java b/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
index 7d7bbc4..53d4a74 100644
--- a/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/runtime/BuildEventStreamerTest.java
@@ -30,6 +30,7 @@
 import com.google.devtools.build.lib.actions.ActionExecutedEvent.ErrorTiming;
 import com.google.devtools.build.lib.actions.ActionExecutionException;
 import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.actions.ArtifactPathResolver;
 import com.google.devtools.build.lib.actions.ArtifactRoot;
 import com.google.devtools.build.lib.actions.EventReportingArtifacts;
 import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
@@ -213,8 +214,8 @@
     }
 
     @Override
-    public Collection<NestedSet<Artifact>> reportedArtifacts() {
-      return artifacts;
+    public ReportedArtifacts reportedArtifacts() {
+      return new ReportedArtifacts(artifacts, ArtifactPathResolver.IDENTITY);
     }
 
     @Override