Search in sources :

Example 1 with ValueOrException2

use of com.google.devtools.build.skyframe.ValueOrException2 in project bazel by bazelbuild.

the class TransitiveBaseTraversalFunction method getStrictLabelAspectKeys.

/**
   * Return an Iterable of SkyKeys corresponding to the Aspect-related dependencies of target.
   *
   * <p>This method may return a precise set of aspect keys, but may need to request additional
   * dependencies from the env to do so.
   */
private Iterable<SkyKey> getStrictLabelAspectKeys(Target target, Map<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> depMap, Environment env) throws InterruptedException {
    List<SkyKey> depKeys = Lists.newArrayList();
    if (target instanceof Rule) {
        Map<Label, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> labelDepMap = new HashMap<>(depMap.size());
        for (Entry<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> entry : depMap.entrySet()) {
            labelDepMap.put((Label) entry.getKey().argument(), entry.getValue());
        }
        Multimap<Attribute, Label> transitions = ((Rule) target).getTransitions(DependencyFilter.NO_NODEP_ATTRIBUTES);
        for (Entry<Attribute, Label> entry : transitions.entries()) {
            ValueOrException2<NoSuchPackageException, NoSuchTargetException> value = labelDepMap.get(entry.getValue());
            for (Label label : getAspectLabels((Rule) target, entry.getKey(), entry.getValue(), value, env)) {
                depKeys.add(getKey(label));
            }
        }
    }
    return depKeys;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) HashMap(java.util.HashMap) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Rule(com.google.devtools.build.lib.packages.Rule)

Example 2 with ValueOrException2

use of com.google.devtools.build.skyframe.ValueOrException2 in project bazel by bazelbuild.

the class TransitiveBaseTraversalFunction method compute.

@Override
public SkyValue compute(SkyKey key, Environment env) throws TransitiveBaseTraversalFunctionException, InterruptedException {
    Label label = (Label) key.argument();
    LoadTargetResults loadTargetResults;
    try {
        loadTargetResults = loadTarget(env, label);
    } catch (NoSuchTargetException e) {
        throw new TransitiveBaseTraversalFunctionException(e);
    } catch (NoSuchPackageException e) {
        throw new TransitiveBaseTraversalFunctionException(e);
    }
    LoadTargetResultsType loadTargetResultsType = loadTargetResults.getType();
    if (loadTargetResultsType.equals(LoadTargetResultsType.VALUES_MISSING)) {
        return null;
    }
    Preconditions.checkState(loadTargetResultsType.equals(LoadTargetResultsType.TARGET_AND_ERROR_IF_ANY), loadTargetResultsType);
    TargetAndErrorIfAny targetAndErrorIfAny = (TargetAndErrorIfAny) loadTargetResults;
    TProcessedTargets processedTargets = processTarget(label, targetAndErrorIfAny);
    // Process deps from attributes.
    Iterable<SkyKey> labelDepKeys = getLabelDepKeys(targetAndErrorIfAny.getTarget());
    Map<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> depMap = env.getValuesOrThrow(labelDepKeys, NoSuchPackageException.class, NoSuchTargetException.class);
    processDeps(processedTargets, env.getListener(), targetAndErrorIfAny, depMap.entrySet());
    if (env.valuesMissing()) {
        return null;
    }
    // Process deps from aspects.
    Iterable<SkyKey> labelAspectKeys = getStrictLabelAspectKeys(targetAndErrorIfAny.getTarget(), depMap, env);
    Set<Entry<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>>> labelAspectEntries = env.getValuesOrThrow(labelAspectKeys, NoSuchPackageException.class, NoSuchTargetException.class).entrySet();
    processDeps(processedTargets, env.getListener(), targetAndErrorIfAny, labelAspectEntries);
    if (env.valuesMissing()) {
        return null;
    }
    return computeSkyValue(targetAndErrorIfAny, processedTargets);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) Entry(java.util.Map.Entry) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException)

Example 3 with ValueOrException2

use of com.google.devtools.build.skyframe.ValueOrException2 in project bazel by bazelbuild.

the class ActionExecutionFunction method checkInputs.

/**
   * Declare dependency on all known inputs of action. Throws exception if any are known to be
   * missing. Some inputs may not yet be in the graph, in which case the builder should abort.
   */
