Search in sources :

Example 21 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.

the class SkyQueryEnvironment method getBuildFiles.

@ThreadSafe
@Override
public Set<Target> getBuildFiles(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(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);
                if (buildFiles) {
                    // Also add the BUILD file of the subinclude.
                    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) LinkedHashSet(java.util.LinkedHashSet) CompactHashSet(com.google.devtools.build.lib.collect.CompactHashSet) HashSet(java.util.HashSet) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 22 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.

the class HeaderDiscovery method discoverInputsFromDotdFiles.

/**
   * Returns a collection with additional input artifacts relevant to the action by reading the
   * dynamically-discovered dependency information from the .d file after the action has run.
   *
   * <p>Artifacts are considered inputs but not "mandatory" inputs.
   *
   * @throws ActionExecutionException iff the .d is missing (when required), malformed, or has
   *     unresolvable included artifacts.
   */
@VisibleForTesting
@ThreadCompatible
public NestedSet<Artifact> discoverInputsFromDotdFiles(Path execRoot, ArtifactResolver artifactResolver) throws ActionExecutionException {
    NestedSetBuilder<Artifact> inputs = NestedSetBuilder.stableOrder();
    if (dotdFile == null) {
        return inputs.build();
    }
    List<Path> systemIncludePrefixes = permittedSystemIncludePrefixes;
    // Check inclusions.
    IncludeProblems problems = new IncludeProblems();
    for (Path execPath : depSet.getDependencies()) {
        PathFragment execPathFragment = execPath.asFragment();
        if (execPathFragment.isAbsolute()) {
            // Absolute includes from system paths are ignored.
            if (FileSystemUtils.startsWithAny(execPath, systemIncludePrefixes)) {
                continue;
            }
            // the build with an error.
            if (execPath.startsWith(execRoot)) {
                // funky but tolerable path
                execPathFragment = execPath.relativeTo(execRoot);
            } else {
                problems.add(execPathFragment.getPathString());
                continue;
            }
        }
        Artifact artifact = allowedDerivedInputsMap.get(execPathFragment);
        if (artifact == null) {
            try {
                RepositoryName repository = PackageIdentifier.discoverFromExecPath(execPathFragment, false).getRepository();
                artifact = artifactResolver.resolveSourceArtifact(execPathFragment, repository);
            } catch (LabelSyntaxException e) {
                throw new ActionExecutionException(String.format("Could not find the external repository for %s", execPathFragment), e, action, false);
            }
        }
        if (artifact != null) {
            inputs.add(artifact);
            // to the set of actual inputs.
            if (specialInputsHandler != null) {
                inputs.addAll(specialInputsHandler.getInputsForIncludedFile(artifact, artifactResolver));
            }
        } else {
            // Abort if we see files that we can't resolve, likely caused by
            // undeclared includes or illegal include constructs.
            problems.add(execPathFragment.getPathString());
        }
    }
    if (shouldValidateInclusions) {
        problems.assertProblemFree(action, sourceFile);
    }
    return inputs.build();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) ActionExecutionException(com.google.devtools.build.lib.actions.ActionExecutionException) Artifact(com.google.devtools.build.lib.actions.Artifact) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ThreadCompatible(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible)

Example 23 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.

the class CrosstoolConfigurationLoader method getCrosstoolProtoFromCrosstoolFile.

private static CrosstoolProto getCrosstoolProtoFromCrosstoolFile(ConfigurationEnvironment env, Label crosstoolTop) throws IOException, InvalidConfigurationException, InterruptedException {
    final Path path;
    try {
        Package containingPackage = env.getTarget(crosstoolTop.getLocalTargetLabel("BUILD")).getPackage();
        if (containingPackage == null) {
            return null;
        }
        path = env.getPath(containingPackage, CROSSTOOL_CONFIGURATION_FILENAME);
    } catch (LabelSyntaxException e) {
        throw new InvalidConfigurationException(e);
    } catch (NoSuchThingException e) {
        // Handled later
        return null;
    }
    if (path == null || !path.exists()) {
        return null;
    }
    return new CrosstoolProto(path.getDigest(), "CROSSTOOL file " + path.getPathString()) {

        @Override
        public String getContents() throws IOException {
            try (InputStream inputStream = path.getInputStream()) {
                return new String(FileSystemUtils.readContentAsLatin1(inputStream));
            }
        }
    };
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) InputStream(java.io.InputStream) Package(com.google.devtools.build.lib.packages.Package) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)

