Search in sources :

Example 1 with TargetPatternSkyKeyOrException

use of com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException in project bazel by bazelbuild.

the class SkyframeTargetPatternEvaluator method parseTargetPatternList.

/**
   * Loads a list of target patterns (eg, "foo/..."). When policy is set to FILTER_TESTS,
   * test_suites are going to be expanded.
   */
ResolvedTargets<Target> parseTargetPatternList(String offset, ExtendedEventHandler eventHandler, List<String> targetPatterns, FilteringPolicy policy, boolean keepGoing) throws InterruptedException, TargetParsingException {
    Iterable<TargetPatternSkyKeyOrException> keysMaybe = TargetPatternValue.keys(targetPatterns, policy, offset);
    ImmutableList.Builder<SkyKey> builder = ImmutableList.builder();
    for (TargetPatternSkyKeyOrException skyKeyOrException : keysMaybe) {
        try {
            builder.add(skyKeyOrException.getSkyKey());
        } catch (TargetParsingException e) {
            if (!keepGoing) {
                throw e;
            }
            String pattern = skyKeyOrException.getOriginalPattern();
            eventHandler.handle(Event.error("Skipping '" + pattern + "': " + e.getMessage()));
            if (eventHandler instanceof ParseFailureListener) {
                ((ParseFailureListener) eventHandler).parsingError(pattern, e.getMessage());
            }
        }
    }
    ImmutableList<SkyKey> skyKeys = builder.build();
    return parseTargetPatternKeys(targetPatterns, skyKeys, SkyframeExecutor.DEFAULT_THREAD_COUNT, keepGoing, eventHandler, createTargetPatternEvaluatorUtil(policy, eventHandler, keepGoing));
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ImmutableList(com.google.common.collect.ImmutableList) ParseFailureListener(com.google.devtools.build.lib.pkgcache.ParseFailureListener) TargetPatternSkyKeyOrException(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException)

Example 2 with TargetPatternSkyKeyOrException

use of com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException in project bazel by bazelbuild.

the class TargetPatternPhaseFunction method determineTests.

/**
   * Interpret test target labels from the command-line arguments and return the corresponding set
   * of targets, handling the filter flags, and expanding test suites.
   *
   * @param targetPatterns the list of command-line target patterns specified by the user
   * @param testFilter the test filter
   */
private static ResolvedTargets<Target> determineTests(Environment env, List<String> targetPatterns, String offset, TestFilter testFilter) throws InterruptedException {
    List<SkyKey> patternSkyKeys = new ArrayList<>();
    for (TargetPatternSkyKeyOrException keyOrException : TargetPatternValue.keys(targetPatterns, FilteringPolicies.FILTER_TESTS, offset)) {
        try {
            patternSkyKeys.add(keyOrException.getSkyKey());
        } catch (TargetParsingException e) {
        // Skip.
        }
    }
    Map<SkyKey, ValueOrException<TargetParsingException>> resolvedPatterns = env.getValuesOrThrow(patternSkyKeys, TargetParsingException.class);
    if (env.valuesMissing()) {
        return null;
    }
    List<SkyKey> expandedSuiteKeys = new ArrayList<>();
    for (SkyKey key : patternSkyKeys) {
        TargetPatternValue value;
        try {
            value = (TargetPatternValue) resolvedPatterns.get(key).get();
        } catch (TargetParsingException e) {
            // Skip.
            continue;
        }
        expandedSuiteKeys.add(TestSuiteExpansionValue.key(value.getTargets().getTargets()));
    }
    Map<SkyKey, SkyValue> expandedSuites = env.getValues(expandedSuiteKeys);
    if (env.valuesMissing()) {
        return null;
    }
    ResolvedTargets.Builder<Target> testTargetsBuilder = ResolvedTargets.builder();
    for (SkyKey key : patternSkyKeys) {
        TargetPatternKey pattern = (TargetPatternKey) key.argument();
        TargetPatternValue value;
        try {
            value = (TargetPatternValue) resolvedPatterns.get(key).get();
        } catch (TargetParsingException e) {
            // This was already reported in getTargetsToBuild (maybe merge the two code paths?).
            continue;
        }
        TestSuiteExpansionValue expandedSuitesValue = (TestSuiteExpansionValue) expandedSuites.get(TestSuiteExpansionValue.key(value.getTargets().getTargets()));
        if (pattern.isNegative()) {
            ResolvedTargets<Target> negativeTargets = expandedSuitesValue.getTargets();
            testTargetsBuilder.filter(Predicates.not(Predicates.in(negativeTargets.getTargets())));
            testTargetsBuilder.mergeError(negativeTargets.hasError());
        } else {
            ResolvedTargets<Target> positiveTargets = expandedSuitesValue.getTargets();
            testTargetsBuilder.addAll(positiveTargets.getTargets());
            testTargetsBuilder.mergeError(positiveTargets.hasError());
        }
    }
    testTargetsBuilder.filter(testFilter);
    return testTargetsBuilder.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) TargetPatternKey(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey) ArrayList(java.util.ArrayList) TargetPatternSkyKeyOrException(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException) ValueOrException(com.google.devtools.build.skyframe.ValueOrException) SkyValue(com.google.devtools.build.skyframe.SkyValue) Target(com.google.devtools.build.lib.packages.Target) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets)

