Search in sources :

Example 6 with NoSuchTargetException

use of com.google.devtools.build.lib.packages.NoSuchTargetException in project bazel by bazelbuild.

the class PrepareDepsOfPatternsFunctionTest method testDependencyTraversalNoSuchTargetException.

@Test
public void testDependencyTraversalNoSuchTargetException() throws Exception {
    // Given a package "//foo" with a target ":foo" that has a dependency on a non-existent target
    // "//bar:bar" in an existing package "//bar",
    createFooWithDependencyOnBarPackageWithMissingTarget();
    // Given a target pattern sequence consisting of a single-target pattern for "//foo",
    ImmutableList<String> patternSequence = ImmutableList.of("//foo");
    // When PrepareDepsOfPatternsFunction completes evaluation,
    WalkableGraph walkableGraph = getGraphFromPatternsEvaluation(patternSequence);
    // Then the graph contains an entry for ":foo" which has both a value and an exception,
    assertValidValue(walkableGraph, getKeyForLabel(Label.create("@//foo", "foo")), /*expectTransitiveException=*/
    true);
    // And an entry with a NoSuchTargetException for "//bar:bar",
    Exception e = assertException(walkableGraph, getKeyForLabel(Label.create("@//bar", "bar")));
    assertThat(e).isInstanceOf(NoSuchTargetException.class);
}
Also used : WalkableGraph(com.google.devtools.build.skyframe.WalkableGraph) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) IOException(java.io.IOException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Test(org.junit.Test)

Example 7 with NoSuchTargetException

use of com.google.devtools.build.lib.packages.NoSuchTargetException in project bazel by bazelbuild.

the class TargetMarkerFunctionTest method testLabelCrossingSubpackageBoundary.

/** Regression test for b/12545745 */
@Test
public void testLabelCrossingSubpackageBoundary() throws Exception {
    scratch.file("a/b/c/foo.sh", "echo 'FOO'");
    scratch.file("a/BUILD", "sh_library(name = 'foo', srcs = ['b/c/foo.sh'])");
    String labelName = "//a:b/c/foo.sh";
    scratch.file("a/b/BUILD");
    ModifiedFileSet subpackageBuildFile = ModifiedFileSet.builder().modify(new PathFragment("a/b/BUILD")).build();
    skyframeExecutor.invalidateFilesUnderPathForTesting(reporter, subpackageBuildFile, rootDirectory);
    NoSuchTargetException exn = (NoSuchTargetException) getErrorFromTargetValue(labelName);
    // In the presence of b/12545745, the error message is different and comes from the
    // PackageFunction.
    assertThat(exn.getMessage()).contains("Label '//a:b/c/foo.sh' crosses boundary of subpackage 'a/b'");
}
Also used : NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) ModifiedFileSet(com.google.devtools.build.lib.vfs.ModifiedFileSet) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Test(org.junit.Test)

Example 8 with NoSuchTargetException

use of com.google.devtools.build.lib.packages.NoSuchTargetException in project bazel by bazelbuild.

