Rollback of commit 4b73e972d909bcd533f2f9940f95a00b9b73bdde.

*** Reason for rollback ***

Broke tests on CI: http://ci.bazel.io/job/bazel-tests/570/


*** Original change description ***

Roll forward execroot change

RELNOTES[INC]: Previously, an external repository would be symlinked into the
execution root at execroot/local_repo/external/remote_repo. This changes it to
be at execroot/remote_repo. This may break genrules/Skylark actions that
hardcode execution root paths. If this causes breakages for you, ensure that
genrules are using $(location :target) to access files and Skylark rules are
using http://bazel.io/docs/skylark/lib/File.html's path, dirname, etc.
functions. Cust...

--
PiperOrigin-RevId: 147833177
MOS_MIGRATED_REVID=147833177
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java b/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
index 2e62eae..8f171a3 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java
@@ -427,19 +427,6 @@
     return newManifest;
   }
 
-  private static PathFragment getRootRelativePath(Artifact artifact) {
-    Preconditions.checkArgument(artifact != null);
-    if (artifact.getRoot().isMainRepo()) {
-      return artifact.getRootRelativePath();
-    }
-
-    return new PathFragment(Label.EXTERNAL_PATH_PREFIX)
-        .getRelative(artifact.isSourceArtifact()
-            ? artifact.getRoot().getPath().getBaseName()
-            : artifact.getRoot().getExecRoot().getBaseName())
-        .getRelative(artifact.getRootRelativePath());
-  }
-
   /**
    * Returns the symlinks as a map from PathFragment to Artifact.
    *
@@ -456,7 +443,7 @@
     Map<PathFragment, Artifact> manifest = getSymlinksAsMap(checker);
     // Add unconditional artifacts (committed to inclusion on construction of runfiles).
     for (Artifact artifact : getUnconditionalArtifactsWithoutMiddlemen()) {
-      checker.put(manifest, Runfiles.getRootRelativePath(artifact), artifact);
+      checker.put(manifest, artifact.getRootRelativePath(), artifact);
     }
 
     // Add conditional artifacts (only included if they appear in a pruning manifest).
@@ -513,7 +500,7 @@
     // workspace.
     private boolean sawWorkspaceName;
 
-    ManifestBuilder(
+    public ManifestBuilder(
         PathFragment workspaceName, boolean legacyExternalRunfiles) {
       this.manifest = new HashMap<>();
       this.workspaceName = workspaceName;
@@ -524,27 +511,19 @@
     /**
      * Adds a map under the workspaceName.
      */
-    void addUnderWorkspace(
+    public void addUnderWorkspace(
         Map<PathFragment, Artifact> inputManifest, ConflictChecker checker) {
       for (Map.Entry<PathFragment, Artifact> entry : inputManifest.entrySet()) {
         PathFragment path = entry.getKey();
-        if (isUnderWorkspace(entry.getValue())) {
-          // For empty files (e.g., ../repo/__init__.py), we don't have an artifact so
-          // isUnderWorkspace returns true.
-          checker.put(manifest, workspaceName.getRelative(path).normalize(), entry.getValue());
+        if (isUnderWorkspace(path)) {
           sawWorkspaceName = true;
+          checker.put(manifest, workspaceName.getRelative(path), entry.getValue());
         } else {
-          PathFragment root = entry.getValue().getRoot().getPath().asFragment();
           if (legacyExternalRunfiles) {
-            // external/repo
-            PathFragment repoDir = root.subFragment(root.segmentCount() - 2, root.segmentCount());
-            // Turn ../repo/foo info wsname/external/repo/foo.
-            checker.put(
-                manifest, workspaceName.getRelative(repoDir).getRelative(path).normalize(),
-                entry.getValue());
+            checker.put(manifest, workspaceName.getRelative(path), entry.getValue());
           }
-          checker.put(
-              manifest, workspaceName.getRelative(entry.getKey()).normalize(), entry.getValue());
+          // Always add the non-legacy .runfiles/repo/whatever path.
+          checker.put(manifest, getExternalPath(path), entry.getValue());
         }
       }
     }
@@ -572,20 +551,17 @@
       return manifest;
     }
 
+    private PathFragment getExternalPath(PathFragment path) {
+      return checkForWorkspace(path.relativeTo(Label.EXTERNAL_PACKAGE_NAME));
+    }
+
     private PathFragment checkForWorkspace(PathFragment path) {
-      sawWorkspaceName = sawWorkspaceName
-          || path.getSegment(0).equals(workspaceName.getPathString());
+      sawWorkspaceName = sawWorkspaceName || path.getSegment(0).equals(workspaceName);
       return path;
     }
 
-    private static boolean isUnderWorkspace(@Nullable Artifact artifact) {
-      if (artifact == null) {
-        return true;
-      }
-      PathFragment root = artifact.getRoot().getPath().asFragment();
-      return root.segmentCount() < 2
-          || !root.getSegment(root.segmentCount() - 2).equals(
-              Label.EXTERNAL_PACKAGE_NAME.getPathString());
+    private static boolean isUnderWorkspace(PathFragment path) {
+      return !path.startsWith(Label.EXTERNAL_PACKAGE_NAME);
     }
   }