Add new skyframe function to lookup the repository given a path, and use that
to report invalid package references. Fixes #1592.

--
MOS_MIGRATED_REVID=137164164
diff --git a/src/main/java/com/google/devtools/build/lib/packages/ErrorDeterminingRepositoryException.java b/src/main/java/com/google/devtools/build/lib/packages/ErrorDeterminingRepositoryException.java
new file mode 100644
index 0000000..f8659ae
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/packages/ErrorDeterminingRepositoryException.java
@@ -0,0 +1,27 @@
+// Copyright 2016 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.packages;
+
+/** Exception indicating an attempt to access a repository which is not found or does not exist. */
+public class ErrorDeterminingRepositoryException extends NoSuchThingException {
+
+  public ErrorDeterminingRepositoryException(String message) {
+    super(message);
+  }
+
+  public ErrorDeterminingRepositoryException(String message, Throwable cause) {
+    super(message, cause);
+  }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Package.java b/src/main/java/com/google/devtools/build/lib/packages/Package.java
index 54ccc3e..7e1fca4 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/Package.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/Package.java
@@ -15,6 +15,7 @@
 package com.google.devtools.build.lib.packages;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
@@ -475,11 +476,23 @@
     return (Rule) targets.get(targetName);
   }
 
+  /** Returns all rules in the package that match the given rule class. */
+  public Iterable<Rule> getRulesMatchingRuleClass(final String ruleClass) {
+    Iterable<Rule> targets = getTargets(Rule.class);
+    return Iterables.filter(
+        targets,
+        new Predicate<Rule>() {
+          @Override
+          public boolean apply(@Nullable Rule rule) {
+            return rule.getRuleClass().equals(ruleClass);
+          }
+        });
+  }
+
   /**
    * Returns this package's workspace name.
    *
-   * <p>Package-private to encourage callers to get their workspace name from a rule, not a
-   * package.</p>
+   * <p>Package-private to encourage callers to get their workspace name from a rule, not a package.
    */
   public String getWorkspaceName() {
     return workspaceName;