the class AspectFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws AspectFunctionException, InterruptedException {
    SkyframeBuildView view = buildViewProvider.getSkyframeBuildView();
    NestedSetBuilder<Package> transitivePackages = NestedSetBuilder.stableOrder();
    NestedSetBuilder<Label> transitiveRootCauses = NestedSetBuilder.stableOrder();
    AspectKey key = (AspectKey) skyKey.argument();
    ConfiguredAspectFactory aspectFactory;
    Aspect aspect;
    if (key.getAspectClass() instanceof NativeAspectClass) {
        NativeAspectClass nativeAspectClass = (NativeAspectClass) key.getAspectClass();
        aspectFactory = (ConfiguredAspectFactory) nativeAspectClass;
        aspect = Aspect.forNative(nativeAspectClass, key.getParameters());
    } else if (key.getAspectClass() instanceof SkylarkAspectClass) {
        SkylarkAspectClass skylarkAspectClass = (SkylarkAspectClass) key.getAspectClass();
        SkylarkAspect skylarkAspect;
        try {
            skylarkAspect = loadSkylarkAspect(env, skylarkAspectClass.getExtensionLabel(), skylarkAspectClass.getExportedName());
        } catch (AspectCreationException e) {
            throw new AspectFunctionException(e);
        }
        if (skylarkAspect == null) {
            return null;
        }
        aspectFactory = new SkylarkAspectFactory(skylarkAspect);
        aspect = Aspect.forSkylark(skylarkAspect.getAspectClass(), skylarkAspect.getDefinition(key.getParameters()), key.getParameters());
    } else {
        throw new IllegalStateException();
    }
    // Keep this in sync with the same code in ConfiguredTargetFunction.
    PackageValue packageValue = (PackageValue) env.getValue(PackageValue.key(key.getLabel().getPackageIdentifier()));
    if (packageValue == null) {
        return null;
    }
    Package pkg = packageValue.getPackage();
    if (pkg.containsErrors()) {
        throw new AspectFunctionException(new BuildFileContainsErrorsException(key.getLabel().getPackageIdentifier()));
    }
    Target target;
    try {
        target = pkg.getTarget(key.getLabel().getName());
    } catch (NoSuchTargetException e) {
        throw new AspectFunctionException(e);
    }
    if (!(target instanceof Rule)) {
        env.getListener().handle(Event.error(target.getLocation(), String.format("%s is attached to %s %s but aspects must be attached to rules", aspect.getAspectClass().getName(), target.getTargetKind(), target.getName())));
        throw new AspectFunctionException(new AspectCreationException("aspects must be attached to rules"));
    }
    ConfiguredTargetValue configuredTargetValue;
    try {
        configuredTargetValue = (ConfiguredTargetValue) env.getValueOrThrow(ConfiguredTargetValue.key(key.getLabel(), key.getBaseConfiguration()), ConfiguredValueCreationException.class);
    } catch (ConfiguredValueCreationException e) {
        throw new AspectFunctionException(new AspectCreationException(e.getRootCauses()));
    }
    if (configuredTargetValue == null) {
        // precomputed.
        return null;
    }
    if (configuredTargetValue.getConfiguredTarget() == null) {
        return null;
    }
    if (configuredTargetValue.getConfiguredTarget().getProvider(AliasProvider.class) != null) {
        return createAliasAspect(env, target, aspect, key, configuredTargetValue.getConfiguredTarget());
    }
    ConfiguredTarget associatedTarget = configuredTargetValue.getConfiguredTarget();
    ImmutableList.Builder<Aspect> aspectPathBuilder = ImmutableList.builder();
    if (!key.getBaseKeys().isEmpty()) {
        // We transitively collect all required aspects to reduce the number of restarts.
        // Semantically it is enough to just request key.getBaseKeys().
        ImmutableMap<AspectDescriptor, SkyKey> aspectKeys = getSkyKeysForAspects(key.getBaseKeys());
        Map<SkyKey, SkyValue> values = env.getValues(aspectKeys.values());
        if (env.valuesMissing()) {
            return null;
        }
        try {
            associatedTarget = getBaseTargetAndCollectPath(associatedTarget, key.getBaseKeys(), values, aspectPathBuilder);
        } catch (DuplicateException e) {
            env.getListener().handle(Event.error(associatedTarget.getTarget().getLocation(), e.getMessage()));
            throw new AspectFunctionException(new AspectCreationException(e.getMessage(), associatedTarget.getLabel()));
        }
    }
    aspectPathBuilder.add(aspect);
    SkyframeDependencyResolver resolver = view.createDependencyResolver(env);
    // When getting the dependencies of this hybrid aspect+base target, use the aspect's
    // configuration. The configuration of the aspect will always be a superset of the target's
    // (dynamic configuration mode: target is part of the aspect's config fragment requirements;
    // static configuration mode: target is the same configuration as the aspect), so the fragments
    // required by all dependencies (both those of the aspect and those of the base target)
    // will be present this way.
    TargetAndConfiguration originalTargetAndAspectConfiguration = new TargetAndConfiguration(target, key.getAspectConfiguration());
    ImmutableList<Aspect> aspectPath = aspectPathBuilder.build();
    try {
        // Get the configuration targets that trigger this rule's configurable attributes.
        ImmutableMap<Label, ConfigMatchingProvider> configConditions = ConfiguredTargetFunction.getConfigConditions(target, env, resolver, originalTargetAndAspectConfiguration, transitivePackages, transitiveRootCauses);
        if (configConditions == null) {
            // Those targets haven't yet been resolved.
            return null;
        }
        OrderedSetMultimap<Attribute, ConfiguredTarget> depValueMap;
        try {
            depValueMap = ConfiguredTargetFunction.computeDependencies(env, resolver, originalTargetAndAspectConfiguration, aspectPath, configConditions, ruleClassProvider, view.getHostConfiguration(originalTargetAndAspectConfiguration.getConfiguration()), transitivePackages, transitiveRootCauses);
        } catch (ConfiguredTargetFunctionException e) {
            throw new AspectCreationException(e.getMessage());
        }
        if (depValueMap == null) {
            return null;
        }
        if (!transitiveRootCauses.isEmpty()) {
            throw new AspectFunctionException(new AspectCreationException("Loading failed", transitiveRootCauses.build()));
        }
        return createAspect(env, key, aspectPath, aspect, aspectFactory, associatedTarget, key.getAspectConfiguration(), configConditions, depValueMap, transitivePackages);
    } catch (DependencyEvaluationException e) {
        if (e.getCause() instanceof ConfiguredValueCreationException) {
            ConfiguredValueCreationException cause = (ConfiguredValueCreationException) e.getCause();
            throw new AspectFunctionException(new AspectCreationException(cause.getMessage(), cause.getAnalysisRootCause()));
        } else if (e.getCause() instanceof InconsistentAspectOrderException) {
            InconsistentAspectOrderException cause = (InconsistentAspectOrderException) e.getCause();
            throw new AspectFunctionException(new AspectCreationException(cause.getMessage()));
        } else {
            // Cast to InvalidConfigurationException as a consistency check. If you add any
            // DependencyEvaluationException constructors, you may need to change this code, too.
            InvalidConfigurationException cause = (InvalidConfigurationException) e.getCause();
            throw new AspectFunctionException(new AspectCreationException(cause.getMessage()));
        }
    } catch (AspectCreationException e) {
        throw new AspectFunctionException(e);
    }
}
Also used : AspectKey(com.google.devtools.build.lib.skyframe.AspectValue.AspectKey) Attribute(com.google.devtools.build.lib.packages.Attribute) ImmutableList(com.google.common.collect.ImmutableList) Label(com.google.devtools.build.lib.cmdline.Label) SkylarkAspect(com.google.devtools.build.lib.packages.SkylarkAspect) ConfiguredAspect(com.google.devtools.build.lib.analysis.ConfiguredAspect) Aspect(com.google.devtools.build.lib.packages.Aspect) DependencyEvaluationException(com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.DependencyEvaluationException) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) SkyValue(com.google.devtools.build.skyframe.SkyValue) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) AliasProvider(com.google.devtools.build.lib.rules.AliasProvider) ConfiguredAspectFactory(com.google.devtools.build.lib.analysis.ConfiguredAspectFactory) NativeAspectClass(com.google.devtools.build.lib.packages.NativeAspectClass) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) SkylarkAspect(com.google.devtools.build.lib.packages.SkylarkAspect) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) SkyKey(com.google.devtools.build.skyframe.SkyKey) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) ConfiguredValueCreationException(com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ConfiguredValueCreationException) InconsistentAspectOrderException(com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException) TargetAndConfiguration(com.google.devtools.build.lib.analysis.TargetAndConfiguration) SkylarkAspectClass(com.google.devtools.build.lib.packages.SkylarkAspectClass) DuplicateException(com.google.devtools.build.lib.analysis.MergedConfiguredTarget.DuplicateException) ConfiguredTargetFunctionException(com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.ConfiguredTargetFunctionException) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Package(com.google.devtools.build.lib.packages.Package) Rule(com.google.devtools.build.lib.packages.Rule) Nullable(javax.annotation.Nullable)

