use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.
the class Package method makeNoSuchTargetException.
protected NoSuchTargetException makeNoSuchTargetException(String targetName, String suffix) {
Label label;
try {
label = createLabel(targetName);
} catch (LabelSyntaxException e) {
throw new IllegalArgumentException(targetName);
}
String msg = String.format("target '%s' not declared in package '%s'%s defined by %s", targetName, name, suffix, filename);
return new NoSuchTargetException(label, msg);
}
use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.
the class BlazeQueryEnvironment method getBuildFiles.
// TODO(bazel-team): rename this to getDependentFiles when all implementations
// of QueryEnvironment is fixed.
@Override
public Set<Target> getBuildFiles(final QueryExpression caller, Set<Target> nodes, boolean buildFiles, boolean subincludes, boolean loads) throws QueryException {
Set<Target> dependentFiles = new LinkedHashSet<>();
Set<Package> seenPackages = new HashSet<>();
// Keep track of seen labels, to avoid adding a fake subinclude label that also exists as a
// real target.
Set<Label> seenLabels = new HashSet<>();
// extensions) for package "pkg", to "buildfiles".
for (Target x : nodes) {
Package pkg = x.getPackage();
if (seenPackages.add(pkg)) {
if (buildFiles) {
addIfUniqueLabel(getNode(pkg.getBuildFile()), seenLabels, dependentFiles);
}
List<Label> extensions = new ArrayList<>();
if (subincludes) {
extensions.addAll(pkg.getSubincludeLabels());
}
if (loads) {
extensions.addAll(pkg.getSkylarkFileDependencies());
}
for (Label subinclude : extensions) {
addIfUniqueLabel(getSubincludeTarget(subinclude, pkg), seenLabels, dependentFiles);
// Also add the BUILD file of the subinclude.
if (buildFiles) {
try {
addIfUniqueLabel(getSubincludeTarget(subinclude.getLocalTargetLabel("BUILD"), pkg), seenLabels, dependentFiles);
} catch (LabelSyntaxException e) {
throw new AssertionError("BUILD should always parse as a target name", e);
}
}
}
}
}
return dependentFiles;
}
use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.
the class WorkspaceFactory method newRuleFunction.
/**
* Returns a function-value implementing the build rule "ruleClass" (e.g. cc_library) in the
* specified package context.
*/
private static BuiltinFunction newRuleFunction(final RuleFactory ruleFactory, final String ruleClassName, final boolean allowOverride) {
return new BuiltinFunction(ruleClassName, FunctionSignature.KWARGS, BuiltinFunction.USE_AST_ENV) {
public Object invoke(Map<String, Object> kwargs, FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
try {
Package.Builder builder = PackageFactory.getContext(env, ast).pkgBuilder;
if (!allowOverride && kwargs.containsKey("name") && builder.targets.containsKey(kwargs.get("name"))) {
throw new EvalException(ast.getLocation(), "Cannot redefine repository after any load statement in the WORKSPACE file" + " (for repository '" + kwargs.get("name") + "')");
}
RuleClass ruleClass = ruleFactory.getRuleClass(ruleClassName);
RuleClass bindRuleClass = ruleFactory.getRuleClass("bind");
Rule rule = builder.externalPackageData().createAndAddRepositoryRule(builder, ruleClass, bindRuleClass, kwargs, ast);
if (!isLegalWorkspaceName(rule.getName())) {
throw new EvalException(ast.getLocation(), rule + "'s name field must be a legal workspace name");
}
} catch (RuleFactory.InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) {
throw new EvalException(ast.getLocation(), e.getMessage());
}
return NONE;
}
};
}
use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.
the class PackageSpecification method fromString.
/**
* Parses the provided {@link String} into a {@link PackageSpecification}.
*
* <p>The {@link String} must have one of the following forms:
*
* <ul>
* <li>The full name of a single package, without repository qualification, prefixed with "//"
* (e.g. "//foo/bar"). This results in a {@link PackageSpecification} that contains exactly
* the named package.
* <li>The full name of a single package, without repository qualification, prefixed with "//",
* and suffixed with "/..." (e.g. "//foo/bar/...") This results in a {@link
* PackageSpecification} that contains all transitive subpackages of that package, inclusive.
* <li>Exactly "//...". This results in a {@link PackageSpecification} that contains all packages.
* </ul>
*
* <p>If and only if the {@link String} is one of the first two forms, the returned {@link
* PackageSpecification} is specific to the provided {@link RepositoryName}. Note that it is not
* possible to construct a repository-specific {@link PackageSpecification} for all transitive
* subpackages of the root package (i.e. a repository-specific "//...").
*
* <p>Throws {@link InvalidPackageSpecificationException} if the {@link String} cannot be parsed.
*/
public static PackageSpecification fromString(RepositoryName repositoryName, String spec) throws InvalidPackageSpecificationException {
String result = spec;
boolean allBeneath = false;
if (result.endsWith(ALL_BENEATH_SUFFIX)) {
allBeneath = true;
result = result.substring(0, result.length() - ALL_BENEATH_SUFFIX.length());
if (result.equals("/")) {
// spec was "//...".
return AllPackages.EVERYTHING;
}
}
if (!spec.startsWith("//")) {
throw new InvalidPackageSpecificationException(String.format("invalid package name '%s': must start with '//'", spec));
}
PackageIdentifier packageId;
try {
packageId = PackageIdentifier.parse(result);
} catch (LabelSyntaxException e) {
throw new InvalidPackageSpecificationException(String.format("invalid package name '%s': %s", spec, e.getMessage()));
}
Verify.verify(packageId.getRepository().isDefault());
PackageIdentifier packageIdForSpecifiedRepository = PackageIdentifier.create(repositoryName, packageId.getPackageFragment());
return allBeneath ? new AllPackagesBeneath(packageIdForSpecifiedRepository) : new SinglePackage(packageIdForSpecifiedRepository);
}
use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.
the class SkylarkImports method create.
/**
* Creates and syntactically validates a {@link SkylarkImports} instance from a string.
* <p>
* There four syntactic import variants: Absolute paths, relative paths, absolute labels, and
* relative labels
*
* @throws SkylarkImportSyntaxException if the string is not a valid Skylark import.
*/
public static SkylarkImport create(String importString) throws SkylarkImportSyntaxException {
if (importString.startsWith("//") || importString.startsWith("@")) {
// Absolute label.
Label importLabel;
try {
importLabel = Label.parseAbsolute(importString, false);
} catch (LabelSyntaxException e) {
throw new SkylarkImportSyntaxException(INVALID_LABEL_PREFIX + e.getMessage());
}
String targetName = importLabel.getName();
if (!targetName.endsWith(".bzl")) {
throw new SkylarkImportSyntaxException(MUST_HAVE_BZL_EXT_MSG);
}
PackageIdentifier packageId = importLabel.getPackageIdentifier();
if (packageId.equals(Label.EXTERNAL_PACKAGE_IDENTIFIER)) {
throw new SkylarkImportSyntaxException(EXTERNAL_PKG_NOT_ALLOWED_MSG);
}
return new AbsoluteLabelImport(importString, importLabel);
} else if (importString.startsWith("/")) {
// Absolute path.
if (importString.endsWith(".bzl")) {
throw new SkylarkImportSyntaxException(INVALID_PATH_SYNTAX);
}
PathFragment importPath = new PathFragment(importString + ".bzl");
return new AbsolutePathImport(importString, importPath);
} else if (importString.startsWith(":")) {
// Relative label. We require that relative labels use an explicit ':' prefix to distinguish
// them from relative paths, which have a different semantics.
String importTarget = importString.substring(1);
if (!importTarget.endsWith(".bzl")) {
throw new SkylarkImportSyntaxException(MUST_HAVE_BZL_EXT_MSG);
}
String maybeErrMsg = LabelValidator.validateTargetName(importTarget);
if (maybeErrMsg != null) {
// Null indicates successful target validation.
throw new SkylarkImportSyntaxException(INVALID_TARGET_PREFIX + maybeErrMsg);
}
return new RelativeLabelImport(importString, importTarget);
} else {
// Relative path.
if (importString.endsWith(".bzl") || importString.contains("/")) {
throw new SkylarkImportSyntaxException(INVALID_PATH_SYNTAX);
}
String importTarget = importString + ".bzl";
String maybeErrMsg = LabelValidator.validateTargetName(importTarget);
if (maybeErrMsg != null) {
// Null indicates successful target validation.
throw new SkylarkImportSyntaxException(INVALID_FILENAME_PREFIX + maybeErrMsg);
}
return new RelativePathImport(importString, importTarget);
}
}
Aggregations