Fix Label() for @short repository labels
--
PiperOrigin-RevId: 143555554
MOS_MIGRATED_REVID=143555554
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java b/src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java
index 8ef3c91..16a913d 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/LabelValidator.java
@@ -256,7 +256,7 @@
if (absName.startsWith("@")) {
int endOfRepo = absName.indexOf("//");
if (endOfRepo < 0) {
- throw new BadLabelException("invalid fully-qualified label: " + absName);
+ return new PackageAndTarget("", absName.substring(1));
}
absName = absName.substring(endOfRepo);
}
diff --git a/src/test/java/com/google/devtools/build/lib/cmdline/LabelValidatorTest.java b/src/test/java/com/google/devtools/build/lib/cmdline/LabelValidatorTest.java
index c5df5717..fcf887a 100644
--- a/src/test/java/com/google/devtools/build/lib/cmdline/LabelValidatorTest.java
+++ b/src/test/java/com/google/devtools/build/lib/cmdline/LabelValidatorTest.java
@@ -14,12 +14,10 @@
package com.google.devtools.build.lib.cmdline;
-import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
-import static org.junit.Assert.fail;
import com.google.devtools.build.lib.cmdline.LabelValidator.PackageAndTarget;
import org.junit.Test;
@@ -124,13 +122,6 @@
LabelValidator.validateAbsoluteLabel("@repo//foo:bar"));
assertEquals(new PackageAndTarget("foo", "bar"),
LabelValidator.validateAbsoluteLabel("@//foo:bar"));
-
- try {
- LabelValidator.validateAbsoluteLabel("@foo");
- fail("Should not have been able to validate @foo");
- } catch (LabelValidator.BadLabelException expected) {
- assertThat(expected.getMessage()).contains("invalid fully-qualified label");
- }
}
@Test
@@ -159,4 +150,9 @@
assertEquals("//foo:foo", newFooTarget().toString());
assertEquals("//bar:bar", newBarTarget().toString());
}
+
+ @Test
+ public void testSlashlessLabel_infersTargetNameFromRepoName() throws Exception {
+ assertEquals("//:foo", LabelValidator.parseAbsoluteLabel("@foo").toString());
+ }
}