Search in sources :

Example 1 with RuleVisibility

use of com.google.devtools.build.lib.packages.RuleVisibility 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

Label (com.google.devtools.build.lib.cmdline.Label)1 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)1 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)1 BuildFileNotFoundException (com.google.devtools.build.lib.packages.BuildFileNotFoundException)1 InvalidPackageNameException (com.google.devtools.build.lib.packages.InvalidPackageNameException)1 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)1 Package (com.google.devtools.build.lib.packages.Package)1 RuleVisibility (com.google.devtools.build.lib.packages.RuleVisibility)1 Statement (com.google.devtools.build.lib.syntax.Statement)1 Path (com.google.devtools.build.lib.vfs.Path)1 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)1 SkyKey (com.google.devtools.build.skyframe.SkyKey)1