Repository: bazelbuild/bazel
Issue:  bad error messaging when using the deprecated  `PACKAGE_NAME` or `REPOSITORY_NAME`
Labels: P1
bad error messaging

### Description of the problem / feature request:

Instead of a nice deprecation warning / error, I got this ambiguous error: `builtin variable 'REPOSITORY_NAME' is referenced before assignment.`.

### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Use `REPOSITORY_NAME` or `PACKAGE_NAME`. With master too. 

### What operating system are you running Bazel on?

Linux
### What's the output of `bazel info release`?

release 0.22.0- (@non-git)

### If `bazel info release` returns "development version" or "(@non-git)", tell us how you built Bazel.

Official archlinux release 

###  Have you found anything relevant by searching the web?
[This deprecation thread](https://github.com/bazelbuild/bazel/issues/5827)

Issue comments:
Not team-Starlark?
I'm tagging @c-parsons since `{s,}he` has opened the original deprecation issue.   
It's probably my fault, sorry about that.
I'm looking at it.
@laurentlb I guess you did too much refactoring on https://github.com/bazelbuild/bazel/commit/2b3ad18eec4e6b968d840777249c809e478b2e13 and removed the warning with the flag by mistake. 

Pull request review comments:

Patch that resolved the issue:
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 98e7d5b69ba855..a246ee5e93ecc8 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 @@ Object doEval(Environment env) throws EvalException {
     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,37 @@ public Kind kind() {
     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);
   }


Test patch:
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 354fb0c58be645..239fcba012ec0c 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
@@ -240,6 +240,16 @@ public void testPrefixBetweenRules2() throws Exception {
         "output file 'a' of rule 'kiwi2' conflicts " + "with output file 'a/b' of rule 'kiwi1'");
   }
 
+  @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')");
@@ -252,6 +262,19 @@ public void testPackageNameFunction() throws Exception {
     assertThat(Sets.newHashSet(pkg.getTargets(Rule.class)).size()).isSameAs(1);
   }
 
+  @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 =