Example 24 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.

the class JavaOptions method getTranslationLabels.

private Set<Label> getTranslationLabels() {
    Set<Label> result = new LinkedHashSet<>();
    for (String s : translationTargets) {
        try {
            Label label = Label.parseAbsolute(s);
            result.add(label);
        } catch (LabelSyntaxException e) {
        // We ignore this exception here - it will cause an error message at a later time.
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Label(com.google.devtools.build.lib.cmdline.Label)

Example 25 with LabelSyntaxException

use of com.google.devtools.build.lib.cmdline.LabelSyntaxException in project bazel by bazelbuild.

the class LocalRepositoryLookupFunction method maybeCheckWorkspaceForRepository.

/**
   * Checks whether the directory exists and is a workspace root. Returns {@link Optional#absent()}
   * if Skyframe needs to re-run, {@link Optional#of(LocalRepositoryLookupValue)} otherwise.
   */
private Optional<LocalRepositoryLookupValue> maybeCheckWorkspaceForRepository(Environment env, final RootedPath directory) throws InterruptedException, LocalRepositoryLookupFunctionException {
    // Look up the main WORKSPACE file by the external package, to find all repositories.
    PackageLookupValue externalPackageLookupValue;
    try {
        externalPackageLookupValue = (PackageLookupValue) env.getValueOrThrow(PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
        if (externalPackageLookupValue == null) {
            return Optional.absent();
        }
    } catch (BuildFileNotFoundException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("BuildFileNotFoundException while loading the //external package", e), Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("InconsistentFilesystemException while loading the //external package", e), Transience.PERSISTENT);
    }
    RootedPath workspacePath = externalPackageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
    do {
        WorkspaceFileValue value;
        try {
            value = (WorkspaceFileValue) env.getValueOrThrow(workspaceKey, PackageFunctionException.class, NameConflictException.class);
            if (value == null) {
                return Optional.absent();
            }
        } catch (PackageFunctionException e) {
            // TODO(jcater): When WFF is rewritten to not throw a PFE, update this.
            throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("PackageFunctionException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
        } catch (NameConflictException e) {
            throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("NameConflictException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
        }
        Package externalPackage = value.getPackage();
        if (externalPackage.containsErrors()) {
            Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
        }
        // Find all local_repository rules in the WORKSPACE, and check if any have a "path" attribute
        // the same as the requested directory.
        Iterable<Rule> localRepositories = externalPackage.getRulesMatchingRuleClass(LocalRepositoryRule.NAME);
        Rule rule = Iterables.find(localRepositories, new Predicate<Rule>() {

            @Override
            public boolean apply(@Nullable Rule rule) {
                AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
                PathFragment pathAttr = new PathFragment(mapper.get("path", Type.STRING));
                return directory.getRelativePath().equals(pathAttr);
            }
        }, null);
        if (rule != null) {
            try {
                return Optional.of(LocalRepositoryLookupValue.success(RepositoryName.create("@" + rule.getName())));
            } catch (LabelSyntaxException e) {
                // validated.
                throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("LabelSyntaxException while creating the repository name from the rule " + rule.getName(), e), Transience.PERSISTENT);
            }
        }
        workspaceKey = value.next();
    // TODO(bazel-team): This loop can be quadratic in the number of load() statements, consider
    // rewriting or unrolling.
    } while (workspaceKey != null);
    return Optional.of(LocalRepositoryLookupValue.notFound());
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) ErrorDeterminingRepositoryException(com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) PackageFunctionException(com.google.devtools.build.lib.skyframe.PackageFunction.PackageFunctionException) NameConflictException(com.google.devtools.build.lib.packages.Package.NameConflictException) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Package(com.google.devtools.build.lib.packages.Package) LocalRepositoryRule(com.google.devtools.build.lib.rules.repository.LocalRepositoryRule) Rule(com.google.devtools.build.lib.packages.Rule) AggregatingAttributeMapper(com.google.devtools.build.lib.packages.AggregatingAttributeMapper)

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