blob: a2a537adce633344e267d1fb7f7a9d6f441986eb [file] [log] [blame]
ulfjack6192b8c2018-05-24 02:40:42 -07001// Copyright 2018 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
15package com.google.devtools.build.lib.analysis;
16
ulfjack06295192019-01-16 09:13:34 -080017import com.google.common.annotations.VisibleForTesting;
adgar412e03c2020-05-07 08:20:55 -070018import com.google.common.base.MoreObjects;
adgar24879202021-04-12 09:21:20 -070019import com.google.common.base.Preconditions;
Klaus Aehligfbdeffe2018-01-26 02:19:58 -080020import com.google.common.collect.ImmutableList;
ulfjack6192b8c2018-05-24 02:40:42 -070021import com.google.common.collect.Iterables;
adgar24879202021-04-12 09:21:20 -070022import com.google.devtools.build.lib.actions.ActionLookupKey;
Klaus Aehligfbdeffe2018-01-26 02:19:58 -080023import com.google.devtools.build.lib.buildeventstream.BuildEvent;
ulfjack26e586d2018-05-17 08:42:13 -070024import com.google.devtools.build.lib.buildeventstream.BuildEventContext;
janakr3ca24682020-04-01 09:12:03 -070025import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil;
Klaus Aehligfbdeffe2018-01-26 02:19:58 -080026import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
janakr3ca24682020-04-01 09:12:03 -070027import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId;
Klaus Aehligfbdeffe2018-01-26 02:19:58 -080028import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent;
29import com.google.devtools.build.lib.buildeventstream.NullConfiguration;
ulfjack6192b8c2018-05-24 02:40:42 -070030import com.google.devtools.build.lib.causes.Cause;
ulfjack904a8d62018-05-29 05:17:35 -070031import com.google.devtools.build.lib.cmdline.Label;
ulfjack527cef42020-01-13 01:29:21 -080032import com.google.devtools.build.lib.collect.nestedset.NestedSet;
lebab52a1902021-09-23 01:35:13 -070033import com.google.devtools.build.lib.skyframe.AspectKeyCreator.AspectKey;
janakrac2cd352017-12-20 13:37:13 -080034import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey;
Klaus Aehligfbdeffe2018-01-26 02:19:58 -080035import java.util.Collection;
ulfjack904a8d62018-05-29 05:17:35 -070036import javax.annotation.Nullable;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037
38/**
ulfjack5e2133a2019-09-09 08:31:41 -070039 * This event is fired during the build, when it becomes known that the analysis of a top-level
40 * target cannot be completed because of an error in one of its dependencies.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 */
Klaus Aehligfbdeffe2018-01-26 02:19:58 -080042public class AnalysisFailureEvent implements BuildEvent {
messac4157da2021-08-06 11:17:19 -070043 @Nullable private final AspectKey failedAspect;
janakrac2cd352017-12-20 13:37:13 -080044 private final ConfiguredTargetKey failedTarget;
Klaus Aehligfbdeffe2018-01-26 02:19:58 -080045 private final BuildEventId configuration;
ulfjack527cef42020-01-13 01:29:21 -080046 private final NestedSet<Cause> rootCauses;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010047
Klaus Aehligfbdeffe2018-01-26 02:19:58 -080048 public AnalysisFailureEvent(
adgar24879202021-04-12 09:21:20 -070049 ActionLookupKey failedTarget, BuildEventId configuration, NestedSet<Cause> rootCauses) {
50 Preconditions.checkArgument(
messac4157da2021-08-06 11:17:19 -070051 failedTarget instanceof ConfiguredTargetKey || failedTarget instanceof AspectKey);
adgar24879202021-04-12 09:21:20 -070052 if (failedTarget instanceof ConfiguredTargetKey) {
53 this.failedAspect = null;
54 this.failedTarget = (ConfiguredTargetKey) failedTarget;
55 } else {
messac4157da2021-08-06 11:17:19 -070056 this.failedAspect = (AspectKey) failedTarget;
adgar24879202021-04-12 09:21:20 -070057 this.failedTarget = failedAspect.getBaseConfiguredTargetKey();
58 }
59 if (configuration != null) {
60 this.configuration = configuration;
61 } else {
62 this.configuration = NullConfiguration.INSTANCE.getEventId();
63 }
64 this.rootCauses = rootCauses;
65 }
66
adgar412e03c2020-05-07 08:20:55 -070067 @Override
68 public String toString() {
69 return MoreObjects.toStringHelper(this)
adgar24879202021-04-12 09:21:20 -070070 .add("failedAspect", failedAspect)
adgar412e03c2020-05-07 08:20:55 -070071 .add("failedTarget", failedTarget)
72 .add("configuration", configuration)
73 .add("legacyFailureReason", getLegacyFailureReason())
74 .toString();
75 }
76
janakrac2cd352017-12-20 13:37:13 -080077 public ConfiguredTargetKey getFailedTarget() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010078 return failedTarget;
79 }
80
ulfjack06295192019-01-16 09:13:34 -080081 @VisibleForTesting
82 BuildEventId getConfigurationId() {
83 return configuration;
84 }
85
ulfjack904a8d62018-05-29 05:17:35 -070086 /**
87 * Returns the label of a single root cause. Use {@link #getRootCauses} to report all root causes.
88 */
89 @Nullable public Label getLegacyFailureReason() {
ulfjack527cef42020-01-13 01:29:21 -080090 if (rootCauses.isEmpty()) {
ulfjack904a8d62018-05-29 05:17:35 -070091 return null;
92 }
ulfjack527cef42020-01-13 01:29:21 -080093 return rootCauses.toList().get(0).getLabel();
ulfjack904a8d62018-05-29 05:17:35 -070094 }
95
ulfjack527cef42020-01-13 01:29:21 -080096 public NestedSet<Cause> getRootCauses() {
ulfjack904a8d62018-05-29 05:17:35 -070097 return rootCauses;
98 }
99
Klaus Aehligfbdeffe2018-01-26 02:19:58 -0800100 @Override
101 public BuildEventId getEventId() {
adgar24879202021-04-12 09:21:20 -0700102 if (failedAspect == null) {
103 return BuildEventIdUtil.targetCompleted(failedTarget.getLabel(), configuration);
104 } else {
105 return BuildEventIdUtil.aspectCompleted(
106 failedTarget.getLabel(), configuration, failedAspect.getAspectName());
107 }
Klaus Aehligfbdeffe2018-01-26 02:19:58 -0800108 }
109
110 @Override
111 public Collection<BuildEventId> getChildrenEvents() {
janakr3ca24682020-04-01 09:12:03 -0700112 return ImmutableList.copyOf(
113 Iterables.transform(rootCauses.toList(), cause -> cause.getIdProto()));
Klaus Aehligfbdeffe2018-01-26 02:19:58 -0800114 }
115
116 @Override
ulfjack26e586d2018-05-17 08:42:13 -0700117 public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) {
Klaus Aehligfbdeffe2018-01-26 02:19:58 -0800118 return GenericBuildEvent.protoChaining(this)
119 .setAborted(
120 BuildEventStreamProtos.Aborted.newBuilder()
121 .setReason(BuildEventStreamProtos.Aborted.AbortReason.ANALYSIS_FAILURE)
122 .build())
123 .build();
124 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100125}