Search in sources :

Example 11 with NoSuchTargetException

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)));
        }
    }
}
Also used : Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) SupportedEnvironmentsProvider(com.google.devtools.build.lib.analysis.constraints.SupportedEnvironmentsProvider) EnvironmentCollection(com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection)

Example 12 with NoSuchTargetException

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SkyKey(com.google.devtools.build.skyframe.SkyKey) HashMap(java.util.HashMap) Label(com.google.devtools.build.lib.cmdline.Label) SkyValue(com.google.devtools.build.skyframe.SkyValue) Target(com.google.devtools.build.lib.packages.Target) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Package(com.google.devtools.build.lib.packages.Package)

Example 13 with NoSuchTargetException

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileNotFoundException(com.google.devtools.build.lib.packages.BuildFileNotFoundException) HashMap(java.util.HashMap) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException(com.google.devtools.build.skyframe.ValueOrException) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Package(com.google.devtools.build.lib.packages.Package)

Example 14 with NoSuchTargetException

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;
}
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 15 with NoSuchTargetException

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;
    }
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) Target(com.google.devtools.build.lib.packages.Target) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Package(com.google.devtools.build.lib.packages.Package) Nullable(javax.annotation.Nullable)

Aggregations

NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)23 SkyKey (com.google.devtools.build.skyframe.SkyKey)13 Label (com.google.devtools.build.lib.cmdline.Label)12 Target (com.google.devtools.build.lib.packages.Target)12 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)9 Package (com.google.devtools.build.lib.packages.Package)9 Test (org.junit.Test)6 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)5 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)4 Attribute (com.google.devtools.build.lib.packages.Attribute)4 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 HashMap (java.util.HashMap)4 Nullable (javax.annotation.Nullable)4 Rule (com.google.devtools.build.lib.packages.Rule)3 SkyValue (com.google.devtools.build.skyframe.SkyValue)3 ValueOrException2 (com.google.devtools.build.skyframe.ValueOrException2)3 LinkedHashSet (java.util.LinkedHashSet)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 InconsistentAspectOrderException (com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException)2 MergedConfiguredTarget (com.google.devtools.build.lib.analysis.MergedConfiguredTarget)2