Search in sources :

Example 6 with QueryException

use of com.google.devtools.build.lib.query2.engine.QueryException in project bazel by bazelbuild.

the class BlazeTargetAccessor method getLabelListAttr.

@Override
public List<Target> getLabelListAttr(QueryExpression caller, Target target, String attrName, String errorMsgPrefix) throws QueryException, InterruptedException {
    Preconditions.checkArgument(target instanceof Rule);
    List<Target> result = new ArrayList<>();
    Rule rule = (Rule) target;
    AggregatingAttributeMapper attrMap = AggregatingAttributeMapper.of(rule);
    Type<?> attrType = attrMap.getAttributeType(attrName);
    if (attrType == null) {
        // Return an empty list if the attribute isn't defined for this rule.
        return ImmutableList.of();
    }
    for (Label label : attrMap.getReachableLabels(attrName, false)) {
        try {
            result.add(queryEnvironment.getTarget(label));
        } catch (TargetNotFoundException e) {
            queryEnvironment.reportBuildFileError(caller, errorMsgPrefix + e.getMessage());
        }
    }
    return result;
}
Also used : Target(com.google.devtools.build.lib.packages.Target) TargetNotFoundException(com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetNotFoundException) ArrayList(java.util.ArrayList) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule) AggregatingAttributeMapper(com.google.devtools.build.lib.packages.AggregatingAttributeMapper)

Example 7 with QueryException

use of com.google.devtools.build.lib.query2.engine.QueryException in project bazel by bazelbuild.

the class SkyQueryEnvironment method getTargetsMatchingPattern.

@ThreadSafe
@Override
public QueryTaskFuture<Void> getTargetsMatchingPattern(final QueryExpression owner, String pattern, Callback<Target> callback) {
    // Directly evaluate the target pattern, making use of packages in the graph.
    Pair<TargetPattern, ImmutableSet<PathFragment>> patternToEvalAndSubdirectoriesToExclude;
    try {
        patternToEvalAndSubdirectoriesToExclude = getPatternAndExcludes(pattern);
    } catch (TargetParsingException tpe) {
        try {
            reportBuildFileError(owner, tpe.getMessage());
        } catch (QueryException qe) {
            return immediateFailedFuture(qe);
        }
        return immediateSuccessfulFuture(null);
    } catch (InterruptedException ie) {
        return immediateCancelledFuture();
    }
    TargetPattern patternToEval = patternToEvalAndSubdirectoriesToExclude.getFirst();
    ImmutableSet<PathFragment> subdirectoriesToExclude = patternToEvalAndSubdirectoriesToExclude.getSecond();
    AsyncFunction<TargetParsingException, Void> reportBuildFileErrorAsyncFunction = new AsyncFunction<TargetParsingException, Void>() {

        @Override
        public ListenableFuture<Void> apply(TargetParsingException exn) throws QueryException {
            reportBuildFileError(owner, exn.getMessage());
            return Futures.immediateFuture(null);
        }
    };
    ListenableFuture<Void> evalFuture = patternToEval.evalAsync(resolver, subdirectoriesToExclude, callback, QueryException.class, executor);
    return QueryTaskFutureImpl.ofDelegate(Futures.catchingAsync(evalFuture, TargetParsingException.class, reportBuildFileErrorAsyncFunction));
}
Also used : QueryException(com.google.devtools.build.lib.query2.engine.QueryException) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) ImmutableSet(com.google.common.collect.ImmutableSet) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 8 with QueryException

use of com.google.devtools.build.lib.query2.engine.QueryException in project bazel by bazelbuild.

the class QueryParser method parseBinaryOperatorTail.

/**
   * tail ::= ( <op> <primary> )*
   * All operators have equal precedence.
   * This factoring is required for left-associative binary operators in LL(1).
   */
