Skip to content

Commit 4158b61

Browse files
illicitonioncopybara-github
authored andcommitted
Use correct exit code on invalid aquery --output
Currently this exits 0, which is dangerous - scripts may assume a query succeeded with no results, where actually it never ran. Instead, properly exit with exit code 2. Closes bazelbuild#13660. PiperOrigin-RevId: 385959603
1 parent 6211b86 commit 4158b61

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
import com.google.devtools.build.lib.runtime.CommandEnvironment;
2929
import com.google.devtools.build.lib.runtime.QueryRuntimeHelper;
3030
import com.google.devtools.build.lib.runtime.QueryRuntimeHelper.QueryRuntimeHelperException;
31+
import com.google.devtools.build.lib.server.FailureDetails.ActionQuery;
3132
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
3233
import com.google.devtools.build.lib.server.FailureDetails.Query;
33-
import com.google.devtools.build.lib.server.FailureDetails.Query.Code;
3434
import com.google.devtools.build.lib.skyframe.SkyframeExecutorWrappingWalkableGraph;
3535
import com.google.devtools.build.lib.util.DetailedExitCode;
36+
import com.google.devtools.build.lib.util.ExitCode;
3637
import com.google.devtools.build.skyframe.SkyKey;
3738
import com.google.devtools.build.skyframe.WalkableGraph;
39+
import com.google.devtools.common.options.OptionsParsingException;
3840
import java.io.IOException;
3941
import java.util.Collection;
4042
import java.util.Set;
@@ -68,7 +70,7 @@ protected void postProcessAnalysisResult(BuildRequest request, AnalysisResult an
6870
.setMessage(
6971
"Queries based on analysis results are not allowed if incrementality state"
7072
+ " is not being kept")
71-
.setQuery(Query.newBuilder().setCode(Code.ANALYSIS_QUERY_PREREQ_UNMET))
73+
.setQuery(Query.newBuilder().setCode(Query.Code.ANALYSIS_QUERY_PREREQ_UNMET))
7274
.build()));
7375
}
7476
try (QueryRuntimeHelper queryRuntimeHelper =
@@ -92,13 +94,22 @@ protected void postProcessAnalysisResult(BuildRequest request, AnalysisResult an
9294
FailureDetail failureDetail =
9395
FailureDetail.newBuilder()
9496
.setMessage(errorMessage + ": " + e.getMessage())
95-
.setQuery(Query.newBuilder().setCode(Code.OUTPUT_FORMATTER_IO_EXCEPTION))
97+
.setQuery(Query.newBuilder().setCode(Query.Code.OUTPUT_FORMATTER_IO_EXCEPTION))
9698
.build();
9799
throw new ViewCreationFailedException(errorMessage, failureDetail, e);
98100
}
99101
env.getReporter().error(null, errorMessage, e);
100102
} catch (QueryRuntimeHelperException e) {
101103
throw new ExitException(DetailedExitCode.of(e.getFailureDetail()));
104+
} catch (OptionsParsingException e) {
105+
throw new ExitException(
106+
DetailedExitCode.of(
107+
ExitCode.COMMAND_LINE_ERROR,
108+
FailureDetail.newBuilder()
109+
.setMessage(e.getMessage())
110+
.setActionQuery(
111+
ActionQuery.newBuilder().setCode(ActionQuery.Code.INCORRECT_ARGUMENTS))
112+
.build()));
102113
}
103114
}
104115
}
@@ -118,7 +129,8 @@ private void doPostAnalysisQuery(
118129
Collection<SkyKey> transitiveConfigurationKeys,
119130
QueryRuntimeHelper queryRuntimeHelper,
120131
QueryExpression queryExpression)
121-
throws InterruptedException, QueryException, IOException, QueryRuntimeHelperException {
132+
throws InterruptedException, QueryException, IOException, QueryRuntimeHelperException,
133+
OptionsParsingException {
122134
WalkableGraph walkableGraph =
123135
SkyframeExecutorWrappingWalkableGraph.of(env.getSkyframeExecutor());
124136

@@ -143,14 +155,10 @@ private void doPostAnalysisQuery(
143155
NamedThreadSafeOutputFormatterCallback<T> callback =
144156
NamedThreadSafeOutputFormatterCallback.selectCallback(outputFormat, callbacks);
145157
if (callback == null) {
146-
env.getReporter()
147-
.handle(
148-
Event.error(
149-
String.format(
150-
"Invalid output format '%s'. Valid values are: %s",
151-
outputFormat,
152-
NamedThreadSafeOutputFormatterCallback.callbackNames(callbacks))));
153-
return;
158+
throw new OptionsParsingException(
159+
String.format(
160+
"Invalid output format '%s'. Valid values are: %s",
161+
outputFormat, NamedThreadSafeOutputFormatterCallback.callbackNames(callbacks)));
154162
}
155163

156164
// A certain subset of output formatters support "streaming" results - the formatter is called

0 commit comments

Comments
 (0)