use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.
the class TransitiveBaseTraversalFunction method loadTarget.
private LoadTargetResults loadTarget(Environment env, Label label) throws NoSuchTargetException, NoSuchPackageException, InterruptedException {
SkyKey packageKey = PackageValue.key(label.getPackageIdentifier());
SkyKey targetKey = TargetMarkerValue.key(label);
boolean packageLoadedSuccessfully;
Target target;
NoSuchTargetException errorLoadingTarget = null;
try {
TargetMarkerValue targetValue = getTargetMarkerValue(targetKey, env);
boolean targetValueMissing = targetValue == null;
Preconditions.checkState(targetValueMissing == env.valuesMissing(), targetKey);
if (targetValueMissing) {
return ValuesMissing.INSTANCE;
}
PackageValue packageValue = (PackageValue) env.getValueOrThrow(packageKey, NoSuchPackageException.class);
if (packageValue == null) {
return ValuesMissing.INSTANCE;
}
Package pkg = packageValue.getPackage();
if (pkg.containsErrors()) {
throw new BuildFileContainsErrorsException(label.getPackageIdentifier());
}
packageLoadedSuccessfully = true;
try {
target = pkg.getTarget(label.getName());
} catch (NoSuchTargetException unexpected) {
// was not present.
throw new IllegalStateException(unexpected);
}
} catch (NoSuchTargetException e) {
if (!e.hasTarget()) {
throw e;
}
// We know that a Target may be extracted, but we need to get it out of the Package
// (which is known to be in error).
PackageValue packageValue = (PackageValue) Preconditions.checkNotNull(env.getValue(packageKey), label);
Package pkg = packageValue.getPackage();
try {
target = pkg.getTarget(label.getName());
} catch (NoSuchTargetException nste) {
throw new IllegalStateException("Expected target to exist", nste);
}
errorLoadingTarget = e;
packageLoadedSuccessfully = false;
}
return new TargetAndErrorIfAnyImpl(packageLoadedSuccessfully, errorLoadingTarget, target);
}
use of com.google.devtools.build.lib.packages.NoSuchPackageException 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;
}
use of com.google.devtools.build.lib.packages.NoSuchPackageException 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);
}
use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.
the class TransitiveTargetFunction method getAspectLabels.
@Override
protected Collection<Label> getAspectLabels(Rule fromRule, Attribute attr, Label toLabel, ValueOrException2<NoSuchPackageException, NoSuchTargetException> toVal, final Environment env) throws InterruptedException {
SkyKey packageKey = PackageValue.key(toLabel.getPackageIdentifier());
try {
PackageValue pkgValue = (PackageValue) env.getValueOrThrow(packageKey, NoSuchPackageException.class);
if (pkgValue == null) {
return ImmutableList.of();
}
Package pkg = pkgValue.getPackage();
if (pkg.containsErrors()) {
// TransitiveTargetValue.
return ImmutableList.of();
}
Target dependedTarget = pkgValue.getPackage().getTarget(toLabel.getName());
return AspectDefinition.visitAspectsIfRequired(fromRule, attr, dependedTarget, DependencyFilter.ALL_DEPS).values();
} catch (NoSuchThingException e) {
// TransitiveTargetValue.
return ImmutableList.of();
}
}
use of com.google.devtools.build.lib.packages.NoSuchPackageException in project bazel by bazelbuild.
the class BuildTool method checkTargetEnvironmentRestrictions.
/**
* Checks that if this is an environment-restricted build, all top-level targets support the
* expected environments.
*
* @param topLevelTargets the build's top-level targets
* @throws ViewCreationFailedException if constraint enforcement is on, the build declares
* environment-restricted top level configurations, and any top-level target doesn't support
* the expected environments
*/
private static void checkTargetEnvironmentRestrictions(Iterable<ConfiguredTarget> topLevelTargets, LoadedPackageProvider packageManager) throws ViewCreationFailedException, InterruptedException {
for (ConfiguredTarget topLevelTarget : topLevelTargets) {
BuildConfiguration config = topLevelTarget.getConfiguration();
if (config == null) {
// TODO(bazel-team): support file targets (they should apply package-default constraints).
continue;
} else if (!config.enforceConstraints() || config.getTargetEnvironments().isEmpty()) {
continue;
}
// Parse and collect this configuration's environments.
EnvironmentCollection.Builder builder = new EnvironmentCollection.Builder();
for (Label envLabel : config.getTargetEnvironments()) {
try {
Target env = packageManager.getLoadedTarget(envLabel);
builder.put(ConstraintSemantics.getEnvironmentGroup(env), envLabel);
} catch (NoSuchPackageException | NoSuchTargetException | ConstraintSemantics.EnvironmentLookupException e) {
throw new ViewCreationFailedException("invalid target environment", e);
}
}
EnvironmentCollection expectedEnvironments = builder.build();
// Now check the target against those environments.
SupportedEnvironmentsProvider provider = Verify.verifyNotNull(topLevelTarget.getProvider(SupportedEnvironmentsProvider.class));
Collection<Label> missingEnvironments = ConstraintSemantics.getUnsupportedEnvironments(provider.getRefinedEnvironments(), expectedEnvironments);
if (!missingEnvironments.isEmpty()) {
throw new ViewCreationFailedException(String.format("This is a restricted-environment build. %s does not support" + " required environment%s %s", topLevelTarget.getLabel(), missingEnvironments.size() == 1 ? "" : "s", Joiner.on(", ").join(missingEnvironments)));
}
}
}
Aggregations