Search in sources :

Example 1 with AspectCreationException

use of com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException in project bazel by bazelbuild.

the class ConfiguredTargetFunction method compute.

@Override
public SkyValue compute(SkyKey key, Environment env) throws ConfiguredTargetFunctionException, InterruptedException {
    SkyframeBuildView view = buildViewProvider.getSkyframeBuildView();
    NestedSetBuilder<Package> transitivePackages = NestedSetBuilder.stableOrder();
    NestedSetBuilder<Label> transitiveLoadingRootCauses = NestedSetBuilder.stableOrder();
    ConfiguredTargetKey configuredTargetKey = (ConfiguredTargetKey) key.argument();
    LabelAndConfiguration lc = LabelAndConfiguration.of(configuredTargetKey.getLabel(), configuredTargetKey.getConfiguration());
    BuildConfiguration configuration = lc.getConfiguration();
    PackageValue packageValue = (PackageValue) env.getValue(PackageValue.key(lc.getLabel().getPackageIdentifier()));
    if (packageValue == null) {
        return null;
    }
    // TODO(ulfjack): This tries to match the logic in TransitiveTargetFunction /
    // TargetMarkerFunction. Maybe we can merge the two?
    Package pkg = packageValue.getPackage();
    Target target;
    try {
        target = pkg.getTarget(lc.getLabel().getName());
    } catch (NoSuchTargetException e) {
        throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(e.getMessage()));
    }
    if (pkg.containsErrors()) {
        transitiveLoadingRootCauses.add(lc.getLabel());
    }
    transitivePackages.add(pkg);
    // null).
    if (!target.isConfigurable()) {
        configuration = null;
    }
    // associates the error with the dep, which is misleading.
    if (useDynamicConfigurations(configuration) && configuration.trimConfigurations() && env.getValue(TransitiveTargetValue.key(lc.getLabel())) == null) {
        return null;
    }
    TargetAndConfiguration ctgValue = new TargetAndConfiguration(target, configuration);
    SkyframeDependencyResolver resolver = view.createDependencyResolver(env);
    // TODO(janakr): this acquire() call may tie up this thread indefinitely, reducing the
    // parallelism of Skyframe. This is a strict improvement over the prior state of the code, in
    // which we ran with #processors threads, but ideally we would call #tryAcquire here, and if we
    // failed, would exit this SkyFunction and restart it when permits were available.
    cpuBoundSemaphore.acquire();
    try {
        // Get the configuration targets that trigger this rule's configurable attributes.
        ImmutableMap<Label, ConfigMatchingProvider> configConditions = getConfigConditions(ctgValue.getTarget(), env, resolver, ctgValue, transitivePackages, transitiveLoadingRootCauses);
        if (env.valuesMissing()) {
            return null;
        }
        // attributes.
        if (!transitiveLoadingRootCauses.isEmpty() && configConditions != NO_CONFIG_CONDITIONS) {
            throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(transitiveLoadingRootCauses.build()));
        }
        OrderedSetMultimap<Attribute, ConfiguredTarget> depValueMap = computeDependencies(env, resolver, ctgValue, ImmutableList.<Aspect>of(), configConditions, ruleClassProvider, view.getHostConfiguration(configuration), transitivePackages, transitiveLoadingRootCauses);
        if (env.valuesMissing()) {
            return null;
        }
        if (!transitiveLoadingRootCauses.isEmpty()) {
            throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(transitiveLoadingRootCauses.build()));
        }
        Preconditions.checkNotNull(depValueMap);
        ConfiguredTargetValue ans = createConfiguredTarget(view, env, target, configuration, depValueMap, configConditions, transitivePackages);
        return ans;
    } catch (DependencyEvaluationException e) {
        if (e.getCause() instanceof ConfiguredValueCreationException) {
            throw new ConfiguredTargetFunctionException((ConfiguredValueCreationException) e.getCause());
        } else if (e.getCause() instanceof InconsistentAspectOrderException) {
            InconsistentAspectOrderException cause = (InconsistentAspectOrderException) e.getCause();
            throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(cause.getMessage(), target.getLabel()));
        } else {
            // Cast to InvalidConfigurationException as a consistency check. If you add any
            // DependencyEvaluationException constructors, you may need to change this code, too.
            InvalidConfigurationException cause = (InvalidConfigurationException) e.getCause();
            throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(cause.getMessage(), target.getLabel()));
        }
    } catch (AspectCreationException e) {
        // getAnalysisRootCause may be null if the analysis of the aspect itself failed.
        Label analysisRootCause = target.getLabel();
        if (e.getAnalysisRootCause() != null) {
            analysisRootCause = e.getAnalysisRootCause();
        }
        throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(e.getMessage(), analysisRootCause));
    } finally {
        cpuBoundSemaphore.release();
    }
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) LabelAndConfiguration(com.google.devtools.build.lib.analysis.LabelAndConfiguration) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) InconsistentAspectOrderException(com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException) AspectCreationException(com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException) TargetAndConfiguration(com.google.devtools.build.lib.analysis.TargetAndConfiguration) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Package(com.google.devtools.build.lib.packages.Package)

