use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.
the class QueryCommand method exec.
/**
* Exit codes:
* 0 on successful evaluation.
* 1 if query evaluation did not complete.
* 2 if query parsing failed.
* 3 if errors were reported but evaluation produced a partial result
* (only when --keep_going is in effect.)
*/
@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
BlazeRuntime runtime = env.getRuntime();
QueryOptions queryOptions = options.getOptions(QueryOptions.class);
try {
env.setupPackageCache(options, runtime.getDefaultsPackageContent());
} catch (InterruptedException e) {
env.getReporter().handle(Event.error("query interrupted"));
return ExitCode.INTERRUPTED;
} catch (AbruptExitException e) {
env.getReporter().handle(Event.error(null, "Unknown error: " + e.getMessage()));
return e.getExitCode();
}
String query;
if (!options.getResidue().isEmpty()) {
if (!queryOptions.queryFile.isEmpty()) {
env.getReporter().handle(Event.error("Command-line query and --query_file cannot both be specified"));
return ExitCode.COMMAND_LINE_ERROR;
}
query = Joiner.on(' ').join(options.getResidue());
} else if (!queryOptions.queryFile.isEmpty()) {
// Works for absolute or relative query file.
Path residuePath = env.getWorkingDirectory().getRelative(queryOptions.queryFile);
try {
query = new String(FileSystemUtils.readContent(residuePath), StandardCharsets.UTF_8);
} catch (IOException e) {
env.getReporter().handle(Event.error("I/O error reading from " + residuePath.getPathString()));
return ExitCode.COMMAND_LINE_ERROR;
}
} else {
env.getReporter().handle(Event.error(String.format("missing query expression. Type '%s help query' for syntax and help", runtime.getProductName())));
return ExitCode.COMMAND_LINE_ERROR;
}
Iterable<OutputFormatter> formatters = runtime.getQueryOutputFormatters();
OutputFormatter formatter = OutputFormatter.getFormatter(formatters, queryOptions.outputFormat);
if (formatter == null) {
env.getReporter().handle(Event.error(String.format("Invalid output format '%s'. Valid values are: %s", queryOptions.outputFormat, OutputFormatter.formatterNames(formatters))));
return ExitCode.COMMAND_LINE_ERROR;
}
Set<Setting> settings = queryOptions.toSettings();
boolean streamResults = QueryOutputUtils.shouldStreamResults(queryOptions, formatter);
QueryEvalResult result;
AbstractBlazeQueryEnvironment<Target> queryEnv = newQueryEnvironment(env, queryOptions.keepGoing, !streamResults, queryOptions.universeScope, queryOptions.loadingPhaseThreads, settings);
// 1. Parse and transform query:
QueryExpression expr;
try {
expr = QueryExpression.parse(query, queryEnv);
} catch (QueryException e) {
env.getReporter().handle(Event.error(null, "Error while parsing '" + query + "': " + e.getMessage()));
return ExitCode.COMMAND_LINE_ERROR;
}
expr = queryEnv.transformParsedQuery(expr);
OutputStream out = env.getReporter().getOutErr().getOutputStream();
ThreadSafeOutputFormatterCallback<Target> callback;
if (streamResults) {
disableAnsiCharactersFiltering(env);
// 2. Evaluate expression:
StreamedFormatter streamedFormatter = ((StreamedFormatter) formatter);
streamedFormatter.setOptions(queryOptions, queryOptions.aspectDeps.createResolver(env.getPackageManager(), env.getReporter()));
callback = streamedFormatter.createStreamCallback(out, queryOptions, queryEnv);
} else {
callback = QueryUtil.newOrderedAggregateAllOutputFormatterCallback();
}
boolean catastrophe = true;
try {
result = queryEnv.evaluateQuery(expr, callback);
catastrophe = false;
} catch (QueryException e) {
catastrophe = false;
// Keep consistent with reportBuildFileError()
env.getReporter().handle(Event.error(e.getMessage() == null ? e.toString() : e.getMessage()));
return ExitCode.ANALYSIS_FAILURE;
} catch (InterruptedException e) {
catastrophe = false;
IOException ioException = callback.getIoException();
if (ioException == null || ioException instanceof ClosedByInterruptException) {
env.getReporter().handle(Event.error("query interrupted"));
return ExitCode.INTERRUPTED;
} else {
env.getReporter().handle(Event.error("I/O error: " + e.getMessage()));
return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
}
} catch (IOException e) {
catastrophe = false;
env.getReporter().handle(Event.error("I/O error: " + e.getMessage()));
return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
} finally {
if (!catastrophe) {
try {
out.flush();
} catch (IOException e) {
env.getReporter().handle(Event.error("Failed to flush query results: " + e.getMessage()));
return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
}
}
}
env.getEventBus().post(new NoBuildEvent());
if (!streamResults) {
disableAnsiCharactersFiltering(env);
// 3. Output results:
try {
Set<Target> targets = ((AggregateAllOutputFormatterCallback<Target>) callback).getResult();
QueryOutputUtils.output(queryOptions, result, targets, formatter, env.getReporter().getOutErr().getOutputStream(), queryOptions.aspectDeps.createResolver(env.getPackageManager(), env.getReporter()));
} catch (ClosedByInterruptException | InterruptedException e) {
env.getReporter().handle(Event.error("query interrupted"));
return ExitCode.INTERRUPTED;
} catch (IOException e) {
env.getReporter().handle(Event.error("I/O error: " + e.getMessage()));
return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
} finally {
try {
out.flush();
} catch (IOException e) {
env.getReporter().handle(Event.error("Failed to flush query results: " + e.getMessage()));
return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
}
}
}
if (result.isEmpty()) {
env.getReporter().handle(Event.info("Empty results"));
}
return result.getSuccess() ? ExitCode.SUCCESS : ExitCode.PARTIAL_ANALYSIS_FAILURE;
}
use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.
the class CommandInterruptionTest method callingExitOnceInterruptsAndOverridesExitCode.
@Test
public void callingExitOnceInterruptsAndOverridesExitCode() throws Exception {
CommandState command = snooze.runIn(executor, dispatcher, /*expectInterruption=*/
false);
command.getModuleEnvironment().exit(new AbruptExitException(ExitCode.NO_TESTS_FOUND));
command.assertFinishedWith(ExitCode.NO_TESTS_FOUND);
}
use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.
the class CommandInterruptionTest method abruptExitCodesDontOverrideInfrastructureFailures.
@Test
public void abruptExitCodesDontOverrideInfrastructureFailures() throws Exception {
CommandState command = snooze.runIn(executor, dispatcher, /*expectInterruption=*/
true);
command.getModuleEnvironment().exit(new AbruptExitException(ExitCode.NO_TESTS_FOUND));
command.assertNotFinishedYet();
command.requestExitWith(ExitCode.BLAZE_INTERNAL_ERROR);
command.assertFinishedWith(ExitCode.BLAZE_INTERNAL_ERROR);
}
use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.
the class CommandInterruptionTest method callingExitAfterCommandCompletesDoesNothing.
@Test
public void callingExitAfterCommandCompletesDoesNothing() throws Exception {
CommandState firstCommand = snooze.runIn(executor, dispatcher, /*expectInterruption=*/
false);
firstCommand.requestExitWith(ExitCode.SUCCESS);
firstCommand.assertFinishedWith(ExitCode.SUCCESS);
CommandState newCommandOnSameThread = snooze.runIn(executor, dispatcher, /*expectInterruption=*/
false);
firstCommand.assertOnSameThreadAs(newCommandOnSameThread);
firstCommand.getModuleEnvironment().exit(new AbruptExitException(ExitCode.RUN_FAILURE));
newCommandOnSameThread.assertNotFinishedYet();
newCommandOnSameThread.requestExitWith(ExitCode.SUCCESS);
}
use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.
the class SkyframeExecutor method maybeInjectEmbeddedArtifacts.
@VisibleForTesting
void maybeInjectEmbeddedArtifacts() throws AbruptExitException {
if (!needToInjectEmbeddedArtifacts) {
return;
}
Preconditions.checkNotNull(artifactFactory.get());
Preconditions.checkNotNull(binTools);
Map<SkyKey, SkyValue> values = Maps.newHashMap();
// Blaze separately handles the symlinks that target these binaries. See BinTools#setupTool.
for (Artifact artifact : binTools.getAllEmbeddedArtifacts(artifactFactory.get())) {
FileArtifactValue fileArtifactValue;
try {
fileArtifactValue = FileArtifactValue.create(artifact);
} catch (IOException e) {
// See ExtractData in blaze.cc.
String message = "Error: corrupt installation: file " + artifact.getPath() + " missing. " + "Please remove '" + directories.getInstallBase() + "' and try again.";
throw new AbruptExitException(message, ExitCode.LOCAL_ENVIRONMENTAL_ERROR, e);
}
values.put(ArtifactSkyKey.key(artifact, /*isMandatory=*/
true), fileArtifactValue);
}
injectable().inject(values);
needToInjectEmbeddedArtifacts = false;
}
Aggregations