private QueryExpression parseBinaryOperatorTail(QueryExpression lhs) throws QueryException {
    if (!BINARY_OPERATORS.contains(token.kind)) {
        return lhs;
    }
    List<QueryExpression> operands = new ArrayList<>();
    operands.add(lhs);
    TokenKind lastOperator = token.kind;
    while (BINARY_OPERATORS.contains(token.kind)) {
        TokenKind operator = token.kind;
        consume(operator);
        if (operator != lastOperator) {
            lhs = new BinaryOperatorExpression(lastOperator, operands);
            operands.clear();
            operands.add(lhs);
            lastOperator = operator;
        }
        QueryExpression rhs = parsePrimary();
        operands.add(rhs);
    }
    return new BinaryOperatorExpression(lastOperator, operands);
}
Also used : TokenKind(com.google.devtools.build.lib.query2.engine.Lexer.TokenKind) ArrayList(java.util.ArrayList)

Example 9 with QueryException

use of com.google.devtools.build.lib.query2.engine.QueryException in project bazel by bazelbuild.

the class AbstractBlazeQueryEnvironment method evaluateQuery.

/**
   * Evaluate the specified query expression in this environment, streaming results to the given
   * {@code callback}. {@code callback.start()} will be called before query evaluation and
   * {@code callback.close()} will be unconditionally called at the end of query evaluation
   * (i.e. regardless of whether it was successful).
   *
   * @return a {@link QueryEvalResult} object that contains the resulting set of targets and a bit
   *   to indicate whether errors occurred during evaluation; note that the
   *   success status can only be false if {@code --keep_going} was in effect
   * @throws QueryException if the evaluation failed and {@code --nokeep_going} was in
   *   effect
   */
public QueryEvalResult evaluateQuery(QueryExpression expr, ThreadSafeOutputFormatterCallback<T> callback) throws QueryException, InterruptedException, IOException {
    EmptinessSensingCallback<T> emptySensingCallback = new EmptinessSensingCallback<>(callback);
    long startTime = System.currentTimeMillis();
    // In the --nokeep_going case, errors are reported in the order in which the patterns are
    // specified; using a linked hash set here makes sure that the left-most error is reported.
    Set<String> targetPatternSet = new LinkedHashSet<>();
    expr.collectTargetPatterns(targetPatternSet);
    try {
        preloadOrThrow(expr, targetPatternSet);
    } catch (TargetParsingException e) {
        // Unfortunately, by evaluating the patterns in parallel, we lose some location information.
        throw new QueryException(expr, e.getMessage());
    }
    IOException ioExn = null;
    boolean failFast = true;
    try {
        callback.start();
        evalTopLevelInternal(expr, emptySensingCallback);
        failFast = false;
    } catch (QueryException e) {
        throw new QueryException(e, expr);
    } catch (InterruptedException e) {
        throw e;
    } finally {
        try {
            callback.close(failFast);
        } catch (IOException e) {
            // Only throw this IOException if we weren't about to throw a different exception.
            ioExn = e;
        }
    }
    if (ioExn != null) {
        throw ioExn;
    }
    long elapsedTime = System.currentTimeMillis() - startTime;
    if (elapsedTime > 1) {
        logger.info("Spent " + elapsedTime + " milliseconds evaluating query");
    }
    if (eventHandler.hasErrors()) {
        if (!keepGoing) {
            // of target patterns that don't cause evaluation to fail per se.
            throw new QueryException("Evaluation of query \"" + expr + "\" failed due to BUILD file errors");
        } else {
            eventHandler.handle(Event.warn("--keep_going specified, ignoring errors.  " + "Results may be inaccurate"));
        }
    }
    return new QueryEvalResult(!eventHandler.hasErrors(), emptySensingCallback.isEmpty());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QueryEvalResult(com.google.devtools.build.lib.query2.engine.QueryEvalResult) IOException(java.io.IOException) QueryException(com.google.devtools.build.lib.query2.engine.QueryException) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException)

Example 10 with QueryException

use of com.google.devtools.build.lib.query2.engine.QueryException in project bazel by bazelbuild.

the class GenQuery method doQuery.

