Search in sources :

Example 11 with TargetParsingException

use of com.google.devtools.build.lib.cmdline.TargetParsingException 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 12 with TargetParsingException

use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.

the class SkyframeTargetPatternEvaluator method parseTargetPatternKeys.

ResolvedTargets<Target> parseTargetPatternKeys(List<String> targetPattern, Iterable<SkyKey> patternSkyKeys, int numThreads, boolean keepGoing, ExtendedEventHandler eventHandler, TargetPatternsResultBuilder finalTargetSetEvaluator) throws InterruptedException, TargetParsingException {
    EvaluationResult<TargetPatternValue> result = skyframeExecutor.targetPatterns(patternSkyKeys, numThreads, keepGoing, eventHandler);
    String errorMessage = null;
    for (SkyKey key : patternSkyKeys) {
        TargetPatternValue resultValue = result.get(key);
        if (resultValue != null) {
            ResolvedTargets<Label> results = resultValue.getTargets();
            if (((TargetPatternValue.TargetPatternKey) key.argument()).isNegative()) {
                finalTargetSetEvaluator.addLabelsOfNegativePattern(results);
            } else {
                finalTargetSetEvaluator.addLabelsOfPositivePattern(results);
            }
        } else {
            TargetPatternValue.TargetPatternKey patternKey = (TargetPatternValue.TargetPatternKey) key.argument();
            String rawPattern = patternKey.getPattern();
            ErrorInfo error = result.errorMap().get(key);
            if (error == null) {
                Preconditions.checkState(!keepGoing);
                continue;
            }
            if (error.getException() != null) {
                // This exception may not be a TargetParsingException because in a nokeep_going build, the
                // target pattern parser may swallow a NoSuchPackageException but the framework will
                // bubble it up anyway.
                Preconditions.checkArgument(!keepGoing || error.getException() instanceof TargetParsingException, error);
                errorMessage = error.getException().getMessage();
            } else if (!Iterables.isEmpty(error.getCycleInfo())) {
                errorMessage = "cycles detected during target parsing";
                skyframeExecutor.getCyclesReporter().reportCycles(error.getCycleInfo(), key, eventHandler);
            } else {
                throw new IllegalStateException(error.toString());
            }
            if (keepGoing) {
                eventHandler.handle(Event.error("Skipping '" + rawPattern + "': " + errorMessage));
                eventHandler.post(PatternExpandingError.skipped(rawPattern, errorMessage));
            }
            finalTargetSetEvaluator.setError();
            if (eventHandler instanceof ParseFailureListener) {
                ParseFailureListener parseListener = (ParseFailureListener) eventHandler;
                parseListener.parsingError(rawPattern, errorMessage);
            }
        }
    }
    if (result.hasError()) {
        Preconditions.checkState(errorMessage != null, "unexpected errors: %s", result.errorMap());
        finalTargetSetEvaluator.setError();
        if (!keepGoing) {
            eventHandler.post(PatternExpandingError.failed(targetPattern, errorMessage));
            throw new TargetParsingException(errorMessage);
        }
    }
    WalkableGraph walkableGraph = Preconditions.checkNotNull(result.getWalkableGraph(), result);
    return finalTargetSetEvaluator.build(walkableGraph);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ErrorInfo(com.google.devtools.build.skyframe.ErrorInfo) Label(com.google.devtools.build.lib.cmdline.Label) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) WalkableGraph(com.google.devtools.build.skyframe.WalkableGraph) ParseFailureListener(com.google.devtools.build.lib.pkgcache.ParseFailureListener)

Example 13 with TargetParsingException

use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.

the class TargetPatternValue method keys.

/**
   * Returns an iterable of {@link TargetPatternSkyKeyOrException}, with {@link TargetPatternKey}
   * arguments, in the same order as the list of patterns provided as input. If a provided pattern
   * fails to parse, the element in the returned iterable corresponding to it will throw when its
   * {@link TargetPatternSkyKeyOrException#getSkyKey} method is called.
   *
   * @param patterns The list of patterns, eg "-foo/biz...". If a pattern's first character is "-",
   *     it is treated as a negative pattern.
   * @param policy The filtering policy, eg "only return test targets"
   * @param offset The offset to apply to relative target patterns.
   */