Example 9 with NoSuchTargetException

use of com.google.devtools.build.lib.packages.NoSuchTargetException in project bazel by bazelbuild.

the class ConfiguredTargetFunction method compute.

@Override
public SkyValue compute(SkyKey key, Environment env) throws ConfiguredTargetFunctionException, InterruptedException {
    SkyframeBuildView view = buildViewProvider.getSkyframeBuildView();
    NestedSetBuilder<Package> transitivePackages = NestedSetBuilder.stableOrder();
    NestedSetBuilder<Label> transitiveLoadingRootCauses = NestedSetBuilder.stableOrder();
    ConfiguredTargetKey configuredTargetKey = (ConfiguredTargetKey) key.argument();
    LabelAndConfiguration lc = LabelAndConfiguration.of(configuredTargetKey.getLabel(), configuredTargetKey.getConfiguration());
    BuildConfiguration configuration = lc.getConfiguration();
    PackageValue packageValue = (PackageValue) env.getValue(PackageValue.key(lc.getLabel().getPackageIdentifier()));
    if (packageValue == null) {
        return null;
    }
    // TODO(ulfjack): This tries to match the logic in TransitiveTargetFunction /
    // TargetMarkerFunction. Maybe we can merge the two?
    Package pkg = packageValue.getPackage();
    Target target;
    try {
        target = pkg.getTarget(lc.getLabel().getName());
    } catch (NoSuchTargetException e) {
        throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(e.getMessage()));
    }
    if (pkg.containsErrors()) {
        transitiveLoadingRootCauses.add(lc.getLabel());
    }
    transitivePackages.add(pkg);
    // null).
    if (!target.isConfigurable()) {
        configuration = null;
    }
    // associates the error with the dep, which is misleading.
    if (useDynamicConfigurations(configuration) && configuration.trimConfigurations() && env.getValue(TransitiveTargetValue.key(lc.getLabel())) == null) {
        return null;
    }
    TargetAndConfiguration ctgValue = new TargetAndConfiguration(target, configuration);
    SkyframeDependencyResolver resolver = view.createDependencyResolver(env);
    // TODO(janakr): this acquire() call may tie up this thread indefinitely, reducing the
    // parallelism of Skyframe. This is a strict improvement over the prior state of the code, in
    // which we ran with #processors threads, but ideally we would call #tryAcquire here, and if we
    // failed, would exit this SkyFunction and restart it when permits were available.
    cpuBoundSemaphore.acquire();
    try {
        // Get the configuration targets that trigger this rule's configurable attributes.
        ImmutableMap<Label, ConfigMatchingProvider> configConditions = getConfigConditions(ctgValue.getTarget(), env, resolver, ctgValue, transitivePackages, transitiveLoadingRootCauses);
        if (env.valuesMissing()) {
            return null;
        }
        // attributes.
        if (!transitiveLoadingRootCauses.isEmpty() && configConditions != NO_CONFIG_CONDITIONS) {
            throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(transitiveLoadingRootCauses.build()));
        }
        OrderedSetMultimap<Attribute, ConfiguredTarget> depValueMap = computeDependencies(env, resolver, ctgValue, ImmutableList.<Aspect>of(), configConditions, ruleClassProvider, view.getHostConfiguration(configuration), transitivePackages, transitiveLoadingRootCauses);
        if (env.valuesMissing()) {
            return null;
        }
        if (!transitiveLoadingRootCauses.isEmpty()) {
            throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(transitiveLoadingRootCauses.build()));
        }
        Preconditions.checkNotNull(depValueMap);
        ConfiguredTargetValue ans = createConfiguredTarget(view, env, target, configuration, depValueMap, configConditions, transitivePackages);
        return ans;
    } catch (DependencyEvaluationException e) {
        if (e.getCause() instanceof ConfiguredValueCreationException) {
            throw new ConfiguredTargetFunctionException((ConfiguredValueCreationException) e.getCause());
        } else if (e.getCause() instanceof InconsistentAspectOrderException) {
            InconsistentAspectOrderException cause = (InconsistentAspectOrderException) e.getCause();
            throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(cause.getMessage(), target.getLabel()));
        } else {
            // Cast to InvalidConfigurationException as a consistency check. If you add any
            // DependencyEvaluationException constructors, you may need to change this code, too.
            InvalidConfigurationException cause = (InvalidConfigurationException) e.getCause();
            throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(cause.getMessage(), target.getLabel()));
        }
    } catch (AspectCreationException e) {
        // getAnalysisRootCause may be null if the analysis of the aspect itself failed.
        Label analysisRootCause = target.getLabel();
        if (e.getAnalysisRootCause() != null) {
            analysisRootCause = e.getAnalysisRootCause();
        }
        throw new ConfiguredTargetFunctionException(new ConfiguredValueCreationException(e.getMessage(), analysisRootCause));
    } finally {
        cpuBoundSemaphore.release();
    }
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) LabelAndConfiguration(com.google.devtools.build.lib.analysis.LabelAndConfiguration) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) InconsistentAspectOrderException(com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException) AspectCreationException(com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException) TargetAndConfiguration(com.google.devtools.build.lib.analysis.TargetAndConfiguration) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Package(com.google.devtools.build.lib.packages.Package)

