-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DefaultCapabilitiesConflictHandler.java
315 lines (277 loc) · 12.9 KB
/
DefaultCapabilitiesConflictHandler.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
/*
* Copyright 2018 the original author or authors.
*
* 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 org.gradle.api.internal.artifacts.ivyservice.resolveengine.graph.conflicts;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.gradle.api.Action;
import org.gradle.api.Describable;
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.capabilities.Capability;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.graph.builder.ComponentState;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.graph.builder.NodeState;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.result.ComponentSelectionDescriptorInternal;
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.result.ComponentSelectionReasons;
import org.gradle.internal.component.external.model.CapabilityInternal;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
public class DefaultCapabilitiesConflictHandler implements CapabilitiesConflictHandler {
private final List<Resolver> resolvers = Lists.newArrayListWithExpectedSize(3);
private final Map<String, Set<NodeState>> capabilityWithoutVersionToNodes = Maps.newHashMap();
private final Deque<CapabilityConflict> conflicts = new ArrayDeque<CapabilityConflict>();
@Override
public PotentialConflict registerCandidate(CapabilitiesConflictHandler.Candidate candidate) {
CapabilityInternal capability = (CapabilityInternal) candidate.getCapability();
String group = capability.getGroup();
String name = capability.getName();
final Set<NodeState> nodes = findNodesFor(capability);
Collection<NodeState> implicitCapabilityProviders = candidate.getImplicitCapabilityProviders();
nodes.addAll(implicitCapabilityProviders);
NodeState node = candidate.getNode();
if ((nodes.add(node) || implicitCapabilityProviders.contains(node)) && nodes.size() > 1) {
// The registered nodes may contain nodes which are no longer selected.
// We don't remove them from the list in the first place because it proved to be
// slower than filtering as needed.
ModuleIdentifier rootId = null;
final List<NodeState> candidatesForConflict = Lists.newArrayListWithCapacity(nodes.size());
for (NodeState ns : nodes) {
if (ns.isSelected()) {
candidatesForConflict.add(ns);
if (ns.isRoot()) {
rootId = ns.getComponent().getId().getModule();
}
}
}
if (rootId != null && candidatesForConflict.size() > 1) {
// This is a special case for backwards compatibility: it is possible to have
// a cycle where the root component depends on a library which transitively
// depends on a different version of the root module. In this case, we effectively
// allow 2 modules to have the same capability, so we filter the nodes coming
// from transitive dependencies
ModuleIdentifier rootModuleId = rootId;
candidatesForConflict.removeIf(n -> !n.isRoot() && n.getComponent().getId().getModule().equals(rootModuleId));
}
if (candidatesForConflict.size() > 1) {
PotentialConflict conflict = new PotentialConflict() {
@Override
public void withParticipatingModules(Action<ModuleIdentifier> action) {
for (NodeState node : candidatesForConflict) {
action.execute(node.getComponent().getId().getModule());
}
}
@Override
public boolean conflictExists() {
return true;
}
};
conflicts.add(new CapabilityConflict(group, name, candidatesForConflict));
return conflict;
}
}
return PotentialConflictFactory.noConflict();
}
private Set<NodeState> findNodesFor(CapabilityInternal capability) {
String capabilityId = capability.getCapabilityId();
Set<NodeState> nodes = capabilityWithoutVersionToNodes.get(capabilityId);
if (nodes == null) {
nodes = Sets.newLinkedHashSet();
capabilityWithoutVersionToNodes.put(capabilityId, nodes);
}
return nodes;
}
@Override
public boolean hasConflicts() {
return !conflicts.isEmpty();
}
@Override
public void resolveNextConflict(Action<ConflictResolutionResult> resolutionAction) {
CapabilityConflict conflict = conflicts.poll();
Details details = new Details(conflict);
for (Resolver resolver : resolvers) {
resolver.resolve(details);
if (details.hasResult()) {
resolutionAction.execute(details);
ComponentSelectionDescriptorInternal conflictResolution = ComponentSelectionReasons.CONFLICT_RESOLUTION;
if (details.reason != null) {
conflictResolution = conflictResolution.withDescription(details.reason);
}
details.getSelected().addCause(conflictResolution);
return;
}
}
}
@Override
public void registerResolver(Resolver conflictResolver) {
resolvers.add(conflictResolver);
}
@Override
public boolean hasSeenCapability(Capability capability) {
return capabilityWithoutVersionToNodes.containsKey(((CapabilityInternal) capability).getCapabilityId());
}
public static CapabilitiesConflictHandler.Candidate candidate(NodeState node, Capability capability, Collection<NodeState> implicitCapabilityProviders) {
return new Candidate(node, capability, implicitCapabilityProviders);
}
private static class Candidate implements CapabilitiesConflictHandler.Candidate {
private final NodeState node;
private final Capability capability;
private final Collection<NodeState> implicitCapabilityProviders;
public Candidate(NodeState node, Capability capability, Collection<NodeState> implicitCapabilityProviders) {
this.node = node;
this.capability = capability;
this.implicitCapabilityProviders = implicitCapabilityProviders;
}
@Override
public NodeState getNode() {
return node;
}
@Override
public Capability getCapability() {
return capability;
}
@Override
public Collection<NodeState> getImplicitCapabilityProviders() {
return implicitCapabilityProviders;
}
}
private static class Details implements ResolutionDetails {
private final CapabilityConflict conflict;
private final Set<NodeState> evicted = Sets.newHashSet();
private NodeState selected;
private Describable reason;
private Details(CapabilityConflict conflict) {
this.conflict = conflict;
}
@Override
public Collection<? extends Capability> getCapabilityVersions() {
return conflict.descriptors;
}
@Override
public Collection<? extends CandidateDetails> getCandidates(Capability capability) {
ImmutableList.Builder<CandidateDetails> candidates = new ImmutableList.Builder<CandidateDetails>();
String group = capability.getGroup();
String name = capability.getName();
String version = capability.getVersion();
for (final NodeState node : conflict.nodes) {
if (!evicted.contains(node)) {
Capability componentCapability = node.findCapability(group, name);
if (componentCapability != null && componentCapability.getVersion().equals(version)) {
candidates.add(new CandidateDetails() {
@Override
public ComponentIdentifier getId() {
return node.getComponent().getComponentId();
}
@Override
public String getVariantName() {
return node.getResolvedConfigurationId().getConfiguration();
}
@Override
public void evict() {
node.evict();
evicted.add(node);
}
@Override
public void select() {
selected = node;
}
@Override
public void reject() {
ComponentState component = node.getComponent();
component.rejectForCapabilityConflict(capability, conflictedNodes(node, conflict.nodes));
component.selectAndRestartModule();
}
@Override
public void byReason(Describable description) {
reason = description;
}
});
}
}
}
return candidates.build();
}
private Collection<NodeState> conflictedNodes(NodeState node, Collection<NodeState> nodes) {
List<NodeState> conflictedNodes = Lists.newArrayList(nodes);
conflictedNodes.remove(node);
return conflictedNodes;
}
@Override
public void withParticipatingModules(Action<? super ModuleIdentifier> action) {
Set<ModuleIdentifier> seen = Sets.newHashSet();
for (NodeState node : conflict.nodes) {
ModuleIdentifier module = node.getComponent().getId().getModule();
if (seen.add(module)) {
action.execute(module);
}
}
}
@Override
public boolean hasResult() {
return selected != null;
}
@Override
public ComponentState getSelected() {
return selected.getComponent();
}
}
private static class CapabilityConflict {
private final Collection<NodeState> nodes;
private final Set<Capability> descriptors;
private CapabilityConflict(String group, String name, Collection<NodeState> nodes) {
this.nodes = nodes;
final ImmutableSet.Builder<Capability> builder = new ImmutableSet.Builder<Capability>();
for (final NodeState node : nodes) {
Capability capability = node.findCapability(group, name);
if (capability != null) {
builder.add(capability);
}
}
this.descriptors = builder.build();
}
}
private static String prettifyCapabilities(CapabilityConflict conflict) {
TreeSet<String> capabilities = Sets.newTreeSet();
for (Capability c : conflict.descriptors) {
capabilities.add(c.getGroup() + ":" + c.getName() + ":" + c.getVersion());
}
return Joiner.on(", ").join(capabilities);
}
private static String prettifyCandidates(CapabilityConflict conflict) {
TreeSet<String> candidates = Sets.newTreeSet();
boolean showVariant = sameComponentAppearsMultipleTimes(conflict);
for (NodeState node : conflict.nodes) {
candidates.add(showVariant ? node.getNameWithVariant() : node.getSimpleName());
}
return Joiner.on(" and ").join(candidates);
}
private static boolean sameComponentAppearsMultipleTimes(CapabilityConflict conflict) {
Set<ComponentState> components = Sets.newHashSet();
for (NodeState node : conflict.nodes) {
if (!components.add(node.getComponent())) {
return true;
}
}
return false;
}
}