private Pair<Map<Artifact, FileArtifactValue>, Map<Artifact, Collection<Artifact>>> checkInputs(Environment env, Action action, Map<SkyKey, ValueOrException2<MissingInputFileException, ActionExecutionException>> inputDeps) throws ActionExecutionException {
    int missingCount = 0;
    int actionFailures = 0;
    boolean catastrophe = false;
    // Only populate input data if we have the input values, otherwise they'll just go unused.
    // We still want to loop through the inputs to collect missing deps errors. During the
    // evaluator "error bubbling", we may get one last chance at reporting errors even though
    // some deps are still missing.
    boolean populateInputData = !env.valuesMissing();
    NestedSetBuilder<Cause> rootCauses = NestedSetBuilder.stableOrder();
    Map<Artifact, FileArtifactValue> inputArtifactData = new HashMap<>(populateInputData ? inputDeps.size() : 0);
    Map<Artifact, Collection<Artifact>> expandedArtifacts = new HashMap<>(populateInputData ? 128 : 0);
    ActionExecutionException firstActionExecutionException = null;
    for (Map.Entry<SkyKey, ValueOrException2<MissingInputFileException, ActionExecutionException>> depsEntry : inputDeps.entrySet()) {
        Artifact input = ArtifactSkyKey.artifact(depsEntry.getKey());
        try {
            SkyValue value = depsEntry.getValue().get();
            if (populateInputData) {
                if (value instanceof AggregatingArtifactValue) {
                    AggregatingArtifactValue aggregatingValue = (AggregatingArtifactValue) value;
                    for (Pair<Artifact, FileArtifactValue> entry : aggregatingValue.getInputs()) {
                        inputArtifactData.put(entry.first, entry.second);
                    }
                    // We have to cache the "digest" of the aggregating value itself,
                    // because the action cache checker may want it.
                    inputArtifactData.put(input, aggregatingValue.getSelfData());
                    ImmutableList.Builder<Artifact> expansionBuilder = ImmutableList.builder();
                    for (Pair<Artifact, FileArtifactValue> pair : aggregatingValue.getInputs()) {
                        expansionBuilder.add(pair.first);
                    }
                    expandedArtifacts.put(input, expansionBuilder.build());
                } else if (value instanceof TreeArtifactValue) {
                    TreeArtifactValue treeValue = (TreeArtifactValue) value;
                    expandedArtifacts.put(input, ImmutableSet.<Artifact>copyOf(treeValue.getChildren()));
                    inputArtifactData.putAll(treeValue.getChildValues());
                    // Again, we cache the "digest" of the value for cache checking.
                    inputArtifactData.put(input, treeValue.getSelfData());
                } else {
                    Preconditions.checkState(value instanceof FileArtifactValue, depsEntry);
                    inputArtifactData.put(input, (FileArtifactValue) value);
                }
            }
        } catch (MissingInputFileException e) {
            missingCount++;
            if (input.getOwner() != null) {
                rootCauses.add(new LabelCause(input.getOwner()));
            }
        } catch (ActionExecutionException e) {
            actionFailures++;
            if (firstActionExecutionException == null) {
                firstActionExecutionException = e;
            }
            catastrophe = catastrophe || e.isCatastrophe();
            rootCauses.addTransitive(e.getRootCauses());
        }
    }
    // We need to rethrow first exception because it can contain useful error message
    if (firstActionExecutionException != null) {
        if (missingCount == 0 && actionFailures == 1) {
            // having to copy the root causes to the upwards transitive closure.
            throw firstActionExecutionException;
        }
        throw new ActionExecutionException(firstActionExecutionException.getMessage(), firstActionExecutionException.getCause(), action, rootCauses.build(), catastrophe, firstActionExecutionException.getExitCode());
    }
    if (missingCount > 0) {
        for (Cause missingInput : rootCauses.build()) {
            env.getListener().handle(Event.error(action.getOwner().getLocation(), String.format("%s: missing input file '%s'", action.getOwner().getLabel(), missingInput.getLabel())));
        }
        throw new ActionExecutionException(missingCount + " input file(s) do not exist", action, rootCauses.build(), /*catastrophe=*/
        false);
    }
    return Pair.of(inputArtifactData, expandedArtifacts);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) Artifact(com.google.devtools.build.lib.actions.Artifact) SkyValue(com.google.devtools.build.skyframe.SkyValue) Cause(com.google.devtools.build.lib.causes.Cause) LabelCause(com.google.devtools.build.lib.causes.LabelCause) LabelCause(com.google.devtools.build.lib.causes.LabelCause) Collection(java.util.Collection) AlreadyReportedActionExecutionException(com.google.devtools.build.lib.actions.AlreadyReportedActionExecutionException) ActionExecutionException(com.google.devtools.build.lib.actions.ActionExecutionException) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) MissingInputFileException(com.google.devtools.build.lib.actions.MissingInputFileException)

