Update ContainingPackageLookupFunction to properly handle cases where a
path crosses into a local repository and correctly report the
repository-relative package.

Fixes #3553.

Change-Id: Ib912e69d546fb740ef8fe4c426dba30fa7776bda
PiperOrigin-RevId: 167760229
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ContainingPackageLookupFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/ContainingPackageLookupFunction.java
index 68c9ff8..f3ca988 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ContainingPackageLookupFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ContainingPackageLookupFunction.java
@@ -14,6 +14,7 @@
 package com.google.devtools.build.lib.skyframe;
 
 import com.google.devtools.build.lib.cmdline.PackageIdentifier;
+import com.google.devtools.build.lib.skyframe.PackageLookupValue.IncorrectRepositoryReferencePackageLookupValue;
 import com.google.devtools.build.lib.vfs.PathFragment;
 import com.google.devtools.build.skyframe.SkyFunction;
 import com.google.devtools.build.skyframe.SkyKey;
@@ -38,6 +39,16 @@
       return ContainingPackageLookupValue.withContainingPackage(dir, pkgLookupValue.getRoot());
     }
 
+    // Does the requested package cross into a sub-repository, which we should report via the
+    // correct package identifier?
+    if (pkgLookupValue instanceof IncorrectRepositoryReferencePackageLookupValue) {
+      IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
+          (IncorrectRepositoryReferencePackageLookupValue) pkgLookupValue;
+      PackageIdentifier correctPackageIdentifier =
+          incorrectPackageLookupValue.getCorrectedPackageIdentifier();
+      return env.getValue(ContainingPackageLookupValue.key(correctPackageIdentifier));
+    }
+
     PathFragment parentDir = dir.getPackageFragment().getParentDirectory();
     if (parentDir == null) {
       return ContainingPackageLookupValue.NONE;