forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlazeCommandDispatcher.java
796 lines (724 loc) · 33 KB
/
BlazeCommandDispatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.runtime;
import static com.google.devtools.build.lib.runtime.BlazeOptionHandler.BAD_OPTION_TAG;
import static com.google.devtools.build.lib.runtime.BlazeOptionHandler.ERROR_SEPARATOR;
import static com.google.devtools.common.options.Converters.BLAZE_ALIASING_FLAG;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.GoogleLogger;
import com.google.common.io.Flushables;
import com.google.devtools.build.lib.analysis.NoBuildEvent;
import com.google.devtools.build.lib.bugreport.BugReporter;
import com.google.devtools.build.lib.bugreport.Crash;
import com.google.devtools.build.lib.bugreport.CrashContext;
import com.google.devtools.build.lib.buildeventstream.BuildEventProtocolOptions;
import com.google.devtools.build.lib.buildtool.buildevent.ProfilerStartedEvent;
import com.google.devtools.build.lib.clock.BlazeClock;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.events.EventKind;
import com.google.devtools.build.lib.events.ExtendedEventHandler.Postable;
import com.google.devtools.build.lib.events.PrintingEventHandler;
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.events.StoredEventHandler;
import com.google.devtools.build.lib.profiler.MemoryProfiler;
import com.google.devtools.build.lib.profiler.Profiler;
import com.google.devtools.build.lib.profiler.SilentCloseable;
import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy;
import com.google.devtools.build.lib.server.FailureDetails;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.util.AbruptExitException;
import com.google.devtools.build.lib.util.AnsiStrippingOutputStream;
import com.google.devtools.build.lib.util.DebugLoggerConfigurator;
import com.google.devtools.build.lib.util.DetailedExitCode;
import com.google.devtools.build.lib.util.InterruptedFailureDetails;
import com.google.devtools.build.lib.util.LoggingUtil;
import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.util.io.DelegatingOutErr;
import com.google.devtools.build.lib.util.io.OutErr;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.OpaqueOptionsData;
import com.google.devtools.common.options.OptionsParser;
import com.google.devtools.common.options.OptionsParsingResult;
import com.google.devtools.common.options.TriState;
import com.google.protobuf.Any;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import net.starlark.java.eval.Starlark;
/**
* Dispatches to the Blaze commands; that is, given a command line, this abstraction looks up the
* appropriate command object, parses the options required by the object, and calls its exec method.
* Also, this object provides the runtime state (BlazeRuntime) to the commands.
*/
public class BlazeCommandDispatcher implements CommandDispatcher {
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
public static final int UNKNOWN_SERVER_PID = -1;
private static final ImmutableList<String> HELP_COMMAND = ImmutableList.of("help");
private static final ImmutableSet<String> ALL_HELP_OPTIONS =
ImmutableSet.of("--help", "-help", "-h");
private final BlazeRuntime runtime;
private final int serverPid;
private final BugReporter bugReporter;
private final Object commandLock;
private String currentClientDescription = null;
private final AtomicReference<String> shutdownReason = new AtomicReference<>();
private OutputStream logOutputStream = null;
private final LoadingCache<BlazeCommand, OpaqueOptionsData> optionsDataCache =
Caffeine.newBuilder()
.build(
new CacheLoader<BlazeCommand, OpaqueOptionsData>() {
@Override
public OpaqueOptionsData load(BlazeCommand command) {
return OptionsParser.getOptionsData(
BlazeCommandUtils.getOptions(
command.getClass(),
runtime.getBlazeModules(),
runtime.getRuleClassProvider()));
}
});
BlazeCommandDispatcher(BlazeRuntime runtime, int serverPid) {
this(runtime, serverPid, runtime.getBugReporter());
}
/** Convenience test-only constructor. */
@VisibleForTesting
public BlazeCommandDispatcher(BlazeRuntime runtime) {
this(runtime, UNKNOWN_SERVER_PID, runtime.getBugReporter());
}
/** Convenience test-only constructor. */
@VisibleForTesting
BlazeCommandDispatcher(BlazeRuntime runtime, BugReporter bugReporter) {
this(runtime, UNKNOWN_SERVER_PID, bugReporter);
}
private BlazeCommandDispatcher(BlazeRuntime runtime, int serverPid, BugReporter bugReporter) {
this.runtime = runtime;
this.serverPid = serverPid;
this.bugReporter = bugReporter;
this.commandLock = new Object();
}
@Override
public BlazeCommandResult exec(
InvocationPolicy invocationPolicy,
List<String> args,
OutErr outErr,
LockingMode lockingMode,
String clientDescription,
long firstContactTimeMillis,
Optional<List<Pair<String, String>>> startupOptionsTaggedWithBazelRc,
List<Any> commandExtensions)
throws InterruptedException {
OriginalUnstructuredCommandLineEvent originalCommandLine =
new OriginalUnstructuredCommandLineEvent(args);
Preconditions.checkNotNull(clientDescription);
if (args.isEmpty()) { // Default to help command if no arguments specified.
args = HELP_COMMAND;
}
String commandName = args.get(0);
// Be gentle to users who want to find out about Blaze invocation.
if (ALL_HELP_OPTIONS.contains(commandName)) {
commandName = "help";
}
BlazeCommand command = runtime.getCommandMap().get(commandName);
if (command == null) {
outErr.printErrLn(
String.format(
"Command '%s' not found. Try '%s help'.", commandName, runtime.getProductName()));
return createDetailedCommandResult(
String.format("Command '%s' not found.", commandName),
FailureDetails.Command.Code.COMMAND_NOT_FOUND);
}
// Take the exclusive server lock. If we fail, we busy-wait until the lock becomes available.
//
// We used to rely on commandLock.wait() to lazy-wait for the lock to become available, which is
// theoretically fine, but doing so prevents us from determining if the PID of the server
// holding the lock has changed under the hood. There have been multiple bug reports where
// users (especially macOS ones) mention that the Blaze invocation hangs on a non-existent PID.
// This should help troubleshoot those scenarios in case there really is a bug somewhere.
boolean multipleAttempts = false;
long clockBefore = BlazeClock.nanoTime();
String otherClientDescription = "";
// TODO(ulfjack): Add lock acquisition to the profiler.
synchronized (commandLock) {
while (currentClientDescription != null) {
switch (lockingMode) {
case WAIT:
if (!otherClientDescription.equals(currentClientDescription)) {
String serverDescription =
serverPid == UNKNOWN_SERVER_PID ? "" : (" (server_pid=" + serverPid + ")");
outErr.printErrLn(
String.format(
"Another command (%s) is running. Waiting for it to complete on the"
+ " server%s...",
currentClientDescription, serverDescription));
otherClientDescription = currentClientDescription;
}
commandLock.wait(500);
break;
case ERROR_OUT:
String message =
String.format(
"Another command (%s) is running. Exiting immediately.",
currentClientDescription);
outErr.printErrLn(message);
return createDetailedCommandResult(
message, FailureDetails.Command.Code.ANOTHER_COMMAND_RUNNING);
default:
throw new IllegalStateException();
}
multipleAttempts = true;
}
currentClientDescription = clientDescription;
}
// If we took the lock on the first try, force the reported wait time to 0 to avoid unnecessary
// noise in the logs. In this metric, we are only interested in knowing how long it took for
// other commands to complete, not how fast acquiring a lock is.
long waitTimeInMs =
!multipleAttempts ? 0 : (BlazeClock.nanoTime() - clockBefore) / (1000L * 1000L);
try {
String retrievedShutdownReason = this.shutdownReason.get();
if (retrievedShutdownReason != null) {
outErr.printErrLn(retrievedShutdownReason);
return createDetailedCommandResult(
retrievedShutdownReason, FailureDetails.Command.Code.PREVIOUSLY_SHUTDOWN);
}
BlazeCommandResult result =
execExclusively(
originalCommandLine,
invocationPolicy,
args,
outErr,
firstContactTimeMillis,
commandName,
command,
waitTimeInMs,
startupOptionsTaggedWithBazelRc,
commandExtensions);
if (result.shutdown()) {
setShutdownReason(
"Server shut down "
+ (result.getExitCode().isInfrastructureFailure()
? "due to a crash: " + result.getFailureDetail().getMessage()
: "explicitly by client " + clientDescription));
}
if (!result.getDetailedExitCode().isSuccess()) {
logger.atInfo().log("Exit status was %s", result.getDetailedExitCode());
}
return result;
} finally {
synchronized (commandLock) {
currentClientDescription = null;
commandLock.notify();
}
}
}
/**
* For testing ONLY. Same as {@link #exec(InvocationPolicy, List, OutErr, LockingMode, String,
* long, Optional, List)} but automatically uses the current time.
*/
@VisibleForTesting
public BlazeCommandResult exec(List<String> args, String clientDescription, OutErr originalOutErr)
throws InterruptedException {
return exec(
InvocationPolicy.getDefaultInstance(),
args,
originalOutErr,
LockingMode.ERROR_OUT,
clientDescription,
runtime.getClock().currentTimeMillis(),
/*startupOptionsTaggedWithBazelRc=*/ Optional.empty(),
/*commandExtensions=*/ ImmutableList.of());
}
private BlazeCommandResult execExclusively(
OriginalUnstructuredCommandLineEvent unstructuredServerCommandLineEvent,
InvocationPolicy invocationPolicy,
List<String> args,
OutErr outErr,
long firstContactTime,
String commandName,
BlazeCommand command,
long waitTimeInMs,
Optional<List<Pair<String, String>>> startupOptionsTaggedWithBazelRc,
List<Any> commandExtensions) {
// Record the start time for the profiler. Do not put anything before this!
long execStartTimeNanos = runtime.getClock().nanoTime();
Command commandAnnotation = command.getClass().getAnnotation(Command.class);
BlazeWorkspace workspace = runtime.getWorkspace();
StoredEventHandler storedEventHandler = new StoredEventHandler();
BlazeOptionHandler optionHandler =
new BlazeOptionHandler(
runtime,
workspace,
command,
commandAnnotation,
// Provide the options parser so that we can cache OptionsData here.
createOptionsParser(command),
invocationPolicy);
DetailedExitCode earlyExitCode = optionHandler.parseOptions(args, storedEventHandler);
OptionsParsingResult options = optionHandler.getOptionsResult();
CommandLineEvent originalCommandLineEvent =
new CommandLineEvent.OriginalCommandLineEvent(
runtime, commandName, options, startupOptionsTaggedWithBazelRc);
CommandLineEvent canonicalCommandLineEvent =
new CommandLineEvent.CanonicalCommandLineEvent(runtime, commandName, options);
// The initCommand call also records the start time for the timestamp granularity monitor.
List<String> commandEnvWarnings = new ArrayList<>();
CommandEnvironment env =
workspace.initCommand(
commandAnnotation,
options,
commandEnvWarnings,
waitTimeInMs,
firstContactTime,
commandExtensions,
this::setShutdownReason);
CommonCommandOptions commonOptions = options.getOptions(CommonCommandOptions.class);
boolean tracerEnabled = false;
if (commonOptions.enableTracer == TriState.YES) {
tracerEnabled = true;
} else if (commonOptions.enableTracer == TriState.AUTO) {
boolean commandSupportsProfile =
(commandAnnotation.builds() || "query".equals(commandName))
&& !"clean".equals(commandName)
&& !"info".equals(commandName);
tracerEnabled = commandSupportsProfile || commonOptions.profilePath != null;
}
// TODO(ulfjack): Move the profiler initialization as early in the startup sequence as possible.
// Profiler setup and shutdown must always happen in pairs. Shutdown is currently performed in
// the afterCommand call in the finally block below.
ProfilerStartedEvent profilerStartedEvent =
runtime.initProfiler(
tracerEnabled,
storedEventHandler,
workspace,
commonOptions,
options.getOptions(BuildEventProtocolOptions.class),
env,
execStartTimeNanos,
waitTimeInMs);
storedEventHandler.post(profilerStartedEvent);
// Enable Starlark CPU profiling (--starlark_cpu_profile=/tmp/foo.pprof.gz)
boolean success = false;
if (!commonOptions.starlarkCpuProfile.isEmpty()) {
FileOutputStream out;
try {
out = new FileOutputStream(commonOptions.starlarkCpuProfile);
} catch (IOException ex) {
String message = "Starlark CPU profiler: " + ex.getMessage();
outErr.printErrLn(message);
return createDetailedCommandResult(
message, FailureDetails.Command.Code.STARLARK_CPU_PROFILE_FILE_INITIALIZATION_FAILURE);
}
try {
success = Starlark.startCpuProfile(out, Duration.ofMillis(10));
} catch (IllegalStateException ex) { // e.g. SIGPROF in use
String message = Strings.nullToEmpty(ex.getMessage());
outErr.printErrLn(message);
return createDetailedCommandResult(
message, FailureDetails.Command.Code.STARLARK_CPU_PROFILING_INITIALIZATION_FAILURE);
}
}
if (!commonOptions.starlarkCoverageReport.isEmpty()) {
// Record coverage for all `.bzl` files, excluding the built-ins, which don't have paths that
// could be resolved when a human-readable coverage report is generated.
Starlark.startCoverageCollection(
path -> !path.startsWith("/virtual_builtins_bzl") && path.endsWith(".bzl"));
}
BlazeCommandResult result =
createDetailedCommandResult(
"Unknown command failure", FailureDetails.Command.Code.COMMAND_FAILURE_UNKNOWN);
boolean needToCallAfterCommand = true;
Reporter reporter = env.getReporter();
OutErr.SystemPatcher systemOutErrPatcher = reporter.getOutErr().getSystemPatcher();
try {
// Temporary: there are modules that output events during beforeCommand, but the reporter
// isn't setup yet. Add the stored event handler to catch those events.
reporter.addHandler(storedEventHandler);
for (BlazeModule module : runtime.getBlazeModules()) {
try (SilentCloseable closeable = Profiler.instance().profile(module + ".beforeCommand")) {
module.beforeCommand(env);
} catch (AbruptExitException e) {
logger.atInfo().withCause(e).log("Error in %s", module);
// Don't let one module's complaints prevent the other modules from doing necessary
// setup. We promised to call beforeCommand exactly once per-module before each command
// and will be calling afterCommand soon in the future - a module's afterCommand might
// rightfully assume its beforeCommand has already been called.
storedEventHandler.handle(Event.error(e.getMessage()));
// It's not ideal but we can only return one exit code, so we just pick the code of the
// last exception.
earlyExitCode = e.getDetailedExitCode();
}
}
reporter.removeHandler(storedEventHandler);
// Setup stdout / stderr.
outErr = tee(outErr, env.getOutputListeners());
// Early exit. We need to guarantee that the ErrOut and Reporter setup below never error out,
// so any invariants they need must be checked before this point.
if (!earlyExitCode.isSuccess()) {
replayEarlyExitEvents(
outErr,
optionHandler,
storedEventHandler,
env,
new NoBuildEvent(
commandName, firstContactTime, false, true, env.getCommandId().toString()));
result = BlazeCommandResult.detailedExitCode(earlyExitCode);
return result;
}
try (SilentCloseable closeable = Profiler.instance().profile("setup event handler")) {
UiOptions eventHandlerOptions = options.getOptions(UiOptions.class);
OutErr colorfulOutErr = outErr;
if (!eventHandlerOptions.useColor()) {
outErr = ansiStripOut(ansiStripErr(outErr));
if (!commandAnnotation.binaryStdOut()) {
colorfulOutErr = ansiStripOut(colorfulOutErr);
}
if (!commandAnnotation.binaryStdErr()) {
colorfulOutErr = ansiStripErr(colorfulOutErr);
}
}
if (!commandAnnotation.binaryStdOut()) {
outErr = bufferOut(outErr);
}
if (!commandAnnotation.binaryStdErr()) {
outErr = bufferErr(outErr);
}
DebugLoggerConfigurator.setupLogging(commonOptions.verbosity);
EventHandler handler = createEventHandler(outErr, eventHandlerOptions);
reporter.addHandler(handler);
env.getEventBus().register(handler);
// We register an ANSI-allowing handler associated with {@code handler} so that ANSI control
// codes can be re-introduced later even if blaze is invoked with --color=no. This is useful
// for commands such as 'blaze run' where the output of the final executable shouldn't be
// modified.
if (!eventHandlerOptions.useColor()) {
UiEventHandler ansiAllowingHandler =
createEventHandler(colorfulOutErr, eventHandlerOptions);
reporter.registerAnsiAllowingHandler(handler, ansiAllowingHandler);
env.getEventBus().register(new PassiveExperimentalEventHandler(ansiAllowingHandler));
}
}
try (SilentCloseable closeable = Profiler.instance().profile("replay stored events")) {
// Now we're ready to replay the events.
storedEventHandler.replayOn(reporter);
for (String warning : commandEnvWarnings) {
reporter.handle(Event.warn(warning));
}
}
try (SilentCloseable closeable = Profiler.instance().profile("announce rc options")) {
if (commonOptions.announceRcOptions) {
if (startupOptionsTaggedWithBazelRc.isPresent()) {
String lastBlazerc = "";
List<String> accumulatedStartupOptions = new ArrayList<>();
for (Pair<String, String> option : startupOptionsTaggedWithBazelRc.get()) {
// Do not include the command line options, marked by the empty string.
if (option.getFirst().isEmpty()) {
continue;
}
// If we've moved to a new blazerc in the list, print out the info from the last one,
// and clear the accumulated list.
if (!lastBlazerc.isEmpty() && !option.getFirst().equals(lastBlazerc)) {
String logMessage =
String.format(
"Reading 'startup' options from %s: %s",
lastBlazerc, String.join(", ", accumulatedStartupOptions));
reporter.handle(Event.info(logMessage));
accumulatedStartupOptions = new ArrayList<>();
}
lastBlazerc = option.getFirst();
accumulatedStartupOptions.add(option.getSecond());
}
// Print out the final blazerc-grouped list, if any startup options were provided by
// blazerc.
if (!lastBlazerc.isEmpty()) {
String logMessage =
String.format(
"Reading 'startup' options from %s: %s",
lastBlazerc, String.join(", ", accumulatedStartupOptions));
reporter.handle(Event.info(logMessage));
}
}
for (String note : optionHandler.getRcfileNotes()) {
reporter.handle(Event.info(note));
}
}
}
// While a Blaze command is active, direct all errors to the client's event handler (and
// out/err streams).
systemOutErrPatcher.start();
try (SilentCloseable closeable = Profiler.instance().profile("CommandEnv.beforeCommand")) {
// Notify the BlazeRuntime, so it can do some initial setup.
env.beforeCommand(invocationPolicy);
} catch (AbruptExitException e) {
logger.atInfo().withCause(e).log("Error before command");
reporter.handle(Event.error(e.getMessage()));
result = BlazeCommandResult.detailedExitCode(e.getDetailedExitCode());
return result;
}
// Log the command line now that the modules have all had a change to register their listeners
// to the event bus.
env.getEventBus().post(unstructuredServerCommandLineEvent);
env.getEventBus().post(originalCommandLineEvent);
env.getEventBus().post(canonicalCommandLineEvent);
env.getEventBus().post(commonOptions.toolCommandLine);
for (BlazeModule module : runtime.getBlazeModules()) {
try (SilentCloseable closeable =
Profiler.instance().profile(module + ".injectExtraPrecomputedValues")) {
env.getSkyframeExecutor().injectExtraPrecomputedValues(module.getPrecomputedValues());
}
}
// {@link CleanCommand} is annotated with {@code builds = true}
// to have access to relevant build options but don't actually do building.
// {@link InfoCommand} is annotated with {@code builds = true} but only conditionally
// does this step based on some complicated logic.
if (commandAnnotation.builds()
&& !commandAnnotation.name().equals("clean")
&& !commandAnnotation.name().equals("info")) {
try {
env.syncPackageLoading(options);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
String message = "command interrupted while syncing package loading";
reporter.handle(Event.error(message));
earlyExitCode = InterruptedFailureDetails.detailedExitCode(message);
} catch (AbruptExitException e) {
logger.atInfo().withCause(e).log("Error package loading");
reporter.handle(Event.error(e.getMessage()));
earlyExitCode = e.getDetailedExitCode();
}
if (!earlyExitCode.isSuccess()) {
replayEarlyExitEvents(
outErr,
optionHandler,
storedEventHandler,
env,
new NoBuildEvent(
commandName, firstContactTime, false, true, env.getCommandId().toString()));
result = BlazeCommandResult.detailedExitCode(earlyExitCode);
return result;
}
}
// Parse starlark options.
earlyExitCode = optionHandler.parseStarlarkOptions(env, storedEventHandler);
if (!earlyExitCode.isSuccess()) {
replayEarlyExitEvents(
outErr,
optionHandler,
storedEventHandler,
env,
new NoBuildEvent(
commandName, firstContactTime, false, true, env.getCommandId().toString()));
result = BlazeCommandResult.detailedExitCode(earlyExitCode);
return result;
}
options = optionHandler.getOptionsResult();
// Run the command.
result = command.exec(env, options);
DetailedExitCode moduleExitCode = env.precompleteCommand(result.getDetailedExitCode());
// If Blaze did not suffer an infrastructure failure, check for errors in modules.
if (!result.getExitCode().isInfrastructureFailure() && moduleExitCode != null) {
result = BlazeCommandResult.detailedExitCode(moduleExitCode);
}
// Finalize the Starlark CPU profile.
if (!commonOptions.starlarkCpuProfile.isEmpty() && success) {
try {
Starlark.stopCpuProfile();
} catch (IOException ex) {
String message = "Starlark CPU profiler: " + ex.getMessage();
reporter.handle(Event.error(message));
if (result.getDetailedExitCode().isSuccess()) { // don't clobber existing error
result =
createDetailedCommandResult(
message, FailureDetails.Command.Code.STARLARK_CPU_PROFILE_FILE_WRITE_FAILURE);
}
}
}
if (!commonOptions.starlarkCoverageReport.isEmpty()) {
FileOutputStream out;
try {
out = new FileOutputStream(commonOptions.starlarkCoverageReport);
} catch (IOException ex) {
String message = "Starlark coverage recorder: " + ex.getMessage();
outErr.printErrLn(message);
return createDetailedCommandResult(
message, FailureDetails.Command.Code.STARLARK_COVERAGE_REPORT_DUMP_FAILURE);
}
Starlark.dumpCoverage(new PrintWriter(out));
}
needToCallAfterCommand = false;
return runtime.afterCommand(env, result);
} catch (Throwable e) {
logger.atSevere().withCause(e).log("Shutting down due to exception");
Crash crash = Crash.from(e);
bugReporter.handleCrash(crash, CrashContext.keepAlive().withArgs(args));
needToCallAfterCommand = false; // We are crashing.
result = BlazeCommandResult.createShutdown(crash);
return result;
} finally {
if (needToCallAfterCommand) {
BlazeCommandResult newResult = runtime.afterCommand(env, result);
if (!newResult.equals(result)) {
logger.atWarning().log("afterCommand yielded different result: %s %s", result, newResult);
}
}
try {
Profiler.instance().stop();
MemoryProfiler.instance().stop();
} catch (IOException e) {
env.getReporter()
.handle(Event.error("Error while writing profile file: " + e.getMessage()));
}
// Swallow IOException, as we are already in a finally clause
Flushables.flushQuietly(outErr.getOutputStream());
Flushables.flushQuietly(outErr.getErrorStream());
systemOutErrPatcher.close();
env.getTimestampGranularityMonitor().waitForTimestampGranularity(outErr);
}
}
private static void replayEarlyExitEvents(
OutErr outErr,
BlazeOptionHandler optionHandler,
StoredEventHandler storedEventHandler,
CommandEnvironment env,
NoBuildEvent noBuildEvent) {
PrintingEventHandler printingEventHandler =
new PrintingEventHandler(outErr, EventKind.ALL_EVENTS);
Optional<String> badOption = retrieveBadOption(storedEventHandler.getEvents());
for (String note : optionHandler.getRcfileNotes()) {
if (badOption.isPresent()) {
if (note.contains(badOption.get())) {
printingEventHandler.handle(Event.info(note));
}
}
}
for (Event event : storedEventHandler.getEvents()) {
printingEventHandler.handle(event);
}
for (Postable post : storedEventHandler.getPosts()) {
env.getEventBus().post(post);
}
env.getEventBus().post(noBuildEvent);
}
private static Optional<String> retrieveBadOption(ImmutableList<Event> events) {
return events.stream()
.filter(e -> e.getTag() != null && e.getTag().equals(BAD_OPTION_TAG))
.map(Event::getMessage)
.filter(message -> message.contains(ERROR_SEPARATOR))
.map(message -> message.substring(0, message.indexOf(ERROR_SEPARATOR)))
.findFirst();
}
private OutErr bufferOut(OutErr outErr) {
OutputStream wrappedOut = new BufferedOutputStream(outErr.getOutputStream());
return OutErr.create(wrappedOut, outErr.getErrorStream());
}
private OutErr bufferErr(OutErr outErr) {
OutputStream wrappedErr = new BufferedOutputStream(outErr.getErrorStream());
return OutErr.create(outErr.getOutputStream(), wrappedErr);
}
private OutErr ansiStripOut(OutErr outErr) {
OutputStream wrappedOut = new AnsiStrippingOutputStream(outErr.getOutputStream());
return OutErr.create(wrappedOut, outErr.getErrorStream());
}
private OutErr ansiStripErr(OutErr outErr) {
OutputStream wrappedErr = new AnsiStrippingOutputStream(outErr.getErrorStream());
return OutErr.create(outErr.getOutputStream(), wrappedErr);
}
private OutErr tee(OutErr outErr, List<OutErr> additionalOutErrs) {
if (additionalOutErrs.isEmpty()) {
return outErr;
}
DelegatingOutErr result = new DelegatingOutErr();
result.addSink(outErr);
for (OutErr additionalOutErr : additionalOutErrs) {
result.addSink(additionalOutErr);
}
return result;
}
private void closeSilently(OutputStream logOutputStream) {
if (logOutputStream != null) {
try {
logOutputStream.close();
} catch (IOException e) {
LoggingUtil.logToRemote(Level.WARNING, "Unable to close command.log", e);
}
}
}
/**
* Creates an option parser using the common options classes and the command-specific options
* classes.
*
* <p>An overriding method should first call this method and can then override default values
* directly or by calling {@link BlazeOptionHandler#parseOptions} for command-specific options.
*/
private OptionsParser createOptionsParser(BlazeCommand command)
throws OptionsParser.ConstructionException {
OpaqueOptionsData optionsData;
optionsData = optionsDataCache.get(command);
Command annotation = command.getClass().getAnnotation(Command.class);
OptionsParser parser =
OptionsParser.builder()
.optionsData(optionsData)
.skipStarlarkOptionPrefixes()
.allowResidue(annotation.allowResidue())
.withAliasFlag(BLAZE_ALIASING_FLAG)
.build();
return parser;
}
/** Returns the event handler to use for this Blaze command. */
private UiEventHandler createEventHandler(OutErr outErr, UiOptions eventOptions) {
Path workspacePath = runtime.getWorkspace().getDirectories().getWorkspace();
PathFragment workspacePathFragment = workspacePath == null ? null : workspacePath.asFragment();
return new UiEventHandler(outErr, eventOptions, runtime.getClock(), workspacePathFragment);
}
/** Returns the runtime instance shared by the commands that this dispatcher dispatches to. */
@VisibleForTesting
public BlazeRuntime getRuntime() {
return runtime;
}
/**
* Shuts down all the registered commands to give them a chance to cleanup or close resources.
* Should be called by the owner of this command dispatcher in all termination cases.
*/
public void shutdown() {
closeSilently(logOutputStream);
logOutputStream = null;
}
private void setShutdownReason(String shutdownReason) {
this.shutdownReason.compareAndSet(null, shutdownReason);
}
private static BlazeCommandResult createDetailedCommandResult(
String message, FailureDetails.Command.Code detailedCode) {
return BlazeCommandResult.detailedExitCode(
DetailedExitCode.of(
FailureDetail.newBuilder()
.setMessage(message)
.setCommand(FailureDetails.Command.newBuilder().setCode(detailedCode))
.build()));
}
}