Search in sources :

Example 66 with Nullable

use of javax.annotation.Nullable 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 67 with Nullable

use of javax.annotation.Nullable in project bazel by bazelbuild.

the class SkylarkImportLookupFunction method findLabelsForLoadStatements.

/**
   * Computes the set of {@link Label}s corresponding to a set of Skylark {@link LoadStatement}s.
   *
   * @param imports a collection of Skylark {@link LoadStatement}s
   * @param containingFileLabel the {@link Label} of the file containing the load statements
   * @return an {@link ImmutableMap} which maps a {@link String} used in the load statement to its
   *     corresponding {@Label}. Returns {@code null} if any Skyframe dependencies are unavailable.
   * @throws SkylarkImportFailedException if no package can be found that contains the loaded file
   */
@Nullable
static ImmutableMap<String, Label> findLabelsForLoadStatements(ImmutableCollection<SkylarkImport> imports, Label containingFileLabel, Environment env) throws SkylarkImportFailedException, InterruptedException {
    Preconditions.checkArgument(!containingFileLabel.getPackageIdentifier().getRepository().isDefault());
    Map<String, Label> outputMap = Maps.newHashMapWithExpectedSize(imports.size());
    // Filter relative vs. absolute paths.
    ImmutableSet.Builder<PathFragment> absoluteImportsToLookup = new ImmutableSet.Builder<>();
    // We maintain a multimap from path fragments to their correspond import strings, to cover the
    // (unlikely) case where two distinct import strings generate the same path fragment.
    ImmutableMultimap.Builder<PathFragment, String> pathToImports = new ImmutableMultimap.Builder<>();
    for (SkylarkImport imp : imports) {
        if (imp.hasAbsolutePath()) {
            absoluteImportsToLookup.add(imp.getAbsolutePath());
            pathToImports.put(imp.getAbsolutePath(), imp.getImportString());
        } else {
            outputMap.put(imp.getImportString(), imp.getLabel(containingFileLabel));
        }
    }
    // Look up labels for absolute paths.
    ImmutableMap<PathFragment, Label> absoluteLabels = labelsForAbsoluteImports(absoluteImportsToLookup.build(), env);
    if (absoluteLabels == null) {
        return null;
    }
    for (Entry<PathFragment, Label> entry : absoluteLabels.entrySet()) {
        PathFragment currPath = entry.getKey();
        Label currLabel = entry.getValue();
        for (String importString : pathToImports.build().get(currPath)) {
            outputMap.put(importString, currLabel);
        }
    }
    ImmutableMap<String, Label> immutableOutputMap = ImmutableMap.copyOf(outputMap);
    return immutableOutputMap;
}
Also used : Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) SkylarkImport(com.google.devtools.build.lib.syntax.SkylarkImport) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Nullable(javax.annotation.Nullable)

Example 68 with Nullable

use of javax.annotation.Nullable in project bazel by bazelbuild.

the class TargetMarkerFunction method computeTargetMarkerValue.

@Nullable
static TargetMarkerValue computeTargetMarkerValue(SkyKey key, Environment env) throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
    Label label = (Label) key.argument();
    PathFragment pkgForLabel = label.getPackageFragment();
    if (label.getName().contains("/")) {
        // This target is in a subdirectory, therefore it could potentially be invalidated by
        // a new BUILD file appearing in the hierarchy.
        PathFragment containingDirectory = label.toPathFragment().getParentDirectory();
        ContainingPackageLookupValue containingPackageLookupValue;
        try {
            PackageIdentifier newPkgId = PackageIdentifier.create(label.getPackageIdentifier().getRepository(), containingDirectory);
            containingPackageLookupValue = (ContainingPackageLookupValue) env.getValueOrThrow(ContainingPackageLookupValue.key(newPkgId), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
        } catch (InconsistentFilesystemException e) {
            throw new NoSuchTargetException(label, e.getMessage());
        }
        if (containingPackageLookupValue == null) {
            return null;
        }
        if (!containingPackageLookupValue.hasContainingPackage()) {
            // trying to build the target for label 'a:b/foo'.
            throw new BuildFileNotFoundException(label.getPackageIdentifier(), "BUILD file not found on package path for '" + pkgForLabel.getPathString() + "'");
        }
        if (!containingPackageLookupValue.getContainingPackageName().equals(label.getPackageIdentifier())) {
            throw new NoSuchTargetException(label, String.format("Label '%s' crosses boundary of subpackage '%s'", label, containingPackageLookupValue.getContainingPackageName()));
        }
    }
    SkyKey pkgSkyKey = PackageValue.key(label.getPackageIdentifier());
    PackageValue value = (PackageValue) env.getValueOrThrow(pkgSkyKey, NoSuchPackageException.class);
    if (value == null) {
        return null;
    }
    Package pkg = value.getPackage();
    Target target = pkg.getTarget(label.getName());
    if (pkg.containsErrors()) {
        // if one of its targets was in error).
        throw new NoSuchTargetException(target);
    }
    return TargetMarkerValue.TARGET_MARKER_INSTANCE;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) Label(com.google.devtools.build.lib.cmdline.Label) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Package(com.google.devtools.build.lib.packages.Package) Nullable(javax.annotation.Nullable)

Example 69 with Nullable

use of javax.annotation.Nullable in project bazel by bazelbuild.