@ThreadSafe
public static Iterable<TargetPatternSkyKeyOrException> keys(List<String> patterns, FilteringPolicy policy, String offset) {
    TargetPattern.Parser parser = new TargetPattern.Parser(offset);
    ImmutableList.Builder<TargetPatternSkyKeyOrException> builder = ImmutableList.builder();
    for (String pattern : patterns) {
        boolean positive = !pattern.startsWith("-");
        String absoluteValueOfPattern = positive ? pattern : pattern.substring(1);
        TargetPattern targetPattern;
        try {
            targetPattern = parser.parse(absoluteValueOfPattern);
        } catch (TargetParsingException e) {
            builder.add(new TargetPatternSkyKeyException(e, absoluteValueOfPattern));
            continue;
        }
        TargetPatternKey targetPatternKey = new TargetPatternKey(targetPattern, positive ? policy : FilteringPolicies.NO_FILTER, /*isNegative=*/
        !positive, offset, ImmutableSet.<PathFragment>of());
        SkyKey skyKey = SkyKey.create(SkyFunctions.TARGET_PATTERN, targetPatternKey);
        builder.add(new TargetPatternSkyKeyValue(skyKey));
    }
    return builder.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ImmutableList(com.google.common.collect.ImmutableList) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 14 with TargetParsingException

use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.

the class SkylarkAspectsTest method multipleAspects.

@Test
public void multipleAspects() throws Exception {
    scratch.file("test/aspect.bzl", "def _aspect_impl(target,ctx):", "  return struct()", "my_aspect = aspect(implementation = _aspect_impl)", "def _dummy_impl(ctx):", "  pass", "r1 = rule(_dummy_impl, ", "          attrs = { 'deps' : attr.label_list(aspects = [my_aspect, my_aspect]) })");
    scratch.file("test/BUILD", "load(':aspect.bzl', 'r1')", "r1(name = 't1')");
    reporter.removeHandler(failFastHandler);
    try {
        AnalysisResult result = update("//test:r1");
        assertThat(keepGoing()).isTrue();
        assertThat(result.hasError()).isTrue();
    } catch (TargetParsingException | ViewCreationFailedException expected) {
    // expected.
    }
    assertContainsEvent("aspect //test:aspect.bzl%my_aspect added more than once");
}
Also used : ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 15 with TargetParsingException

use of com.google.devtools.build.lib.cmdline.TargetParsingException in project bazel by bazelbuild.

the class CompileOneDependencyTransformer method transformCompileOneDependency.

private Target transformCompileOneDependency(ExtendedEventHandler eventHandler, Target target) throws TargetParsingException, InterruptedException {
    if (!(target instanceof FileTarget)) {
        throw new TargetParsingException("--compile_one_dependency target '" + target.getLabel() + "' must be a file");
    }
    Rule result = null;
    Iterable<Rule> orderedRuleList = getOrderedRuleList(target.getPackage());
    for (Rule rule : orderedRuleList) {
        Set<Label> labels = getInputLabels(rule);
        if (listContainsFile(eventHandler, labels, target.getLabel(), Sets.<Label>newHashSet())) {
            if (rule.getRuleClassObject().isPreferredDependency(target.getName())) {
                result = rule;
                break;
            }
            if (result == null) {
                result = rule;
            }
        }
    }
    if (result == null) {
        throw new TargetParsingException("Couldn't find dependency on target '" + target.getLabel() + "'");
    }
    // If the rule has source targets, return it.
    if (!RawAttributeMapper.of(result).getMergedValues("srcs", BuildType.LABEL_LIST).isEmpty()) {
        return result;
    }
    // Try to find a rule in the same package that has 'result' as a dependency.
    for (Rule rule : orderedRuleList) {
        RawAttributeMapper attributes = RawAttributeMapper.of(rule);
        // We don't know which path to follow for configurable attributes, so skip them.
        if (attributes.isConfigurable("deps") || attributes.isConfigurable("srcs")) {
            continue;
        }
        RuleClass ruleClass = rule.getRuleClassObject();
        if (ruleClass.hasAttr("deps", BuildType.LABEL_LIST) && ruleClass.hasAttr("srcs", BuildType.LABEL_LIST)) {
            for (Label dep : attributes.get("deps", BuildType.LABEL_LIST)) {
                if (dep.equals(result.getLabel())) {
                    if (!attributes.get("srcs", BuildType.LABEL_LIST).isEmpty()) {
                        return rule;
                    }
                }
            }
        }
    }
    return result;
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) FileTarget(com.google.devtools.build.lib.packages.FileTarget) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Aggregations

TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)22 SkyKey (com.google.devtools.build.skyframe.SkyKey)9 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)7 Label (com.google.devtools.build.lib.cmdline.Label)6 Target (com.google.devtools.build.lib.packages.Target)6 ImmutableList (com.google.common.collect.ImmutableList)5 TargetPattern (com.google.devtools.build.lib.cmdline.TargetPattern)5 TargetPatternSkyKeyOrException (com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternSkyKeyOrException)5 ResolvedTargets (com.google.devtools.build.lib.cmdline.ResolvedTargets)4 ParseFailureListener (com.google.devtools.build.lib.pkgcache.ParseFailureListener)4 TargetPatternKey (com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey)4 Test (org.junit.Test)4 ThreadSafe (com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)3 MissingDepException (com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException)3 ValueOrException (com.google.devtools.build.skyframe.ValueOrException)3 ArrayList (java.util.ArrayList)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ViewCreationFailedException (com.google.devtools.build.lib.analysis.ViewCreationFailedException)2 ExtendedEventHandler (com.google.devtools.build.lib.events.ExtendedEventHandler)2 LoadingFailedException (com.google.devtools.build.lib.pkgcache.LoadingFailedException)2