Search in sources :

Example 6 with BuildFileContainsErrorsException

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

the class PackageCacheTest method testParseBrokenPackage.

/**
   * A broken package is one that exists but contains lexer/parser/evaluator errors. The
   * PackageCache only makes one attempt to parse each package once found.
   *
   * <p>Depending on the strictness of the PackageFactory, parsing a broken package may cause a
   * Package object to be returned (possibly missing some rules) or an exception to be thrown. For
   * this test we need that strict behavior.
   *
   * <p>Note: since the PackageCache.setStrictPackageCreation method was deleted (since it wasn't
   * used by any significant clients) creating a "broken" build file got trickier--syntax errors are
   * not enough.  For now, we create an unreadable BUILD file, which will cause an IOException to be
   * thrown. This test seems less valuable than it once did.
   */
@Test
public void testParseBrokenPackage() throws Exception {
    reporter.removeHandler(failFastHandler);
    Path brokenBuildFile = scratch.file("broken/BUILD");
    brokenBuildFile.setReadable(false);
    try {
        getPackage("broken");
        fail();
    } catch (BuildFileContainsErrorsException e) {
        assertThat(e.getMessage()).contains("/workspace/broken/BUILD (Permission denied)");
    }
    eventCollector.clear();
    // Update the BUILD file on disk so "broken" is no longer broken:
    scratch.overwriteFile("broken/BUILD", "# an ok build file");
    //  resets cache of failures
    invalidatePackages();
    Package broken = getPackage("broken");
    assertEquals("broken", broken.getName());
    assertNoEvents();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) Package(com.google.devtools.build.lib.packages.Package) Test(org.junit.Test)

Example 7 with BuildFileContainsErrorsException

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

the class RepositoryFunction method getRule.

/**
   * Uses a remote repository name to fetch the corresponding Rule describing how to get it.
   *
   * <p>This should be the unique entry point for resolving a remote repository function.
   */
@Nullable
public static Rule getRule(String repository, Environment env) throws RepositoryFunctionException, InterruptedException {
    SkyKey packageLookupKey = PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    PackageLookupValue packageLookupValue = (PackageLookupValue) env.getValue(packageLookupKey);
    if (packageLookupValue == null) {
        return null;
    }
    RootedPath workspacePath = packageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
    do {
        WorkspaceFileValue value = (WorkspaceFileValue) env.getValue(workspaceKey);
        if (value == null) {
            return null;
        }
        Package externalPackage = value.getPackage();
        if (externalPackage.containsErrors()) {
            Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
            throw new RepositoryFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Could not load //external package"), Transience.PERSISTENT);
        }
        Rule rule = externalPackage.getRule(repository);
        if (rule != null) {
            return rule;
        }
        workspaceKey = value.next();
    } while (workspaceKey != null);
    throw new RepositoryNotFoundException(repository);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) PackageLookupValue(com.google.devtools.build.lib.skyframe.PackageLookupValue) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) WorkspaceFileValue(com.google.devtools.build.lib.skyframe.WorkspaceFileValue) Package(com.google.devtools.build.lib.packages.Package) Rule(com.google.devtools.build.lib.packages.Rule) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Nullable(javax.annotation.Nullable)

Example 8 with BuildFileContainsErrorsException

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

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

Example 10 with BuildFileContainsErrorsException

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

the class EnvironmentBackedRecursivePackageProvider method getPackage.

@Override
public Package getPackage(ExtendedEventHandler eventHandler, PackageIdentifier packageName) throws NoSuchPackageException, MissingDepException, InterruptedException {
    SkyKey pkgKey = PackageValue.key(packageName);
    PackageValue pkgValue = (PackageValue) env.getValueOrThrow(pkgKey, NoSuchPackageException.class);
    if (pkgValue == null) {
        throw new MissingDepException();
    }
    Package pkg = pkgValue.getPackage();
    if (pkg.containsErrors()) {
        // continue. This gives the framework notification to shut down the build if it should.
        try {
            env.getValueOrThrow(PackageErrorFunction.key(packageName), BuildFileContainsErrorsException.class);
            Preconditions.checkState(env.valuesMissing(), "Should have thrown for %s", packageName);
            throw new MissingDepException();
        } catch (BuildFileContainsErrorsException e) {
        // Expected.
        }
    }
    return pkgValue.getPackage();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Package(com.google.devtools.build.lib.packages.Package)

Aggregations

BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)10 Package (com.google.devtools.build.lib.packages.Package)8 SkyKey (com.google.devtools.build.skyframe.SkyKey)8 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)6 Nullable (javax.annotation.Nullable)5 Label (com.google.devtools.build.lib.cmdline.Label)3 Path (com.google.devtools.build.lib.vfs.Path)3 ImmutableList (com.google.common.collect.ImmutableList)2 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)2 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)2 Rule (com.google.devtools.build.lib.packages.Rule)2 Target (com.google.devtools.build.lib.packages.Target)2 BuildFileAST (com.google.devtools.build.lib.syntax.BuildFileAST)2 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)2 IOException (java.io.IOException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ConfiguredAspect (com.google.devtools.build.lib.analysis.ConfiguredAspect)1 ConfiguredAspectFactory (com.google.devtools.build.lib.analysis.ConfiguredAspectFactory)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 InconsistentAspectOrderException (com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException)1