use of com.google.devtools.build.lib.packages.NoSuchTargetException 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)));
}
}
}
use of com.google.devtools.build.lib.packages.NoSuchTargetException in project bazel by bazelbuild.
the class TestSuiteExpansionFunction method labelsToTargets.
static ResolvedTargets<Target> labelsToTargets(Environment env, ImmutableSet<Label> labels, boolean hasError) throws InterruptedException {
Set<PackageIdentifier> pkgIdentifiers = new LinkedHashSet<>();
for (Label label : labels) {
pkgIdentifiers.add(label.getPackageIdentifier());
}
// Don't bother to check for exceptions - the incoming list should only contain valid targets.
Map<SkyKey, SkyValue> packages = env.getValues(PackageValue.keys(pkgIdentifiers));
if (env.valuesMissing()) {
return null;
}
ResolvedTargets.Builder<Target> builder = ResolvedTargets.builder();
builder.mergeError(hasError);
Map<PackageIdentifier, Package> packageMap = new HashMap<>();
for (Entry<SkyKey, SkyValue> entry : packages.entrySet()) {
packageMap.put((PackageIdentifier) entry.getKey().argument(), ((PackageValue) entry.getValue()).getPackage());
}
for (Label label : labels) {
Package pkg = packageMap.get(label.getPackageIdentifier());
if (pkg == null) {
continue;
}
try {
builder.add(pkg.getTarget(label.getName()));
if (pkg.containsErrors()) {
builder.setError();
}
} catch (NoSuchTargetException e) {
builder.setError();
}
}
return builder.build();
}
use of com.google.devtools.build.lib.packages.NoSuchTargetException in project bazel by bazelbuild.
the class TestsInSuiteFunction method getPrerequisites.
/**
* Adds the set of targets found in the attribute named {@code attrName}, which must be of label
* list type, of the {@code test_suite} rule named {@code testSuite}. Returns true if the method
* found a problem during the lookup process; the actual error message is reported to the
* environment.
*/
private static boolean getPrerequisites(Environment env, Rule testSuite, String attrName, List<Target> targets) throws InterruptedException {
List<Label> labels = NonconfigurableAttributeMapper.of(testSuite).get(attrName, BuildType.LABEL_LIST);
Set<PackageIdentifier> pkgIdentifiers = new LinkedHashSet<>();
for (Label label : labels) {
pkgIdentifiers.add(label.getPackageIdentifier());
}
Map<SkyKey, ValueOrException<BuildFileNotFoundException>> packages = env.getValuesOrThrow(PackageValue.keys(pkgIdentifiers), BuildFileNotFoundException.class);
if (env.valuesMissing()) {
return false;
}
boolean hasError = false;
Map<PackageIdentifier, Package> packageMap = new HashMap<>();
for (Entry<SkyKey, ValueOrException<BuildFileNotFoundException>> entry : packages.entrySet()) {
try {
packageMap.put((PackageIdentifier) entry.getKey().argument(), ((PackageValue) entry.getValue().get()).getPackage());
} catch (BuildFileNotFoundException e) {
env.getListener().handle(Event.error(e.getMessage()));
hasError = true;
}
}
for (Label label : labels) {
Package pkg = packageMap.get(label.getPackageIdentifier());
if (pkg == null) {
continue;
}
try {
targets.add(pkg.getTarget(label.getName()));
} catch (NoSuchTargetException e) {
env.getListener().handle(Event.error(e.getMessage()));
hasError = true;
}
}
return hasError;
}
use of com.google.devtools.build.lib.packages.NoSuchTargetException 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 com.google.devtools.build.lib.packages.NoSuchTargetException in project bazel by bazelbuild.
the class SkyframeDependencyResolver method getTarget.
@Nullable
@Override
protected Target getTarget(Target from, Label label, NestedSetBuilder<Label> rootCauses) throws InterruptedException {
SkyKey key = PackageValue.key(label.getPackageIdentifier());
PackageValue packageValue;
try {
packageValue = (PackageValue) env.getValueOrThrow(key, NoSuchPackageException.class);
} catch (NoSuchPackageException e) {
rootCauses.add(label);
missingEdgeHook(from, label, e);
return null;
}
if (packageValue == null) {
return null;
}
Package pkg = packageValue.getPackage();
try {
Target target = pkg.getTarget(label.getName());
if (pkg.containsErrors()) {
NoSuchTargetException e = new NoSuchTargetException(target);
missingEdgeHook(from, label, e);
if (target != null) {
rootCauses.add(label);
return target;
} else {
return null;
}
}
return target;
} catch (NoSuchTargetException e) {
rootCauses.add(label);
missingEdgeHook(from, label, e);
return null;
}
}
Aggregations