aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Justine Tunney <jart@google.com>2016-11-22 00:46:24 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2016-11-22 14:15:00 +0000
commit61affe77f9f5e5e7fcbbba7d8c2bcf8fbab776d5 (patch)
treecbfdf9b4eb30a6b7eb636f1d33e432559ee485e0 /src
parent7d32e255fb47bc2bb4e9ec1644ab272005758336 (diff)
Allow labels like @foo//:foo to be shortened @foo
Fixes #1924 RELNOTES: Labels like "@foo//:foo" can now be written as "@foo" in build files -- MOS_MIGRATED_REVID=139848658
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/cmdline/Label.java7
-rw-r--r--src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java16
2 files changed, 18 insertions, 5 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
index b5a699023e..753909c130 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java
@@ -81,6 +81,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
* <pre>
* //foo/bar
* //foo/bar:quux
+ * {@literal @}foo
* {@literal @}foo//bar
* {@literal @}foo//bar:baz
* </pre>
@@ -96,6 +97,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
* <pre>
* //foo/bar
* //foo/bar:quux
+ * {@literal @}foo
* {@literal @}foo//bar
* {@literal @}foo//bar:baz
* </pre>
@@ -110,6 +112,9 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
if (packageStartPos > 0) {
repo = absName.substring(0, packageStartPos);
absName = absName.substring(packageStartPos);
+ } else if (absName.startsWith("@")) {
+ repo = absName;
+ absName = "//:" + absName.substring(1);
}
try {
LabelValidator.PackageAndTarget labelParts = LabelValidator.parseAbsoluteLabel(absName);
@@ -324,7 +329,7 @@ public final class Label implements Comparable<Label>, Serializable, SkylarkPrin
public String getPackageName() {
return packageIdentifier.getPackageFragment().getPathString();
}
-
+
/**
* Returns the execution root for the workspace, relative to the execroot (e.g., for label
* {@code @repo//pkg:b}, it will returns {@code external/repo/pkg} and for label {@code //pkg:a},
diff --git a/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java b/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
index 47dd0b1d67..3766a76755 100644
--- a/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
+++ b/src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
@@ -21,13 +21,11 @@ import static org.junit.Assert.fail;
import com.google.devtools.build.lib.testutil.TestUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
-
+import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
-import java.util.regex.Pattern;
-
/**
* Tests for {@link Label}.
*/
@@ -55,6 +53,12 @@ public class LabelTest {
assertEquals("", l.getPackageName());
assertEquals("bar", l.getName());
}
+ {
+ Label l = Label.parseAbsolute("@foo");
+ assertEquals("@foo", l.getPackageIdentifier().getRepository().getName());
+ assertEquals("", l.getPackageName());
+ assertEquals("foo", l.getName());
+ }
}
private static String parseCommandLine(String label, String prefix) throws LabelSyntaxException {
@@ -249,6 +253,10 @@ public class LabelTest {
Label l = Label.parseAbsolute("//foo/bar");
assertEquals("//foo/bar:bar", l.toString());
}
+ {
+ Label l = Label.parseAbsolute("@foo");
+ assertEquals("@foo//:foo", l.toString());
+ }
}
@Test
@@ -434,7 +442,7 @@ public class LabelTest {
"invalid repository name 'foo': workspace names must start with '@'");
}
}
-
+
@Test
public void testGetWorkspaceRoot() throws Exception {
Label label = Label.parseAbsolute("//bar/baz");