Example 2 with AspectCreationException

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

use of com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException in project bazel by bazelbuild.

the class ConfiguredTargetFunction method resolveAspectDependencies.

/**
   * Given a list of {@link Dependency} objects, returns a multimap from the {@link SkyKey} of the
   * dependency to the {@link ConfiguredAspect} instances that should be merged into it.
   *
   * <p>Returns null if the required aspects are not computed yet.
   */
@Nullable
private static OrderedSetMultimap<SkyKey, ConfiguredAspect> resolveAspectDependencies(Environment env, Map<SkyKey, ConfiguredTarget> configuredTargetMap, Iterable<Dependency> deps, NestedSetBuilder<Package> transitivePackages) throws AspectCreationException, InterruptedException {
    OrderedSetMultimap<SkyKey, ConfiguredAspect> result = OrderedSetMultimap.create();
    Set<SkyKey> allAspectKeys = new HashSet<>();
    for (Dependency dep : deps) {
        allAspectKeys.addAll(getAspectKeys(dep).values());
    }
    Map<SkyKey, ValueOrException2<AspectCreationException, NoSuchThingException>> depAspects = env.getValuesOrThrow(allAspectKeys, AspectCreationException.class, NoSuchThingException.class);
    for (Dependency dep : deps) {
        SkyKey depKey = TO_KEYS.apply(dep);
        // twice.
        if (result.containsKey(depKey)) {
            continue;
        }
        Map<AspectDescriptor, SkyKey> aspectToKeys = getAspectKeys(dep);
        ConfiguredTarget depConfiguredTarget = configuredTargetMap.get(depKey);
        for (AspectDeps depAspect : dep.getAspects().getVisibleAspects()) {
            SkyKey aspectKey = aspectToKeys.get(depAspect.getAspect());
            AspectValue aspectValue;
            try {
                // TODO(ulfjack): Catch all thrown AspectCreationException and NoSuchThingException
                // instances and merge them into a single Exception to get full root cause data.
                aspectValue = (AspectValue) depAspects.get(aspectKey).get();
            } catch (NoSuchThingException e) {
                throw new AspectCreationException(String.format("Evaluation of aspect %s on %s failed: %s", depAspect.getAspect().getAspectClass().getName(), dep.getLabel(), e.toString()));
            }
            if (aspectValue == null) {
                // Dependent aspect has either not been computed yet or is in error.
                return null;
            }
            if (!aspectMatchesConfiguredTarget(depConfiguredTarget, aspectValue.getAspect())) {
                continue;
            }
            result.put(depKey, aspectValue.getConfiguredAspect());
            transitivePackages.addTransitive(aspectValue.getTransitivePackages());
        }
    }
    return result;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ConfiguredAspect(com.google.devtools.build.lib.analysis.ConfiguredAspect) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) Dependency(com.google.devtools.build.lib.analysis.Dependency) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) AspectCreationException(com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) AspectDeps(com.google.devtools.build.lib.analysis.AspectCollection.AspectDeps) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Aggregations

AspectCreationException (com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException)3 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 MergedConfiguredTarget (com.google.devtools.build.lib.analysis.MergedConfiguredTarget)2 Label (com.google.devtools.build.lib.cmdline.Label)2 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)2 SkyKey (com.google.devtools.build.skyframe.SkyKey)2 Nullable (javax.annotation.Nullable)2 AspectDeps (com.google.devtools.build.lib.analysis.AspectCollection.AspectDeps)1 ConfiguredAspect (com.google.devtools.build.lib.analysis.ConfiguredAspect)1 Dependency (com.google.devtools.build.lib.analysis.Dependency)1 InconsistentAspectOrderException (com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException)1 LabelAndConfiguration (com.google.devtools.build.lib.analysis.LabelAndConfiguration)1 TargetAndConfiguration (com.google.devtools.build.lib.analysis.TargetAndConfiguration)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 ConfigMatchingProvider (com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider)1 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)1 Attribute (com.google.devtools.build.lib.packages.Attribute)1 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)1 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)1 Package (com.google.devtools.build.lib.packages.Package)1