Inline Package#makeNoSuchTargetException
This existed as a hook for subclasses and is no longer necessary.
@@ -592,16 +592,27 @@ public class Package {
return target;
}
- if (!suggestNoSuchTargetCorrections) {
- throw makeNoSuchTargetException(targetName, /*suffix=*/ "");
+ String alternateTargetSuggestion =
+ suggestNoSuchTargetCorrections ? getAlternateTargetSuggestion(targetName) : "";
+ Label label;
+ try {
+ label = Label.create(packageIdentifier, targetName);
+ } catch (LabelSyntaxException e) {
+ throw new IllegalArgumentException(targetName);
+ }
+ String msg =
+ String.format(
+ "target '%s' not declared in package '%s'%s defined by %s",
+ targetName, getName(), alternateTargetSuggestion, filename.asPath().getPathString());
+ throw new NoSuchTargetException(label, msg);
}
+ private String getAlternateTargetSuggestion(String targetName) {
// If there's a file on the disk that's not mentioned in the BUILD file,
// produce a more informative error. NOTE! this code path is only executed
// on failure, which is (relatively) very rare. In the common case no
// stat(2) is executed.
Path filename = getPackageDirectory().getRelative(targetName);
- String suffix;
if (!PathFragment.isNormalized(targetName) || "*".equals(targetName)) {
// Don't check for file existence if the target name is not normalized
// because the error message would be confusing and wrong. If the
@@ -609,10 +620,9 @@ public class Package {
// doesn't mean that "//pkg:foo/bar/." is a valid label.
// Also don't check if the target name is a single * character since
// it's invalid on Windows.
- suffix = "";
+ return "";
} else if (filename.isDirectory()) {
- suffix =
- "; however, a source directory of this name exists. (Perhaps add "
+ return "; however, a source directory of this name exists. (Perhaps add "
+ "'exports_files([\""
+ targetName
+ "\"])' to "
@@ -620,32 +630,15 @@ public class Package {
+ "/BUILD, or define a "
+ "filegroup?)";
} else if (filename.exists()) {
- suffix =
- "; however, a source file of this name exists. (Perhaps add "
+ return "; however, a source file of this name exists. (Perhaps add "
+ "'exports_files([\""
+ targetName
+ "\"])' to "
+ getName()
+ "/BUILD?)";
} else {
- suffix = SpellChecker.didYouMean(targetName, targets.keySet());
- }
-
- throw makeNoSuchTargetException(targetName, suffix);
+ return SpellChecker.didYouMean(targetName, targets.keySet());
}
-
- protected NoSuchTargetException makeNoSuchTargetException(String targetName, String suffix) {
- Label label;
- try {
- label = Label.create(packageIdentifier, targetName);
- } catch (LabelSyntaxException e) {
- throw new IllegalArgumentException(targetName);
- }
- String msg =
- String.format(
- "target '%s' not declared in package '%s'%s defined by %s",
- targetName, getName(), suffix, filename.asPath().getPathString());
- return new NoSuchTargetException(label, msg);
}
/**
