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;
}
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));
}
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);
}
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());
}
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();
}
Aggregations