Example 3 with TargetPatternSkyKeyOrException

use of com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException in project bazel by bazelbuild.

the class TargetPatternPhaseFunction method getTargetsToBuild.

/**
   * Interpret the command-line arguments.
   *
   * @param options the command-line arguments in structured form
   */
private static ResolvedTargets<Target> getTargetsToBuild(Environment env, TargetPatternList options) throws InterruptedException {
    List<SkyKey> patternSkyKeys = new ArrayList<>();
    for (TargetPatternSkyKeyOrException keyOrException : TargetPatternValue.keys(options.getTargetPatterns(), FilteringPolicies.FILTER_MANUAL, options.getOffset())) {
        try {
            patternSkyKeys.add(keyOrException.getSkyKey());
        } catch (TargetParsingException e) {
        // Skip.
        }
    }
    Map<SkyKey, ValueOrException<TargetParsingException>> resolvedPatterns = env.getValuesOrThrow(patternSkyKeys, TargetParsingException.class);
    if (env.valuesMissing()) {
        return null;
    }
    ResolvedTargets.Builder<Target> builder = ResolvedTargets.builder();
    for (SkyKey key : patternSkyKeys) {
        TargetPatternKey pattern = (TargetPatternKey) key.argument();
        TargetPatternValue value;
        try {
            value = (TargetPatternValue) resolvedPatterns.get(key).get();
        } catch (TargetParsingException e) {
            // TODO(ulfjack): Report to EventBus.
            String rawPattern = pattern.getPattern();
            String errorMessage = e.getMessage();
            env.getListener().handle(Event.error("Skipping '" + rawPattern + "': " + errorMessage));
            builder.setError();
            continue;
        }
        // TODO(ulfjack): This is terribly inefficient.
        ResolvedTargets<Target> asTargets = TestSuiteExpansionFunction.labelsToTargets(env, value.getTargets().getTargets(), value.getTargets().hasError());
        if (pattern.isNegative()) {
            builder.filter(Predicates.not(Predicates.in(asTargets.getTargets())));
        } else {
            builder.merge(asTargets);
        }
    }
    ResolvedTargets<Target> result = builder.filter(TargetUtils.tagFilter(options.getBuildTargetFilter())).build();
    if (options.getCompileOneDependency()) {
        TargetProvider targetProvider = new EnvironmentBackedRecursivePackageProvider(env);
        try {
            return new CompileOneDependencyTransformer(targetProvider).transformCompileOneDependency(env.getListener(), result);
        } catch (MissingDepException e) {
            return null;
        } catch (TargetParsingException e) {
            env.getListener().handle(Event.error(e.getMessage()));
            return ResolvedTargets.failed();
        }
    }
    return result;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) TargetPatternKey(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey) TargetProvider(com.google.devtools.build.lib.pkgcache.TargetProvider) ArrayList(java.util.ArrayList) TargetPatternSkyKeyOrException(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException) ValueOrException(com.google.devtools.build.skyframe.ValueOrException) CompileOneDependencyTransformer(com.google.devtools.build.lib.pkgcache.CompileOneDependencyTransformer) Target(com.google.devtools.build.lib.packages.Target) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ResolvedTargets(com.google.devtools.build.lib.cmdline.ResolvedTargets) MissingDepException(com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException)

Example 4 with TargetPatternSkyKeyOrException

use of com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException in project bazel by bazelbuild.

the class PrepareDepsOfPatternValue method excludedDirectoriesBeneath.

private static ImmutableSet<PathFragment> excludedDirectoriesBeneath(TargetPatternKey targetPatternKey, int position, List<TargetPatternSkyKeyOrException> keysMaybe) {
    ImmutableSet.Builder<PathFragment> excludedDirectoriesBuilder = ImmutableSet.builder();
    for (int j = position + 1; j < keysMaybe.size(); j++) {
        TargetPatternSkyKeyOrException laterPatternMaybe = keysMaybe.get(j);
        SkyKey laterSkyKey;
        try {
            laterSkyKey = laterPatternMaybe.getSkyKey();
        } catch (TargetParsingException ignored) {
            laterSkyKey = null;
        }
        if (laterSkyKey != null) {
            TargetPatternKey laterTargetPatternKey = (TargetPatternKey) laterSkyKey.argument();
            TargetPattern laterParsedPattern = laterTargetPatternKey.getParsedPattern();
            if (laterTargetPatternKey.isNegative() && laterParsedPattern.getType() == Type.TARGETS_BELOW_DIRECTORY && targetPatternKey.getParsedPattern().containsDirectoryOfTBDForTBD(laterParsedPattern)) {
                excludedDirectoriesBuilder.add(laterParsedPattern.getDirectoryForTargetsUnderDirectory().getPackageFragment());
            }
        }
    }
    return excludedDirectoriesBuilder.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) TargetPatternKey(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey) ImmutableSet(com.google.common.collect.ImmutableSet) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) TargetPatternSkyKeyOrException(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException)