Example 10 with NoSuchTargetException

use of com.google.devtools.build.lib.packages.NoSuchTargetException in project bazel by bazelbuild.

the class GenQuery method getScope.

// The transitive closure of these targets is an upper estimate on the labels
// the query will touch
private static Set<Target> getScope(RuleContext context) throws InterruptedException {
    List<Label> scopeLabels = context.attributes().get("scope", BuildType.LABEL_LIST);
    Set<Target> scope = Sets.newHashSetWithExpectedSize(scopeLabels.size());
    for (Label scopePart : scopeLabels) {
        SkyFunction.Environment env = context.getAnalysisEnvironment().getSkyframeEnv();
        PackageValue packageNode = (PackageValue) env.getValue(PackageValue.key(scopePart.getPackageIdentifier()));
        Preconditions.checkNotNull(packageNode, "Packages in transitive closure of scope '%s'" + "were already loaded during the loading phase", scopePart);
        try {
            scope.add(packageNode.getPackage().getTarget(scopePart.getName()));
        } catch (NoSuchTargetException e) {
            throw new IllegalStateException(e);
        }
    }
    return scope;
}
Also used : ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) SkyFunction(com.google.devtools.build.skyframe.SkyFunction) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Label(com.google.devtools.build.lib.cmdline.Label) PackageValue(com.google.devtools.build.lib.skyframe.PackageValue)

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