Search in sources :

Example 16 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class PreciseAspectResolver method computeBuildFileDependencies.

@Override
public Set<Label> computeBuildFileDependencies(Package pkg, BuildFileDependencyMode mode) throws InterruptedException {
    Set<Label> result = new LinkedHashSet<>();
    result.addAll(mode.getDependencies(pkg));
    Set<PackageIdentifier> dependentPackages = new LinkedHashSet<>();
    // Iterate over all rules...
    for (Target target : pkg.getTargets()) {
        if (!(target instanceof Rule)) {
            continue;
        }
        // ...figure out which direct dependencies can possibly have aspects attached to them...
        Multimap<Attribute, Label> depsWithPossibleAspects = ((Rule) target).getTransitions(new BinaryPredicate<Rule, Attribute>() {

            @Override
            public boolean apply(@Nullable Rule rule, Attribute attribute) {
                for (Aspect aspectWithParameters : attribute.getAspects(rule)) {
                    if (!aspectWithParameters.getDefinition().getAttributes().isEmpty()) {
                        return true;
                    }
                }
                return false;
            }
        });
        // ...and add the package of the aspect.
        for (Label depLabel : depsWithPossibleAspects.values()) {
            dependentPackages.add(depLabel.getPackageIdentifier());
        }
    }
    // Then add all the subinclude labels of the packages thus found to the result.
    for (PackageIdentifier packageIdentifier : dependentPackages) {
        try {
            result.add(Label.create(packageIdentifier, "BUILD"));
            Package dependentPackage = packageProvider.getPackage(eventHandler, packageIdentifier);
            result.addAll(mode.getDependencies(dependentPackage));
        } catch (NoSuchPackageException e) {
        // If the package is not found, just add its BUILD file, which is already done above.
        // Hopefully this error is not raised when there is a syntax error in a subincluded file
        // or something.
        } catch (LabelSyntaxException e) {
            throw new IllegalStateException(e);
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) Aspect(com.google.devtools.build.lib.packages.Aspect) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Rule(com.google.devtools.build.lib.packages.Rule) Package(com.google.devtools.build.lib.packages.Package)

Example 17 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class ProcessPackageDirectory method getPackageExistenceAndSubdirDeps.

/**
   * Examines {@code rootedPath} to see if it is the location of a package, and to see if it has any
   * subdirectory children that should also be examined. Returns a {@link
   * ProcessPackageDirectoryResult}, or {@code null} if required dependencies were missing.
   */
@Nullable
ProcessPackageDirectoryResult getPackageExistenceAndSubdirDeps(RootedPath rootedPath, RepositoryName repositoryName, SkyFunction.Environment env, Set<PathFragment> excludedPaths) throws InterruptedException {
    PathFragment rootRelativePath = rootedPath.getRelativePath();
    SkyKey fileKey = FileValue.key(rootedPath);
    FileValue fileValue;
    try {
        fileValue = (FileValue) env.getValueOrThrow(fileKey, InconsistentFilesystemException.class, FileSymlinkException.class, IOException.class);
    } catch (InconsistentFilesystemException | FileSymlinkException | IOException e) {
        return reportErrorAndReturn("Failed to get information about path", e, rootRelativePath, env.getListener());
    }
    if (env.valuesMissing()) {
        return null;
    }
    if (!fileValue.isDirectory()) {
        return ProcessPackageDirectoryResult.EMPTY_RESULT;
    }
    PackageIdentifier packageId = PackageIdentifier.create(repositoryName, rootRelativePath);
    if ((packageId.getRepository().isDefault() || packageId.getRepository().isMain()) && fileValue.isSymlink() && fileValue.getUnresolvedLinkTarget().startsWith(directories.getOutputBase().asFragment())) {
        // somewhere in the directory tree manually.
        return ProcessPackageDirectoryResult.EMPTY_RESULT;
    }
    SkyKey pkgLookupKey = PackageLookupValue.key(packageId);
    SkyKey dirListingKey = DirectoryListingValue.key(rootedPath);
    Map<SkyKey, ValueOrException4<NoSuchPackageException, InconsistentFilesystemException, FileSymlinkException, IOException>> pkgLookupAndDirectoryListingDeps = env.getValuesOrThrow(ImmutableList.of(pkgLookupKey, dirListingKey), NoSuchPackageException.class, InconsistentFilesystemException.class, FileSymlinkException.class, IOException.class);
    if (env.valuesMissing()) {
        return null;
    }
    PackageLookupValue pkgLookupValue;
    try {
        pkgLookupValue = (PackageLookupValue) Preconditions.checkNotNull(pkgLookupAndDirectoryListingDeps.get(pkgLookupKey).get(), "%s %s %s", rootedPath, repositoryName, pkgLookupKey);
    } catch (NoSuchPackageException | InconsistentFilesystemException e) {
        return reportErrorAndReturn("Failed to load package", e, rootRelativePath, env.getListener());
    } catch (IOException | FileSymlinkException e) {
        throw new IllegalStateException(e);
    }
    DirectoryListingValue dirListingValue;
    try {
        dirListingValue = (DirectoryListingValue) Preconditions.checkNotNull(pkgLookupAndDirectoryListingDeps.get(dirListingKey).get(), "%s %s %s", rootedPath, repositoryName, dirListingKey);
    } catch (InconsistentFilesystemException | IOException e) {
        return reportErrorAndReturn("Failed to list directory contents", e, rootRelativePath, env.getListener());
    } catch (FileSymlinkException e) {
        // be able to avoid throwing there but throw here.
        throw new IllegalStateException("Symlink cycle found after not being found for \"" + rootedPath + "\"");
    } catch (NoSuchPackageException e) {
        throw new IllegalStateException(e);
    }
    return new ProcessPackageDirectoryResult(pkgLookupValue.packageExists() && pkgLookupValue.getRoot().equals(rootedPath.getRoot()), getSubdirDeps(dirListingValue, rootedPath, repositoryName, excludedPaths));
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ValueOrException4(com.google.devtools.build.skyframe.ValueOrException4) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Nullable(javax.annotation.Nullable)

Example 18 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class PackageFunction method getExternalPackage.

/**
   * Adds a dependency on the WORKSPACE file, representing it as a special type of package.
   *
   * @throws PackageFunctionException if there is an error computing the workspace file or adding
   *     its rules to the //external package.
   */
private SkyValue getExternalPackage(Environment env, Path packageLookupPath) throws PackageFunctionException, InterruptedException {
    RootedPath workspacePath = RootedPath.toRootedPath(packageLookupPath, Label.EXTERNAL_PACKAGE_FILE_NAME);
    SkyKey workspaceKey = ExternalPackageFunction.key(workspacePath);
    PackageValue workspace = null;
    try {
        // This may throw a NoSuchPackageException if the WORKSPACE file was malformed or had other
        // problems. Since this function can't add much context, we silently bubble it up.
        workspace = (PackageValue) env.getValueOrThrow(workspaceKey, IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class, EvalException.class, SkylarkImportFailedException.class);
    } catch (IOException | FileSymlinkException | InconsistentFilesystemException | EvalException | SkylarkImportFailedException e) {
        throw new PackageFunctionException(new NoSuchPackageException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Error encountered while dealing with the WORKSPACE file: " + e.getMessage()), Transience.PERSISTENT);
    }
    if (workspace == null) {
        return null;
    }
    Package pkg = workspace.getPackage();
    Event.replayEventsOn(env.getListener(), pkg.getEvents());
    packageFactory.afterDoneLoadingPackage(pkg);
    return new PackageValue(pkg);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) Package(com.google.devtools.build.lib.packages.Package) RootedPath(com.google.devtools.build.lib.vfs.RootedPath)

Example 19 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class PackageFunction method fetchImportsFromBuildFile.

/**
   * Fetch the skylark loads for this BUILD file. If any of them haven't been computed yet,
   * returns null.
   */
@Nullable
static SkylarkImportResult fetchImportsFromBuildFile(Path buildFilePath, PackageIdentifier packageId, BuildFileAST buildFileAST, Environment env, SkylarkImportLookupFunction skylarkImportLookupFunctionForInlining) throws NoSuchPackageException, InterruptedException {
    Preconditions.checkArgument(!packageId.getRepository().isDefault());
    ImmutableList<SkylarkImport> imports = buildFileAST.getImports();
    Map<String, Extension> importMap = Maps.newHashMapWithExpectedSize(imports.size());
    ImmutableList.Builder<SkylarkFileDependency> fileDependencies = ImmutableList.builder();
    ImmutableMap<String, Label> importPathMap;
    // Find the labels corresponding to the load statements.
    Label labelForCurrBuildFile;
    try {
        labelForCurrBuildFile = Label.create(packageId, "BUILD");
    } catch (LabelSyntaxException e) {
        // Shouldn't happen; the Label is well-formed by construction.
        throw new IllegalStateException(e);
    }
    try {
        importPathMap = SkylarkImportLookupFunction.findLabelsForLoadStatements(imports, labelForCurrBuildFile, env);
        if (importPathMap == null) {
            return null;
        }
    } catch (SkylarkImportFailedException e) {
        throw new BuildFileContainsErrorsException(packageId, e.getMessage());
    }
    // Look up and load the imports.
    ImmutableCollection<Label> importLabels = importPathMap.values();
    List<SkyKey> importLookupKeys = Lists.newArrayListWithExpectedSize(importLabels.size());
    boolean inWorkspace = buildFilePath.getBaseName().endsWith("WORKSPACE");
    for (Label importLabel : importLabels) {
        importLookupKeys.add(SkylarkImportLookupValue.key(importLabel, inWorkspace));
    }
    Map<SkyKey, SkyValue> skylarkImportMap = Maps.newHashMapWithExpectedSize(importPathMap.size());
    boolean valuesMissing = false;
    try {
        if (skylarkImportLookupFunctionForInlining == null) {
            // Not inlining
            Map<SkyKey, ValueOrException2<SkylarkImportFailedException, InconsistentFilesystemException>> skylarkLookupResults = env.getValuesOrThrow(importLookupKeys, SkylarkImportFailedException.class, InconsistentFilesystemException.class);
            valuesMissing = env.valuesMissing();
            for (Map.Entry<SkyKey, ValueOrException2<SkylarkImportFailedException, InconsistentFilesystemException>> entry : skylarkLookupResults.entrySet()) {
                // Fetching the value will raise any deferred exceptions
                skylarkImportMap.put(entry.getKey(), entry.getValue().get());
            }
        } else {
            // Inlining calls to SkylarkImportLookupFunction
            LinkedHashMap<Label, SkylarkImportLookupValue> alreadyVisitedImports = Maps.newLinkedHashMapWithExpectedSize(importLookupKeys.size());
            for (SkyKey importLookupKey : importLookupKeys) {
                SkyValue skyValue = skylarkImportLookupFunctionForInlining.computeWithInlineCalls(importLookupKey, env, alreadyVisitedImports);
                if (skyValue == null) {
                    Preconditions.checkState(env.valuesMissing(), "no skylark import value for %s", importLookupKey);
                    // We continue making inline calls even if some requested values are missing, to
                    // maximize the number of dependent (non-inlined) SkyFunctions that are requested, thus
                    // avoiding a quadratic number of restarts.
                    valuesMissing = true;
                } else {
                    skylarkImportMap.put(importLookupKey, skyValue);
                }
            }
        }
    } catch (SkylarkImportFailedException e) {
        throw new BuildFileContainsErrorsException(packageId, e.getMessage());
    } catch (InconsistentFilesystemException e) {
        throw new NoSuchPackageException(packageId, e.getMessage(), e);
    }
    if (valuesMissing) {
        // Some imports are unavailable.
        return null;
    }
    // Process the loaded imports.
    for (Entry<String, Label> importEntry : importPathMap.entrySet()) {
        String importString = importEntry.getKey();
        Label importLabel = importEntry.getValue();
        SkyKey keyForLabel = SkylarkImportLookupValue.key(importLabel, inWorkspace);
        SkylarkImportLookupValue importLookupValue = (SkylarkImportLookupValue) skylarkImportMap.get(keyForLabel);
        importMap.put(importString, importLookupValue.getEnvironmentExtension());
        fileDependencies.add(importLookupValue.getDependency());
    }
    return new SkylarkImportResult(importMap, transitiveClosureOfLabels(fileDependencies.build()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Label(com.google.devtools.build.lib.cmdline.Label) SkyValue(com.google.devtools.build.skyframe.SkyValue) SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) SkylarkImport(com.google.devtools.build.lib.syntax.SkylarkImport) Extension(com.google.devtools.build.lib.syntax.Environment.Extension) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Nullable(javax.annotation.Nullable)

Example 20 with NoSuchPackageException

use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.

the class PackageFunction method compute.

@Override
public SkyValue compute(SkyKey key, Environment env) throws PackageFunctionException, InterruptedException {
    PackageIdentifier packageId = (PackageIdentifier) key.argument();
    SkyKey packageLookupKey = PackageLookupValue.key(packageId);
    PackageLookupValue packageLookupValue;
    try {
        packageLookupValue = (PackageLookupValue) env.getValueOrThrow(packageLookupKey, BuildFileNotFoundException.class, InconsistentFilesystemException.class);
    } catch (BuildFileNotFoundException e) {
        throw new PackageFunctionException(e, Transience.PERSISTENT);
    } catch (InconsistentFilesystemException e) {
        // This error is not transient from the perspective of the PackageFunction.
        throw new PackageFunctionException(new NoSuchPackageException(packageId, e.getMessage(), e), Transience.PERSISTENT);
    }
    if (packageLookupValue == null) {
        return null;
    }
    if (!packageLookupValue.packageExists()) {
        switch(packageLookupValue.getErrorReason()) {
            case NO_BUILD_FILE:
            case DELETED_PACKAGE:
                throw new PackageFunctionException(new BuildFileNotFoundException(packageId, packageLookupValue.getErrorMsg()), Transience.PERSISTENT);
            case INVALID_PACKAGE_NAME:
                throw new PackageFunctionException(new InvalidPackageNameException(packageId, packageLookupValue.getErrorMsg()), Transience.PERSISTENT);
            default:
                // We should never get here.
                throw new IllegalStateException();
        }
    }
    if (packageId.equals(Label.EXTERNAL_PACKAGE_IDENTIFIER)) {
        return getExternalPackage(env, packageLookupValue.getRoot());
    }
    WorkspaceNameValue workspaceNameValue = (WorkspaceNameValue) env.getValue(WorkspaceNameValue.key());
    if (workspaceNameValue == null) {
        return null;
    }
    String workspaceName = workspaceNameValue.maybeGetName();
    if (workspaceName == null) {
        throw new PackageFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER), Transience.PERSISTENT);
    }
    RootedPath buildFileRootedPath = packageLookupValue.getRootedPath(packageId);
    FileValue buildFileValue = null;
    Path buildFilePath = buildFileRootedPath.asPath();
    String replacementContents = null;
    if (!isDefaultsPackage(packageId)) {
        buildFileValue = getBuildFileValue(env, buildFileRootedPath);
        if (buildFileValue == null) {
            return null;
        }
    } else {
        replacementContents = PrecomputedValue.DEFAULTS_PACKAGE_CONTENTS.get(env);
        if (replacementContents == null) {
            return null;
        }
    }
    RuleVisibility defaultVisibility = PrecomputedValue.DEFAULT_VISIBILITY.get(env);
    if (defaultVisibility == null) {
        return null;
    }
    SkyKey astLookupKey = ASTFileLookupValue.key(preludeLabel);
    ASTFileLookupValue astLookupValue = null;
    try {
        astLookupValue = (ASTFileLookupValue) env.getValueOrThrow(astLookupKey, ErrorReadingSkylarkExtensionException.class, InconsistentFilesystemException.class);
    } catch (ErrorReadingSkylarkExtensionException | InconsistentFilesystemException e) {
        throw new PackageFunctionException(new NoSuchPackageException(packageId, "Error encountered while reading the prelude file: " + e.getMessage()), Transience.PERSISTENT);
    }
    if (astLookupValue == null) {
        return null;
    }
    // The prelude file doesn't have to exist. If not, we substitute an empty statement list.
    List<Statement> preludeStatements = astLookupValue.lookupSuccessful() ? astLookupValue.getAST().getStatements() : ImmutableList.<Statement>of();
    CacheEntryWithGlobDeps<Package.Builder> packageBuilderAndGlobDeps = loadPackage(workspaceName, replacementContents, packageId, buildFilePath, buildFileValue, defaultVisibility, preludeStatements, packageLookupValue.getRoot(), env);
    if (packageBuilderAndGlobDeps == null) {
        return null;
    }
    Package.Builder pkgBuilder = packageBuilderAndGlobDeps.value;
    pkgBuilder.buildPartial();
    try {
        // Since the Skyframe dependencies we request below in
        // markDependenciesAndPropagateFilesystemExceptions are requested independently of
        // the ones requested here in
        // handleLabelsCrossingSubpackagesAndPropagateInconsistentFilesystemExceptions, we don't
        // bother checking for missing values and instead piggyback on the env.missingValues() call
        // for the former. This avoids a Skyframe restart.
        handleLabelsCrossingSubpackagesAndPropagateInconsistentFilesystemExceptions(packageLookupValue.getRoot(), packageId, pkgBuilder, env);
    } catch (InternalInconsistentFilesystemException e) {
        packageFunctionCache.invalidate(packageId);
        throw new PackageFunctionException(e.toNoSuchPackageException(), e.isTransient() ? Transience.TRANSIENT : Transience.PERSISTENT);
    }
    Set<SkyKey> globKeys = packageBuilderAndGlobDeps.globDepKeys;
    Map<Label, Path> subincludes = pkgBuilder.getSubincludes();
    boolean packageShouldBeConsideredInError;
    try {
        packageShouldBeConsideredInError = markDependenciesAndPropagateFilesystemExceptions(env, globKeys, subincludes, packageId, pkgBuilder.containsErrors());
    } catch (InternalInconsistentFilesystemException e) {
        packageFunctionCache.invalidate(packageId);
        throw new PackageFunctionException(e.toNoSuchPackageException(), e.isTransient() ? Transience.TRANSIENT : Transience.PERSISTENT);
    }
    if (env.valuesMissing()) {
        return null;
    }
    Event.replayEventsOn(env.getListener(), pkgBuilder.getEvents());
    if (packageShouldBeConsideredInError) {
        pkgBuilder.setContainsErrors();
    }
    Package pkg = pkgBuilder.finishBuild();
    // We know this SkyFunction will not be called again, so we can remove the cache entry.
    packageFunctionCache.invalidate(packageId);
    packageFactory.afterDoneLoadingPackage(pkg);
    return new PackageValue(pkg);
}
Also used : BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) InvalidPackageNameException(com.google.devtools.build.lib.packages.InvalidPackageNameException) Label(com.google.devtools.build.lib.cmdline.Label) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) SkyKey(com.google.devtools.build.skyframe.SkyKey) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) Statement(com.google.devtools.build.lib.syntax.Statement) RuleVisibility(com.google.devtools.build.lib.packages.RuleVisibility) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Package(com.google.devtools.build.lib.packages.Package)

Aggregations

NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)27 SkyKey (com.google.devtools.build.skyframe.SkyKey)21 Package (com.google.devtools.build.lib.packages.Package)13 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)11 Label (com.google.devtools.build.lib.cmdline.Label)10 Target (com.google.devtools.build.lib.packages.Target)8 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)7 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)6 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)6 Nullable (javax.annotation.Nullable)6 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)5 IOException (java.io.IOException)5 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 SkyValue (com.google.devtools.build.skyframe.SkyValue)4 ValueOrException2 (com.google.devtools.build.skyframe.ValueOrException2)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Path (com.google.devtools.build.lib.vfs.Path)3 Map (java.util.Map)3 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)2 Attribute (com.google.devtools.build.lib.packages.Attribute)2