Search in sources :

Example 1 with SkylarkImportFailedException

use of com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException in project bazel by bazelbuild.

the class ToplevelSkylarkAspectFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws LoadSkylarkAspectFunctionException, InterruptedException {
    SkylarkAspectLoadingKey aspectLoadingKey = (SkylarkAspectLoadingKey) skyKey.argument();
    String skylarkValueName = aspectLoadingKey.getSkylarkValueName();
    SkylarkImport extensionFile = aspectLoadingKey.getSkylarkImport();
    // Find label corresponding to skylark file, if one exists.
    ImmutableMap<String, Label> labelLookupMap;
    try {
        labelLookupMap = SkylarkImportLookupFunction.findLabelsForLoadStatements(ImmutableList.of(extensionFile), Label.parseAbsoluteUnchecked("//:empty"), env);
    } catch (SkylarkImportFailedException e) {
        env.getListener().handle(Event.error(e.getMessage()));
        throw new LoadSkylarkAspectFunctionException(new AspectCreationException(e.getMessage()));
    }
    if (labelLookupMap == null) {
        return null;
    }
    SkylarkAspect skylarkAspect;
    Label extensionFileLabel = Iterables.getOnlyElement(labelLookupMap.values());
    try {
        skylarkAspect = AspectFunction.loadSkylarkAspect(env, extensionFileLabel, skylarkValueName);
        if (skylarkAspect == null) {
            return null;
        }
        if (!skylarkAspect.getParamAttributes().isEmpty()) {
            throw new AspectCreationException("Cannot instantiate parameterized aspect " + skylarkAspect.getName() + " at the top level.", extensionFileLabel);
        }
    } catch (AspectCreationException e) {
        throw new LoadSkylarkAspectFunctionException(e);
    }
    SkyKey aspectKey = ActionLookupValue.key(AspectValue.createAspectKey(aspectLoadingKey.getTargetLabel(), aspectLoadingKey.getTargetConfiguration(), new AspectDescriptor(skylarkAspect.getAspectClass(), AspectParameters.EMPTY), aspectLoadingKey.getAspectConfiguration()));
    return env.getValue(aspectKey);
}
Also used : SkylarkImport(com.google.devtools.build.lib.syntax.SkylarkImport) AspectCreationException(com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException) SkyKey(com.google.devtools.build.skyframe.SkyKey) SkylarkAspectLoadingKey(com.google.devtools.build.lib.skyframe.AspectValue.SkylarkAspectLoadingKey) SkylarkAspect(com.google.devtools.build.lib.packages.SkylarkAspect) Label(com.google.devtools.build.lib.cmdline.Label) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Nullable(javax.annotation.Nullable)

Example 2 with SkylarkImportFailedException

use of com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException 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 3 with SkylarkImportFailedException

use of com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException 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 4 with SkylarkImportFailedException

use of com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException in project bazel by bazelbuild.

the class AspectFunction method loadSkylarkAspect.

/**
   * Load Skylark aspect from an extension file. Is to be called from a SkyFunction.
   *
   * @return {@code null} if dependencies cannot be satisfied.
   */
@Nullable
static SkylarkAspect loadSkylarkAspect(Environment env, Label extensionLabel, String skylarkValueName) throws AspectCreationException, InterruptedException {
    SkyKey importFileKey = SkylarkImportLookupValue.key(extensionLabel, false);
    try {
        SkylarkImportLookupValue skylarkImportLookupValue = (SkylarkImportLookupValue) env.getValueOrThrow(importFileKey, SkylarkImportFailedException.class);
        if (skylarkImportLookupValue == null) {
            return null;
        }
        Object skylarkValue = skylarkImportLookupValue.getEnvironmentExtension().get(skylarkValueName);
        if (!(skylarkValue instanceof SkylarkAspect)) {
            throw new ConversionException(skylarkValueName + " from " + extensionLabel.toString() + " is not an aspect");
        }
        return (SkylarkAspect) skylarkValue;
    } catch (SkylarkImportFailedException | ConversionException e) {
        env.getListener().handle(Event.error(e.getMessage()));
        throw new AspectCreationException(e.getMessage());
    }
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ConversionException(com.google.devtools.build.lib.syntax.Type.ConversionException) SkylarkAspect(com.google.devtools.build.lib.packages.SkylarkAspect) SkylarkImportFailedException(com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException) Nullable(javax.annotation.Nullable)

Aggregations

SkylarkImportFailedException (com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException)4 SkyKey (com.google.devtools.build.skyframe.SkyKey)4 Nullable (javax.annotation.Nullable)3 Label (com.google.devtools.build.lib.cmdline.Label)2 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)2 SkylarkAspect (com.google.devtools.build.lib.packages.SkylarkAspect)2 SkylarkImport (com.google.devtools.build.lib.syntax.SkylarkImport)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)1 BuildFileContainsErrorsException (com.google.devtools.build.lib.packages.BuildFileContainsErrorsException)1 Package (com.google.devtools.build.lib.packages.Package)1 AspectCreationException (com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException)1 SkylarkAspectLoadingKey (com.google.devtools.build.lib.skyframe.AspectValue.SkylarkAspectLoadingKey)1 Extension (com.google.devtools.build.lib.syntax.Environment.Extension)1 EvalException (com.google.devtools.build.lib.syntax.EvalException)1 ConversionException (com.google.devtools.build.lib.syntax.Type.ConversionException)1 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)1 SkyValue (com.google.devtools.build.skyframe.SkyValue)1