diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java b/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java index 82ea54975275ab..527cccc8099550 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java @@ -21,6 +21,7 @@ import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.vfs.PathFragment; import java.util.Objects; +import java.util.Optional; import javax.annotation.concurrent.Immutable; /** @@ -177,6 +178,65 @@ public String getCanonicalForm() { return repository.getCanonicalForm() + "//" + getPackageFragment(); } + /** + * Returns an absolutely unambiguous canonical form for this package in label form. Parsing this + * string in any environment, even when subject to repository mapping, should identify the same + * package. + */ + public String getUnambiguousCanonicalForm() { + return String.format("@@%s//%s", getRepository().getName(), getPackageFragment()); + } + + /** + * Returns a label representation for this package that is suitable for display. The returned + * string is as simple as possible while referencing the current package when parsed in the + * context of the main repository. + * + * @param mainOrOtherRepoMapping the {@link RepositoryMapping} of the main repository or, if that + * is not available to the caller, the {@link RepositoryMapping} of + * the current package. The former will yield more readable output. + * @return