diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java index 04a7fd03c66a1f..b12952ee89966c 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java @@ -54,10 +54,9 @@ public class ModuleFileGlobals { /** * A valid module name must: 1) begin with a lowercase letter; 2) end with a lowercase letter or a - * digit; 3) be at least 2 characters long; 4) contain only lowercase letters, digits, or one of - * '._-'. + * digit; 3) contain only lowercase letters, digits, or one of * '._-'. */ - private static final Pattern VALID_MODULE_NAME = Pattern.compile("[a-z][a-z0-9._-]*[a-z0-9]"); + private static final Pattern VALID_MODULE_NAME = Pattern.compile("[a-z]([a-z0-9._-]*[a-z0-9])?"); private boolean moduleCalled = false; private final boolean ignoreDevDeps; @@ -114,8 +113,7 @@ static void validateModuleName(String moduleName) throws EvalException { throw Starlark.errorf( "invalid module name '%s': valid names must 1) only contain lowercase letters (a-z)," + " digits (0-9), dots (.), hyphens (-), and underscores (_); 2) begin with a" - + " lowercase letter; 3) end with a lowercase letter or digit; 4) be at least two" - + " characters long.", + + " lowercase letter; 3) end with a lowercase letter or digit.", moduleName); } } @@ -136,7 +134,7 @@ static void validateModuleName(String moduleName) throws EvalException { + " in, if it's not going to be depended on by another module). A valid module" + " name must: 1) only contain lowercase letters (a-z), digits (0-9), dots (.)," + " hyphens (-), and underscores (_); 2) begin with a lowercase letter; 3) end" - + " with a lowercase letter or digit; 4) be at least two characters long.", + + " with a lowercase letter or digit.", named = true, positional = false, defaultValue = "''"), diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java index dcb28bf5a66bc1..ea1e30b0683a9f 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java @@ -642,9 +642,9 @@ public void validateModuleName() throws Exception { ModuleFileGlobals.validateModuleName("a3"); ModuleFileGlobals.validateModuleName("a.e"); ModuleFileGlobals.validateModuleName("a.-_e"); + ModuleFileGlobals.validateModuleName("a"); assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("")); - assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("f")); assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("fooBar")); assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("_foo")); assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("foo#bar")); @@ -655,24 +655,24 @@ public void validateModuleName() throws Exception { public void badModuleName_module() throws Exception { scratch.file( rootDirectory.getRelative("MODULE.bazel").getPathString(), - "module(name='f',version='0.1')"); + "module(name='f.',version='0.1')"); reporter.removeHandler(failFastHandler); // expect failures evaluator.evaluate(ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext); - assertContainsEvent("invalid module name 'f'"); + assertContainsEvent("invalid module name 'f.'"); } @Test public void badModuleName_bazelDep() throws Exception { scratch.file( rootDirectory.getRelative("MODULE.bazel").getPathString(), - "bazel_dep(name='f',version='0.1')"); + "bazel_dep(name='f.',version='0.1')"); reporter.removeHandler(failFastHandler); // expect failures evaluator.evaluate(ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext); - assertContainsEvent("invalid module name 'f'"); + assertContainsEvent("invalid module name 'f.'"); } @Test