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();
}
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;
}
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;
}
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);
}
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;
}
Aggregations