Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 1 | // 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 | |
ulfjack | 5e2133a | 2019-09-09 08:31:41 -0700 | [diff] [blame] | 15 | package com.google.devtools.build.lib.analysis; |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 16 | |
ulfjack | 5e2133a | 2019-09-09 08:31:41 -0700 | [diff] [blame] | 17 | import com.google.common.annotations.VisibleForTesting; |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 20 | import com.google.devtools.build.lib.buildeventstream.BuildEvent; |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.buildeventstream.BuildEventContext; |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil; |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 23 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos; |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 25 | import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration; |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent; |
| 27 | import com.google.devtools.build.lib.cmdline.Label; |
adgar | 412e03c | 2020-05-07 08:20:55 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 29 | import java.util.Collection; |
adgar | 412e03c | 2020-05-07 08:20:55 -0700 | [diff] [blame] | 30 | import javax.annotation.Nullable; |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 31 | |
ulfjack | 5e2133a | 2019-09-09 08:31:41 -0700 | [diff] [blame] | 32 | /** |
| 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 | */ |
| 37 | public class AnalysisRootCauseEvent implements BuildEventWithConfiguration { |
| 38 | private final BuildConfiguration configuration; |
| 39 | private final Label label; |
| 40 | private final String errorMessage; |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 41 | |
ulfjack | 5e2133a | 2019-09-09 08:31:41 -0700 | [diff] [blame] | 42 | public AnalysisRootCauseEvent( |
adgar | 412e03c | 2020-05-07 08:20:55 -0700 | [diff] [blame] | 43 | @Nullable BuildConfiguration configuration, Label label, String errorMessage) { |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 44 | this.configuration = configuration; |
| 45 | this.label = label; |
| 46 | this.errorMessage = errorMessage; |
| 47 | } |
| 48 | |
ulfjack | 5e2133a | 2019-09-09 08:31:41 -0700 | [diff] [blame] | 49 | @VisibleForTesting |
| 50 | public Label getLabel() { |
| 51 | return label; |
| 52 | } |
| 53 | |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 54 | @Override |
| 55 | public BuildEventId getEventId() { |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 56 | // This needs to match AnalysisFailedCause. |
| 57 | if (configuration == null) { |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 58 | return BuildEventIdUtil.unconfiguredLabelId(label); |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 59 | } |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 60 | return BuildEventIdUtil.configuredLabelId(label, configuration.getEventId()); |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Collection<BuildEventId> getChildrenEvents() { |
| 65 | return ImmutableList.<BuildEventId>of(); |
| 66 | } |
| 67 | |
| 68 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 69 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 70 | return GenericBuildEvent.protoChaining(this) |
Klaus Aehlig | f6b1ef9 | 2017-07-24 11:30:25 +0200 | [diff] [blame] | 71 | .setAborted( |
| 72 | BuildEventStreamProtos.Aborted.newBuilder() |
| 73 | .setReason(BuildEventStreamProtos.Aborted.AbortReason.ANALYSIS_FAILURE) |
| 74 | .setDescription(errorMessage) |
| 75 | .build()) |
Klaus Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 76 | .build(); |
| 77 | } |
| 78 | |
| 79 | @Override |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 80 | public Collection<BuildEvent> getConfigurations() { |
adgar | 412e03c | 2020-05-07 08:20:55 -0700 | [diff] [blame] | 81 | 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 Aehlig | 16a107d | 2017-05-31 18:02:43 +0200 | [diff] [blame] | 88 | } |
| 89 | } |