Search in sources :

Example 1 with LabelSyntaxException

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);
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label)

Example 2 with LabelSyntaxException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Target(com.google.devtools.build.lib.packages.Target) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label) ArrayList(java.util.ArrayList) Package(com.google.devtools.build.lib.packages.Package) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with LabelSyntaxException

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;
        }
    };
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) EvalException(com.google.devtools.build.lib.syntax.EvalException) NameConflictException(com.google.devtools.build.lib.packages.Package.NameConflictException) BuiltinFunction(com.google.devtools.build.lib.syntax.BuiltinFunction) Environment(com.google.devtools.build.lib.syntax.Environment) FuncallExpression(com.google.devtools.build.lib.syntax.FuncallExpression) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) InvalidRuleException(com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException)

Example 4 with LabelSyntaxException

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);
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier)

Example 5 with LabelSyntaxException

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);
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment)

Aggregations

LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)28 Label (com.google.devtools.build.lib.cmdline.Label)17 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)10 SkyKey (com.google.devtools.build.skyframe.SkyKey)8 Path (com.google.devtools.build.lib.vfs.Path)7 EvalException (com.google.devtools.build.lib.syntax.EvalException)6 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)6 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)5 Package (com.google.devtools.build.lib.packages.Package)5 Target (com.google.devtools.build.lib.packages.Target)4 LinkedHashSet (java.util.LinkedHashSet)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)3 NameConflictException (com.google.devtools.build.lib.packages.Package.NameConflictException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)2 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)2 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)2