Example 4 with ValueOrException2

use of com.google.devtools.build.skyframe.ValueOrException2 in project bazel by bazelbuild.

the class SkylarkImportLookupFunction method labelsForAbsoluteImports.

/**
   * Computes the set of Labels corresponding to a collection of PathFragments representing absolute
   * import paths.
   *
   * @return a map from the computed {@link Label}s to the corresponding {@link PathFragment}s;
   *     {@code null} if any Skyframe dependencies are unavailable.
   * @throws SkylarkImportFailedException
   */
@Nullable
static ImmutableMap<PathFragment, Label> labelsForAbsoluteImports(ImmutableSet<PathFragment> pathsToLookup, Environment env) throws SkylarkImportFailedException, InterruptedException {
    // Import PathFragments are absolute, so there is a 1-1 mapping from corresponding Labels.
    ImmutableMap.Builder<PathFragment, Label> outputMap = new ImmutableMap.Builder<>();
    // The SkyKey here represents the directory containing an import PathFragment, hence there
    // can in general be multiple imports per lookup.
    Multimap<SkyKey, PathFragment> lookupMap = LinkedHashMultimap.create();
    for (PathFragment importPath : pathsToLookup) {
        PathFragment relativeImportPath = importPath.toRelative();
        PackageIdentifier pkgToLookUp = PackageIdentifier.createInMainRepo(relativeImportPath.getParentDirectory());
        lookupMap.put(ContainingPackageLookupValue.key(pkgToLookUp), importPath);
    }
    // Attempt to find a package for every directory containing an import.
    Map<SkyKey, ValueOrException2<BuildFileNotFoundException, InconsistentFilesystemException>> lookupResults = env.getValuesOrThrow(lookupMap.keySet(), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
    if (env.valuesMissing()) {
        return null;
    }
    try {
        // Process lookup results.
        for (Entry<SkyKey, ValueOrException2<BuildFileNotFoundException, InconsistentFilesystemException>> entry : lookupResults.entrySet()) {
            ContainingPackageLookupValue lookupValue = (ContainingPackageLookupValue) entry.getValue().get();
            if (!lookupValue.hasContainingPackage()) {
                // Although multiple imports may be in the same package-less directory, we only
                // report an error for the first one.
                PackageIdentifier lookupKey = ((PackageIdentifier) entry.getKey().argument());
                PathFragment importFile = lookupKey.getPackageFragment();
                throw SkylarkImportFailedException.noBuildFile(importFile);
            }
            PackageIdentifier pkgIdForImport = lookupValue.getContainingPackageName();
            PathFragment containingPkgPath = pkgIdForImport.getPackageFragment();
            for (PathFragment importPath : lookupMap.get(entry.getKey())) {
                PathFragment relativeImportPath = importPath.toRelative();
                String targetNameForImport = relativeImportPath.relativeTo(containingPkgPath).toString();
                try {
                    outputMap.put(importPath, Label.create(pkgIdForImport, targetNameForImport));
                } catch (LabelSyntaxException e) {
                    // simple path.
                    throw new SkylarkImportFailedException(e);
                }
            }
        }
    } catch (BuildFileNotFoundException e) {
        // Thrown when there are IO errors looking for BUILD files.
        throw new SkylarkImportFailedException(e);
    } catch (InconsistentFilesystemException e) {
        throw new SkylarkImportFailedException(e);
    }
    return outputMap.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) ImmutableMap(com.google.common.collect.ImmutableMap) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) Nullable(javax.annotation.Nullable)