@SuppressWarnings("unchecked")
@Nullable
private byte[] doQuery(QueryOptions queryOptions, PackageProvider packageProvider, Predicate<Label> labelFilter, TargetPatternEvaluator evaluator, String query, RuleContext ruleContext) throws InterruptedException {
    DigraphQueryEvalResult<Target> queryResult;
    OutputFormatter formatter;
    AggregateAllOutputFormatterCallback<Target> targets = QueryUtil.newOrderedAggregateAllOutputFormatterCallback();
    try {
        Set<Setting> settings = queryOptions.toSettings();
        // Turns out, if we have two targets with a cycle of length 2 were one of
        // the edges is of type NODEP_LABEL type, the targets both show up in
        // each other's result for deps(X) when the query is executed using
        // 'blaze query'. This obviously does not fly when doing the query as a
        // part of the build, thus, there is a slight discrepancy between the
        // behavior of the query engine in these two use cases.
        settings.add(Setting.NO_NODEP_DEPS);
        ImmutableList<OutputFormatter> outputFormatters = QUERY_OUTPUT_FORMATTERS.get(ruleContext.getAnalysisEnvironment().getSkyframeEnv());
        // This is a precomputed value so it should have been injected by the rules module by the
        // time we get there.
        formatter = OutputFormatter.getFormatter(Preconditions.checkNotNull(outputFormatters), queryOptions.outputFormat);
        // All the packages are already loaded at this point, so there is no need
        // to start up many threads. 4 are started up to make good use of multiple
        // cores.
        BlazeQueryEnvironment queryEnvironment = (BlazeQueryEnvironment) QUERY_ENVIRONMENT_FACTORY.create(/*transitivePackageLoader=*/
        null, /*graph=*/
        null, packageProvider, evaluator, /*keepGoing=*/
        false, ruleContext.attributes().get("strict", Type.BOOLEAN), /*orderedResults=*/
        !QueryOutputUtils.shouldStreamResults(queryOptions, formatter), /*universeScope=*/
        ImmutableList.<String>of(), /*loadingPhaseThreads=*/
        4, labelFilter, getEventHandler(ruleContext), settings, ImmutableList.<QueryFunction>of(), /*packagePath=*/
        null, /*blockUniverseEvaluationErrors=*/
        false);
        queryResult = (DigraphQueryEvalResult<Target>) queryEnvironment.evaluateQuery(query, targets);
    } catch (SkyframeRestartQueryException e) {
        // inconsistent from run to run, and make detecting legitimate errors more difficult.
        return null;
    } catch (QueryException e) {
        ruleContext.ruleError("query failed: " + e.getMessage());
        return null;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        QueryOutputUtils.output(queryOptions, queryResult, targets.getResult(), formatter, outputStream, queryOptions.aspectDeps.createResolver(packageProvider, getEventHandler(ruleContext)));
    } catch (ClosedByInterruptException e) {
        throw new InterruptedException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return outputStream.toByteArray();
}
Also used : Setting(com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputFormatter(com.google.devtools.build.lib.query2.output.OutputFormatter) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) QueryException(com.google.devtools.build.lib.query2.engine.QueryException) SkyframeRestartQueryException(com.google.devtools.build.lib.query2.engine.SkyframeRestartQueryException) BlazeQueryEnvironment(com.google.devtools.build.lib.query2.BlazeQueryEnvironment) QueryFunction(com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction) SkyframeRestartQueryException(com.google.devtools.build.lib.query2.engine.SkyframeRestartQueryException) Nullable(javax.annotation.Nullable)

Aggregations

QueryException (com.google.devtools.build.lib.query2.engine.QueryException)7 IOException (java.io.IOException)5 Target (com.google.devtools.build.lib.packages.Target)4 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)3 QueryEvalResult (com.google.devtools.build.lib.query2.engine.QueryEvalResult)3 ImmutableList (com.google.common.collect.ImmutableList)2 ThreadSafe (com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)2 Setting (com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting)2 QueryExpression (com.google.devtools.build.lib.query2.engine.QueryExpression)2 OutputFormatter (com.google.devtools.build.lib.query2.output.OutputFormatter)2 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)2 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)2 SkyKey (com.google.devtools.build.skyframe.SkyKey)2 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)2 ArrayList (java.util.ArrayList)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 NoBuildEvent (com.google.devtools.build.lib.analysis.NoBuildEvent)1