Restore deprecation error for {PACKAGE,REPOSITORY}_NAME
This code has been removed by mistake in
2b3ad18eec4e6b968d840777249c809e478b2e13.
This commit also restores its test.
Fixes https://github.com/bazelbuild/bazel/issues/7331
Closes #7491.
PiperOrigin-RevId: 235037738
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Identifier.java b/src/main/java/com/google/devtools/build/lib/syntax/Identifier.java
index 98e7d5b..29858d5 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Identifier.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Identifier.java
@@ -107,9 +107,12 @@
if (result == null) {
// Since Scope was set, we know that the variable is defined in the scope.
// However, the assignment was not yet executed.
- throw new EvalException(
- getLocation(),
- scope.getQualifier() + " variable '" + name + "' is referenced before assignment.");
+ EvalException e = getSpecialException();
+ throw e != null
+ ? e
+ : new EvalException(
+ getLocation(),
+ scope.getQualifier() + " variable '" + name + "' is referenced before assignment.");
}
return result;
@@ -125,11 +128,35 @@
return Kind.IDENTIFIER;
}
+ /** Exception to provide a better error message for using PACKAGE_NAME or REPOSITORY_NAME. */
+ private EvalException getSpecialException() {
+ if (name.equals("PACKAGE_NAME")) {
+ return new EvalException(
+ getLocation(),
+ "The value 'PACKAGE_NAME' has been removed in favor of 'package_name()', "
+ + "please use the latter ("
+ + "https://docs.bazel.build/versions/master/skylark/lib/native.html#package_name). ");
+ }
+ if (name.equals("REPOSITORY_NAME")) {
+ return new EvalException(
+ getLocation(),
+ "The value 'REPOSITORY_NAME' has been removed in favor of 'repository_name()', "
+ + "please use the latter ("
+ + "https://docs.bazel.build/versions/master/skylark/lib/native.html#repository_name).");
+ }
+ return null;
+ }
+
EvalException createInvalidIdentifierException(Set<String> symbols) {
if (name.equals("$error$")) {
return new EvalException(getLocation(), "contains syntax error(s)", true);
}
+ EvalException e = getSpecialException();
+ if (e != null) {
+ return e;
+ }
+
String suggestion = SpellChecker.didYouMean(name, symbols);
return new EvalException(getLocation(), "name '" + name + "' is not defined" + suggestion);
}
diff --git a/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java b/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
index 354fb0c..c6c74db 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
@@ -241,6 +241,14 @@
}
@Test
+ public void testPackageConstantIsForbidden() throws Exception {
+ events.setFailFast(false);
+ Path buildFile = scratch.file("/pina/BUILD", "cc_library(name=PACKAGE_NAME + '-colada')");
+ packages.createPackage("pina", RootedPath.toRootedPath(root, buildFile));
+ events.assertContainsError("The value 'PACKAGE_NAME' has been removed");
+ }
+
+ @Test
public void testPackageNameFunction() throws Exception {
Path buildFile = scratch.file("/pina/BUILD", "cc_library(name=package_name() + '-colada')");
@@ -253,6 +261,19 @@
}
@Test
+ public void testPackageConstantInExternalRepositoryIsForbidden() throws Exception {
+ events.setFailFast(false);
+ Path buildFile =
+ scratch.file(
+ "/external/a/b/BUILD", "genrule(name='c', srcs=[], outs=['ao'], cmd=REPOSITORY_NAME)");
+ packages.createPackage(
+ PackageIdentifier.create("@a", PathFragment.create("b")),
+ RootedPath.toRootedPath(root, buildFile),
+ events.reporter());
+ events.assertContainsError("The value 'REPOSITORY_NAME' has been removed");
+ }
+
+ @Test
public void testPackageFunctionInExternalRepository() throws Exception {
Path buildFile =
scratch.file(