Example 5 with ValueOrException2

use of com.google.devtools.build.skyframe.ValueOrException2 in project bazel by bazelbuild.

the class TransitiveTargetFunction method processDeps.

@Override
void processDeps(TransitiveTargetValueBuilder builder, EventHandler eventHandler, TargetAndErrorIfAny targetAndErrorIfAny, Iterable<Entry<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>>> depEntries) throws InterruptedException {
    boolean successfulTransitiveLoading = builder.isSuccessfulTransitiveLoading();
    Target target = targetAndErrorIfAny.getTarget();
    NestedSetBuilder<Label> transitiveRootCauses = builder.getTransitiveRootCauses();
    for (Entry<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> entry : depEntries) {
        Label depLabel = (Label) entry.getKey().argument();
        TransitiveTargetValue transitiveTargetValue;
        try {
            transitiveTargetValue = (TransitiveTargetValue) entry.getValue().get();
            if (transitiveTargetValue == null) {
                continue;
            }
        } catch (NoSuchPackageException | NoSuchTargetException e) {
            successfulTransitiveLoading = false;
            transitiveRootCauses.add(depLabel);
            maybeReportErrorAboutMissingEdge(target, depLabel, e, eventHandler);
            continue;
        }
        builder.getTransitiveSuccessfulPkgs().addTransitive(transitiveTargetValue.getTransitiveSuccessfulPackages());
        builder.getTransitiveUnsuccessfulPkgs().addTransitive(transitiveTargetValue.getTransitiveUnsuccessfulPackages());
        builder.getTransitiveTargets().addTransitive(transitiveTargetValue.getTransitiveTargets());
        NestedSet<Label> rootCauses = transitiveTargetValue.getTransitiveRootCauses();
        if (rootCauses != null) {
            successfulTransitiveLoading = false;
            transitiveRootCauses.addTransitive(rootCauses);
            if (transitiveTargetValue.getErrorLoadingTarget() != null) {
                maybeReportErrorAboutMissingEdge(target, depLabel, transitiveTargetValue.getErrorLoadingTarget(), eventHandler);
            }
        }
        NestedSet<Class<? extends Fragment>> depFragments = transitiveTargetValue.getTransitiveConfigFragments();
        Collection<Class<? extends Fragment>> depFragmentsAsCollection = depFragments.toCollection();
        // The simplest collection technique would be to unconditionally add all deps' nested
        // sets to the current target's nested set. But when there's large overlap between their
        // fragment needs, this produces unnecessarily bloated nested sets and a lot of references
        // that don't contribute anything unique to the required fragment set. So we optimize here
        // by completely skipping sets that don't offer anything new. More fine-tuned optimization
        // is possible, but this offers a good balance between simplicity and practical efficiency.
        Set<Class<? extends Fragment>> addedConfigFragments = builder.getConfigFragmentsFromDeps();
        if (!addedConfigFragments.containsAll(depFragmentsAsCollection)) {
            builder.getTransitiveConfigFragments().addTransitive(depFragments);
            addedConfigFragments.addAll(depFragmentsAsCollection);
        }
    }
    builder.setSuccessfulTransitiveLoading(successfulTransitiveLoading);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) Fragment(com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment) Target(com.google.devtools.build.lib.packages.Target) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException)

Aggregations

SkyKey (com.google.devtools.build.skyframe.SkyKey)9 ValueOrException2 (com.google.devtools.build.skyframe.ValueOrException2)9 Label (com.google.devtools.build.lib.cmdline.Label)6 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Nullable (javax.annotation.Nullable)4 ActionExecutionException (com.google.devtools.build.lib.actions.ActionExecutionException)3 Artifact (com.google.devtools.build.lib.actions.Artifact)3 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)3 SkyValue (com.google.devtools.build.skyframe.SkyValue)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 AlreadyReportedActionExecutionException (com.google.devtools.build.lib.actions.AlreadyReportedActionExecutionException)2 MissingInputFileException (com.google.devtools.build.lib.actions.MissingInputFileException)2 Cause (com.google.devtools.build.lib.causes.Cause)2 LabelCause (com.google.devtools.build.lib.causes.LabelCause)2 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 Action (com.google.devtools.build.lib.actions.Action)1