From 3c30fbcf3abbe4a46ef804b35f7676cea695cb1c Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 30 May 2022 08:23:28 -0700 Subject: [PATCH] Get rid of the '@' in RepositoryName This CL is purely an internal refactor with no visible behavior changes. In preparation for https://github.com/bazelbuild/bazel/issues/15593 PiperOrigin-RevId: 451879164 Change-Id: I3642455cc324891f23ed58f4c1187ebb9ccaba78 --- .../build/lib/cmdline/LabelParser.java | 2 +- .../build/lib/cmdline/RepositoryName.java | 31 ++++++++----------- .../workspace/WorkspaceBlackBoxTest.java | 4 +-- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/LabelParser.java b/src/main/java/com/google/devtools/build/lib/cmdline/LabelParser.java index 19cc1822b008e9..8af98a08ae9220 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/LabelParser.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/LabelParser.java @@ -119,7 +119,7 @@ static Parts parse(String rawLabel) throws LabelSyntaxException { private static void validateRepoName(@Nullable String repo) throws LabelSyntaxException { if (repo != null) { - RepositoryName.validate('@' + repo); + RepositoryName.validate(repo); } } diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java b/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java index f23a0b5d83c90c..a647563a4b10bf 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/RepositoryName.java @@ -33,15 +33,11 @@ public final class RepositoryName { @SerializationConstant - public static final RepositoryName BAZEL_TOOLS = new RepositoryName("@bazel_tools"); + public static final RepositoryName BAZEL_TOOLS = new RepositoryName("bazel_tools"); - @SerializationConstant - public static final RepositoryName LOCAL_CONFIG_PLATFORM = - new RepositoryName("@local_config_platform"); - - @SerializationConstant public static final RepositoryName MAIN = new RepositoryName("@"); + @SerializationConstant public static final RepositoryName MAIN = new RepositoryName(""); - private static final Pattern VALID_REPO_NAME = Pattern.compile("@[\\w\\-.]*"); + private static final Pattern VALID_REPO_NAME = Pattern.compile("[\\w\\-.]*"); private static final LoadingCache repositoryNameCache = Caffeine.newBuilder() @@ -62,9 +58,8 @@ public static RepositoryName create(String name) throws LabelSyntaxException { if (name.isEmpty()) { return MAIN; } - // TODO(b/200024947): Get rid of the '@' in the #name field. try { - return repositoryNameCache.get('@' + name); + return repositoryNameCache.get(name); } catch (CompletionException e) { Throwables.propagateIfPossible(e.getCause(), LabelSyntaxException.class); throw e; @@ -82,7 +77,7 @@ public static RepositoryName createUnvalidated(String name) { // reference equality instead of #equals(). return MAIN; } - return repositoryNameCache.get("@" + name); + return repositoryNameCache.get(name); } /** @@ -140,27 +135,27 @@ private RepositoryName(String name) { * message is sanitized. */ static void validate(String name) throws LabelSyntaxException { - if (name.isEmpty() || name.equals("@")) { + if (name.isEmpty()) { return; } // Some special cases for more user-friendly error messages. - if (name.equals("@.") || name.equals("@..")) { + if (name.equals(".") || name.equals("..")) { throw LabelParser.syntaxErrorf( - "invalid repository name '%s': repo names are not allowed to be '%s'", name, name); + "invalid repository name '@%s': repo names are not allowed to be '@%s'", name, name); } if (!VALID_REPO_NAME.matcher(name).matches()) { throw LabelParser.syntaxErrorf( - "invalid repository name '%s': repo names may contain only A-Z, a-z, 0-9, '-', '_' and" + "invalid repository name '@%s': repo names may contain only A-Z, a-z, 0-9, '-', '_' and" + " '.'", StringUtilities.sanitizeControlChars(name)); } } - /** Returns the repository name without the leading "{@literal @}". */ + /** Returns the bare repository name without the leading "{@literal @}". */ public String getName() { - return name.substring(1); + return name; } /** @@ -184,12 +179,12 @@ public String getOwnerRepoIfNotVisible() { /** Returns if this is the main repository, that is, {@link #getName} is empty. */ public boolean isMain() { - return name.equals("@"); + return name.isEmpty(); } /** Returns the repository name, with leading "{@literal @}". */ public String getNameWithAt() { - return name; + return '@' + name; } /** diff --git a/src/test/java/com/google/devtools/build/lib/blackbox/tests/workspace/WorkspaceBlackBoxTest.java b/src/test/java/com/google/devtools/build/lib/blackbox/tests/workspace/WorkspaceBlackBoxTest.java index 842ae310154435..48fc1f94cc18c2 100644 --- a/src/test/java/com/google/devtools/build/lib/blackbox/tests/workspace/WorkspaceBlackBoxTest.java +++ b/src/test/java/com/google/devtools/build/lib/blackbox/tests/workspace/WorkspaceBlackBoxTest.java @@ -261,7 +261,7 @@ public void testBadRepoName() throws Exception { ProcessResult result = context().bazel().shouldFail().build("//..."); assertThat(result.errString()) .contains( - "invalid repository name '@@a': repo names may contain only " - + "A-Z, a-z, 0-9, '-', '_' and '.'"); + "invalid repository name '@@a': repo names may contain only A-Z, a-z, 0-9, '-', '_' and" + + " '.'"); } }