Example 5 with TargetPatternSkyKeyOrException

use of com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException in project bazel by bazelbuild.

the class PrepareDepsOfPatternValue method keys.

/**
   * Returns an iterable of {@link PrepareDepsOfPatternSkyKeyOrException}, with {@link
   * TargetPatternKey} arguments. Negative target patterns of type other than {@link
   * Type#TARGETS_BELOW_DIRECTORY} are not permitted. If a provided pattern fails to parse or is
   * negative but not a {@link Type#TARGETS_BELOW_DIRECTORY}, an element in the returned iterable
   * will throw when its {@link PrepareDepsOfPatternSkyKeyOrException#getSkyKey} method is called
   * and will return the failing pattern when its {@link
   * PrepareDepsOfPatternSkyKeyOrException#getOriginalPattern} method is called.
   *
   * <p>There may be fewer returned elements than patterns provided as input. This function will
   * combine negative {@link Type#TARGETS_BELOW_DIRECTORY} patterns with preceding patterns to
   * return an iterable of SkyKeys that avoids loading excluded directories during evaluation.
   *
   * @param patterns The list of patterns, e.g. [//foo/..., -//foo/biz/...]. If a pattern's first
   *     character is "-", it is treated as a negative pattern.
   * @param offset The offset to apply to relative target patterns.
   */
@ThreadSafe
public static Iterable<PrepareDepsOfPatternSkyKeyOrException> keys(List<String> patterns, String offset) {
    List<TargetPatternSkyKeyOrException> keysMaybe = ImmutableList.copyOf(TargetPatternValue.keys(patterns, FilteringPolicies.NO_FILTER, offset));
    // This code path is evaluated only for query universe preloading, and the quadratic cost of
    // the code below (i.e. for each pattern, consider each later pattern as a candidate for
    // subdirectory exclusion) is only acceptable because all the use cases for query universe
    // preloading involve short (<10 items) pattern sequences.
    ImmutableList.Builder<PrepareDepsOfPatternSkyKeyOrException> builder = ImmutableList.builder();
    for (int i = 0; i < keysMaybe.size(); i++) {
        TargetPatternSkyKeyOrException keyMaybe = keysMaybe.get(i);
        SkyKey skyKey;
        try {
            skyKey = keyMaybe.getSkyKey();
        } catch (TargetParsingException e) {
            // keyMaybe.getSkyKey() may throw TargetParsingException if its corresponding pattern
            // failed to parse. If so, wrap the exception and return it, so that our caller can
            // deal with it.
            skyKey = null;
            builder.add(new PrepareDepsOfPatternSkyKeyException(e, keyMaybe.getOriginalPattern()));
        }
        if (skyKey != null) {
            TargetPatternKey targetPatternKey = (TargetPatternKey) skyKey.argument();
            if (targetPatternKey.isNegative()) {
                if (!targetPatternKey.getParsedPattern().getType().equals(Type.TARGETS_BELOW_DIRECTORY)) {
                    builder.add(new PrepareDepsOfPatternSkyKeyException(new TargetParsingException("Negative target patterns of types other than \"targets below directory\"" + " are not permitted."), targetPatternKey.toString()));
                }
            // Otherwise it's a negative TBD pattern which was combined with previous patterns as an
            // excluded directory. These can be skipped because there's no PrepareDepsOfPattern work
            // to be done for them.
            } else {
                builder.add(new PrepareDepsOfPatternSkyKeyValue(setExcludedDirectories(targetPatternKey, excludedDirectoriesBeneath(targetPatternKey, i, keysMaybe))));
            }
        }
    }
    return builder.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) TargetPatternKey(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ImmutableList(com.google.common.collect.ImmutableList) TargetPatternSkyKeyOrException(com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Aggregations

TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)5 TargetPatternSkyKeyOrException (com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException)5 SkyKey (com.google.devtools.build.skyframe.SkyKey)5 TargetPatternKey (com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey)4 ImmutableList (com.google.common.collect.ImmutableList)2 ResolvedTargets (com.google.devtools.build.lib.cmdline.ResolvedTargets)2 Target (com.google.devtools.build.lib.packages.Target)2 ValueOrException (com.google.devtools.build.skyframe.ValueOrException)2 ArrayList (java.util.ArrayList)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 TargetPattern (com.google.devtools.build.lib.cmdline.TargetPattern)1 ThreadSafe (com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)1 CompileOneDependencyTransformer (com.google.devtools.build.lib.pkgcache.CompileOneDependencyTransformer)1 ParseFailureListener (com.google.devtools.build.lib.pkgcache.ParseFailureListener)1 TargetProvider (com.google.devtools.build.lib.pkgcache.TargetProvider)1 MissingDepException (com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException)1 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)1 SkyValue (com.google.devtools.build.skyframe.SkyValue)1