the class PostConfiguredTargetFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException {
    ImmutableMap<ActionAnalysisMetadata, ConflictException> badActions = PrecomputedValue.BAD_ACTIONS.get(env);
    ConfiguredTargetValue ctValue = (ConfiguredTargetValue) env.getValue(ConfiguredTargetValue.key((ConfiguredTargetKey) skyKey.argument()));
    if (env.valuesMissing()) {
        return null;
    }
    for (ActionAnalysisMetadata action : ctValue.getActions()) {
        if (badActions.containsKey(action)) {
            throw new ActionConflictFunctionException(badActions.get(action));
        }
    }
    ConfiguredTarget ct = ctValue.getConfiguredTarget();
    TargetAndConfiguration ctgValue = new TargetAndConfiguration(ct.getTarget(), ct.getConfiguration());
    ImmutableMap<Label, ConfigMatchingProvider> configConditions = getConfigurableAttributeConditions(ctgValue, env);
    if (configConditions == null) {
        return null;
    }
    OrderedSetMultimap<Attribute, Dependency> deps;
    try {
        BuildConfiguration hostConfiguration = buildViewProvider.getSkyframeBuildView().getHostConfiguration(ct.getConfiguration());
        SkyframeDependencyResolver resolver = buildViewProvider.getSkyframeBuildView().createDependencyResolver(env);
        // We don't track root causes here - this function is only invoked for successfully analyzed
        // targets - as long as we redo the exact same steps here as in ConfiguredTargetFunction, this
        // can never fail.
        deps = resolver.dependentNodeMap(ctgValue, hostConfiguration, /*aspect=*/
        null, configConditions);
        if (ct.getConfiguration() != null && ct.getConfiguration().useDynamicConfigurations()) {
            deps = ConfiguredTargetFunction.getDynamicConfigurations(env, ctgValue, deps, hostConfiguration, ruleClassProvider);
        }
    } catch (EvalException | ConfiguredTargetFunction.DependencyEvaluationException | InvalidConfigurationException | InconsistentAspectOrderException e) {
        throw new PostConfiguredTargetFunctionException(e);
    }
    env.getValues(Iterables.transform(deps.values(), TO_KEYS));
    if (env.valuesMissing()) {
        return null;
    }
    return new PostConfiguredTargetValue(ct);
}
Also used : ConflictException(com.google.devtools.build.lib.skyframe.SkyframeActionExecutor.ConflictException) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) ActionAnalysisMetadata(com.google.devtools.build.lib.actions.ActionAnalysisMetadata) Dependency(com.google.devtools.build.lib.analysis.Dependency) EvalException(com.google.devtools.build.lib.syntax.EvalException) InconsistentAspectOrderException(com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) TargetAndConfiguration(com.google.devtools.build.lib.analysis.TargetAndConfiguration) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Nullable(javax.annotation.Nullable)

Example 70 with Nullable

use of javax.annotation.Nullable in project bazel by bazelbuild.

the class PrepareDepsOfPatternFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey key, Environment env) throws SkyFunctionException, InterruptedException {
    TargetPatternValue.TargetPatternKey patternKey = ((TargetPatternValue.TargetPatternKey) key.argument());
    // DepsOfPatternPreparer below expects to be able to ignore the filtering policy from the
    // TargetPatternKey, which should be valid because PrepareDepsOfPatternValue.keys
    // unconditionally creates TargetPatternKeys with the NO_FILTER filtering policy. (Compare
    // with SkyframeTargetPatternEvaluator, which can create TargetPatternKeys with other
    // filtering policies like FILTER_TESTS or FILTER_MANUAL.) This check makes sure that the
    // key's filtering policy is NO_FILTER as expected.
    Preconditions.checkState(patternKey.getPolicy().equals(FilteringPolicies.NO_FILTER), patternKey.getPolicy());
    TargetPattern parsedPattern = patternKey.getParsedPattern();
    BlacklistedPackagePrefixesValue blacklist = (BlacklistedPackagePrefixesValue) env.getValue(BlacklistedPackagePrefixesValue.key());
    if (blacklist == null) {
        return null;
    }
    ImmutableSet<PathFragment> subdirectoriesToExclude = patternKey.getAllSubdirectoriesToExclude(blacklist.getPatterns());
    DepsOfPatternPreparer preparer = new DepsOfPatternPreparer(env, pkgPath.get());
    try {
        parsedPattern.eval(preparer, subdirectoriesToExclude, NullCallback.<Void>instance(), RuntimeException.class);
    } catch (TargetParsingException e) {
        throw new PrepareDepsOfPatternFunctionException(e);
    } catch (MissingDepException e) {
        // when it has a dependency on a missing Environment value.
        return null;
    }
    return PrepareDepsOfPatternValue.INSTANCE;
}
Also used : TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) MissingDepException(com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException) Nullable(javax.annotation.Nullable)

Aggregations

Nullable (javax.annotation.Nullable)859 IOException (java.io.IOException)94 Map (java.util.Map)77 List (java.util.List)67 Test (org.junit.Test)62 Function (com.google.common.base.Function)60 File (java.io.File)53 ArrayList (java.util.ArrayList)52 HashMap (java.util.HashMap)47 ImmutableList (com.google.common.collect.ImmutableList)37 ItemStack (net.minecraft.item.ItemStack)35 ImmutableMap (com.google.common.collect.ImmutableMap)33 Nonnull (javax.annotation.Nonnull)31 Predicate (com.google.common.base.Predicate)29 SkyKey (com.google.devtools.build.skyframe.SkyKey)28 URL (java.net.URL)24 Collectors (java.util.stream.Collectors)24 HashSet (java.util.HashSet)22 Label (com.google.devtools.build.lib.cmdline.Label)21 Set (java.util.Set)20