blob: 7be2f31b470b7fc815a3531d7a0c5a8197b621c4 [file] [log] [blame]
Klaus Aehlig16a107d2017-05-31 18:02:43 +02001// Copyright 2017 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
ulfjack5e2133a2019-09-09 08:31:41 -070015package com.google.devtools.build.lib.analysis;
Klaus Aehlig16a107d2017-05-31 18:02:43 +020016
ulfjack5e2133a2019-09-09 08:31:41 -070017import com.google.common.annotations.VisibleForTesting;
Klaus Aehlig16a107d2017-05-31 18:02:43 +020018import com.google.common.collect.ImmutableList;
19import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
Klaus Aehliga708a022017-07-11 12:54:40 +020020import com.google.devtools.build.lib.buildeventstream.BuildEvent;
ulfjack26e586d2018-05-17 08:42:13 -070021import com.google.devtools.build.lib.buildeventstream.BuildEventContext;
janakr3ca24682020-04-01 09:12:03 -070022import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil;
Klaus Aehlig16a107d2017-05-31 18:02:43 +020023import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
janakr3ca24682020-04-01 09:12:03 -070024import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId;
Klaus Aehliga708a022017-07-11 12:54:40 +020025import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration;
Klaus Aehlig16a107d2017-05-31 18:02:43 +020026import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent;
27import com.google.devtools.build.lib.cmdline.Label;
adgar412e03c2020-05-07 08:20:55 -070028import java.util.ArrayList;
Klaus Aehlig16a107d2017-05-31 18:02:43 +020029import java.util.Collection;
adgar412e03c2020-05-07 08:20:55 -070030import javax.annotation.Nullable;
Klaus Aehlig16a107d2017-05-31 18:02:43 +020031
ulfjack5e2133a2019-09-09 08:31:41 -070032/**
33 * Error message of an analysis root cause. This is separate from {@link AnalysisFailureEvent} to
34 * avoid duplicating error messages in the stream if multiple targets fail due to the same root
35 * cause. It also allows UIs to collate errors by root cause.
36 */
37public class AnalysisRootCauseEvent implements BuildEventWithConfiguration {
38 private final BuildConfiguration configuration;
39 private final Label label;
40 private final String errorMessage;
Klaus Aehlig16a107d2017-05-31 18:02:43 +020041
ulfjack5e2133a2019-09-09 08:31:41 -070042 public AnalysisRootCauseEvent(
adgar412e03c2020-05-07 08:20:55 -070043 @Nullable BuildConfiguration configuration, Label label, String errorMessage) {
Klaus Aehlig16a107d2017-05-31 18:02:43 +020044 this.configuration = configuration;
45 this.label = label;
46 this.errorMessage = errorMessage;
47 }
48
ulfjack5e2133a2019-09-09 08:31:41 -070049 @VisibleForTesting
50 public Label getLabel() {
51 return label;
52 }
53
Klaus Aehlig16a107d2017-05-31 18:02:43 +020054 @Override
55 public BuildEventId getEventId() {
ulfjack904a8d62018-05-29 05:17:35 -070056 // This needs to match AnalysisFailedCause.
57 if (configuration == null) {
janakr3ca24682020-04-01 09:12:03 -070058 return BuildEventIdUtil.unconfiguredLabelId(label);
ulfjack904a8d62018-05-29 05:17:35 -070059 }
janakr3ca24682020-04-01 09:12:03 -070060 return BuildEventIdUtil.configuredLabelId(label, configuration.getEventId());
Klaus Aehlig16a107d2017-05-31 18:02:43 +020061 }
62
63 @Override
64 public Collection<BuildEventId> getChildrenEvents() {
65 return ImmutableList.<BuildEventId>of();
66 }
67
68 @Override
ulfjack26e586d2018-05-17 08:42:13 -070069 public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) {
Klaus Aehlig16a107d2017-05-31 18:02:43 +020070 return GenericBuildEvent.protoChaining(this)
Klaus Aehligf6b1ef92017-07-24 11:30:25 +020071 .setAborted(
72 BuildEventStreamProtos.Aborted.newBuilder()
73 .setReason(BuildEventStreamProtos.Aborted.AbortReason.ANALYSIS_FAILURE)
74 .setDescription(errorMessage)
75 .build())
Klaus Aehlig16a107d2017-05-31 18:02:43 +020076 .build();
77 }
78
79 @Override
Klaus Aehliga708a022017-07-11 12:54:40 +020080 public Collection<BuildEvent> getConfigurations() {
adgar412e03c2020-05-07 08:20:55 -070081 ArrayList<BuildEvent> result = new ArrayList<>();
82 if (configuration == null) {
83 result.add(null);
84 } else {
85 result.add(configuration.toBuildEvent());
86 }
87 return result;
Klaus Aehlig16a107d2017-05-31 18:02:43 +020088 }
89}