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
use std::collections::BTreeSet;
use std::collections::VecDeque;
use std::collections::hash_map::Entry;
use std::path::Path;
use std::sync::Arc;
use either::Either;
use itertools::Itertools;
use petgraph::Graph;
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use uv_configuration::ExtrasSpecificationWithDefaults;
use uv_configuration::{BuildOptions, DependencyGroupsWithDefaults, InstallOptions};
use uv_distribution_types::{Edge, Node, Resolution, ResolvedDist};
use uv_normalize::{ExtraName, GroupName, PackageName};
use uv_platform_tags::Tags;
use uv_pypi_types::ResolverMarkerEnvironment;
use crate::lock::{Dependency, HashedDist, LockErrorKind, Package, TagPolicy};
use crate::{Lock, LockError};
fn newly_activated_extras<'lock>(
dep: &'lock Dependency,
activated_extras: &[(&'lock PackageName, &'lock ExtraName)],
) -> Vec<(&'lock PackageName, &'lock ExtraName)> {
dep.extra
.iter()
.filter_map(|extra| {
let key = (&dep.package_id.name, extra);
(!activated_extras.contains(&key)).then_some(key)
})
.collect()
}
pub trait Installable<'lock> {
/// Return the root install path.
fn install_path(&self) -> &'lock Path;
/// Return the [`Lock`] to install.
fn lock(&self) -> &'lock Lock;
/// Return the [`PackageName`] of the root packages in the target.
fn roots(&self) -> impl Iterator<Item = &PackageName>;
/// Return the [`PackageName`] of the target, if available.
fn project_name(&self) -> Option<&PackageName>;
/// Convert the [`Lock`] to a [`Resolution`] using the given marker environment, tags, and root.
fn to_resolution(
&self,
marker_env: &ResolverMarkerEnvironment,
tags: &Tags,
extras: &ExtrasSpecificationWithDefaults,
groups: &DependencyGroupsWithDefaults,
build_options: &BuildOptions,
install_options: &InstallOptions,
) -> Result<Resolution, LockError> {
let size_guess = self.lock().packages.len();
let mut petgraph = Graph::with_capacity(size_guess, size_guess);
let mut inverse = FxHashMap::with_capacity_and_hasher(size_guess, FxBuildHasher);
let mut queue: VecDeque<(&Package, Option<&ExtraName>)> = VecDeque::new();
let mut seen = FxHashSet::default();
let mut activated_projects: Vec<&PackageName> = vec![];
let mut activated_extras: Vec<(&PackageName, &ExtraName)> = vec![];
let mut activated_groups: Vec<(&PackageName, &GroupName)> = vec![];
let root = petgraph.add_node(Node::Root);
// Determine the set of activated extras and groups, from the root.
//
// TODO(charlie): This isn't quite right. Below, when we add the dependency groups to the
// graph, we rely on the activated extras and dependency groups, to evaluate the conflict
// marker. But at that point, we don't know the full set of activated extras; this is only
// computed below. We somehow need to add the dependency groups _after_ we've computed all
// enabled extras, but the groups themselves could depend on the set of enabled extras.
if !self.lock().conflicts().is_empty() {
for root_name in self.roots() {
let dist = self
.lock()
.find_by_name(root_name)
.map_err(|_| LockErrorKind::MultipleRootPackages {
name: root_name.clone(),
})?
.ok_or_else(|| LockErrorKind::MissingRootPackage {
name: root_name.clone(),
})?;
// Track the activated extras.
if groups.prod() {
activated_projects.push(&dist.id.name);
for extra in extras.extra_names(dist.optional_dependencies.keys()) {
activated_extras.push((&dist.id.name, extra));
}
}
// Track the activated groups.
for group in dist
.dependency_groups
.keys()
.filter(|group| groups.contains(group))
{
activated_groups.push((&dist.id.name, group));
}
}
}
// Initialize the workspace roots.
let mut roots = vec![];
for root_name in self.roots() {
let dist = self
.lock()
.find_by_name(root_name)
.map_err(|_| LockErrorKind::MultipleRootPackages {
name: root_name.clone(),
})?
.ok_or_else(|| LockErrorKind::MissingRootPackage {
name: root_name.clone(),
})?;
// Add the workspace package to the graph.
let index = petgraph.add_node(if groups.prod() {
self.package_to_node(dist, tags, build_options, install_options, marker_env)?
} else {
self.non_installable_node(dist, tags, marker_env)?
});
inverse.insert(&dist.id, index);
// Add an edge from the root.
petgraph.add_edge(root, index, Edge::Prod);
// Push the package onto the queue.
roots.push((dist, index));
}
// Add the workspace dependencies to the queue.
for (dist, index) in roots {
if groups.prod() {
// Push its dependencies onto the queue.
queue.push_back((dist, None));
for extra in extras.extra_names(dist.optional_dependencies.keys()) {
queue.push_back((dist, Some(extra)));
}
}
// Add any dev dependencies.
for (group, dep) in dist
.dependency_groups
.iter()
.filter_map(|(group, deps)| {
if groups.contains(group) {
Some(deps.iter().map(move |dep| (group, dep)))
} else {
None
}
})
.flatten()
{
let additional_activated_extras = newly_activated_extras(dep, &activated_extras);
if !dep.complexified_marker.evaluate(
marker_env,
activated_projects.iter().copied(),
activated_extras
.iter()
.chain(additional_activated_extras.iter())
.copied(),
activated_groups.iter().copied(),
) {
continue;
}
let dep_dist = self.lock().find_by_id(&dep.package_id);
// Add the package to the graph.
let dep_index = match inverse.entry(&dep.package_id) {
Entry::Vacant(entry) => {
let index = petgraph.add_node(self.package_to_node(
dep_dist,
tags,
build_options,
install_options,
marker_env,
)?);
entry.insert(index);
index
}
Entry::Occupied(entry) => {
// Critically, if the package is already in the graph, then it's a workspace
// member. If it was omitted due to, e.g., `--only-dev`, but is itself
// referenced as a development dependency, then we need to re-enable it.
let index = *entry.get();
let node = &mut petgraph[index];
if !groups.prod() {
*node = self.package_to_node(
dep_dist,
tags,
build_options,
install_options,
marker_env,
)?;
}
index
}
};
petgraph.add_edge(
index,
dep_index,
// This is OK because we are resolving to a resolution for
// a specific marker environment and set of extras/groups.
// So at this point, we know the extras/groups have been
// satisfied, so we can safely drop the conflict marker.
Edge::Dev(group.clone()),
);
// Push its dependencies on the queue.
if seen.insert((&dep.package_id, None)) {
queue.push_back((dep_dist, None));
}
for extra in &dep.extra {
if seen.insert((&dep.package_id, Some(extra))) {
queue.push_back((dep_dist, Some(extra)));
}
}
}
}
// Add any requirements that are exclusive to the workspace root (e.g., dependencies in
// PEP 723 scripts).
for dependency in self.lock().requirements() {
if !dependency.marker.evaluate(marker_env, &[]) {
continue;
}
let root_name = &dependency.name;
let dist = self
.lock()
.find_by_markers(root_name, marker_env)
.map_err(|_| LockErrorKind::MultipleRootPackages {
name: root_name.clone(),
})?
.ok_or_else(|| LockErrorKind::MissingRootPackage {
name: root_name.clone(),
})?;
// Add the package to the graph.
let index = petgraph.add_node(if groups.prod() {
self.package_to_node(dist, tags, build_options, install_options, marker_env)?
} else {
self.non_installable_node(dist, tags, marker_env)?
});
inverse.insert(&dist.id, index);
// Add the edge.
petgraph.add_edge(root, index, Edge::Prod);
// Push its dependencies on the queue.
if seen.insert((&dist.id, None)) {
queue.push_back((dist, None));
}
for extra in &dependency.extras {
if seen.insert((&dist.id, Some(extra))) {
queue.push_back((dist, Some(extra)));
}
}
}
// Add any dependency groups that are exclusive to the workspace root (e.g., dev
// dependencies in non-project workspace roots).
for (group, dependency) in self
.lock()
.dependency_groups()
.iter()
.filter_map(|(group, deps)| {
if groups.contains(group) {
Some(deps.iter().map(move |dep| (group, dep)))
} else {
None
}
})
.flatten()
{
if !dependency.marker.evaluate(marker_env, &[]) {
continue;
}
let root_name = &dependency.name;
let dist = self
.lock()
.find_by_markers(root_name, marker_env)
.map_err(|_| LockErrorKind::MultipleRootPackages {
name: root_name.clone(),
})?
.ok_or_else(|| LockErrorKind::MissingRootPackage {
name: root_name.clone(),
})?;
// Add the package to the graph.
let index = match inverse.entry(&dist.id) {
Entry::Vacant(entry) => {
let index = petgraph.add_node(self.package_to_node(
dist,
tags,
build_options,
install_options,
marker_env,
)?);
entry.insert(index);
index
}
Entry::Occupied(entry) => {
// Critically, if the package is already in the graph, then it's a workspace
// member. If it was omitted due to, e.g., `--only-dev`, but is itself
// referenced as a development dependency, then we need to re-enable it.
let index = *entry.get();
let node = &mut petgraph[index];
if !groups.prod() {
*node = self.package_to_node(
dist,
tags,
build_options,
install_options,
marker_env,
)?;
}
index
}
};
// Add the edge.
petgraph.add_edge(root, index, Edge::Dev(group.clone()));
// Push its dependencies on the queue.
if seen.insert((&dist.id, None)) {
queue.push_back((dist, None));
}
for extra in &dependency.extras {
if seen.insert((&dist.id, Some(extra))) {
queue.push_back((dist, Some(extra)));
}
}
}
// Below, we traverse the dependency graph in a breadth first manner
// twice. It's only in the second traversal that we actually build
// up our resolution graph. In the first traversal, we accumulate all
// activated extras. This includes the extras explicitly enabled on
// the CLI (which were gathered above) and the extras enabled via
// dependency specifications like `foo[extra]`. We need to do this
// to correctly support conflicting extras.
//
// In particular, the way conflicting extras works is by forking the
// resolver based on the extras that are declared as conflicting. But
// this forking needs to be made manifest somehow in the lock file to
// avoid multiple versions of the same package being installed into the
// environment. This is why "conflict markers" were invented. For
// example, you might have both `torch` and `torch+cpu` in your
// dependency graph, where the latter is only enabled when the `cpu`
// extra is enabled, and the former is specifically *not* enabled
// when the `cpu` extra is enabled.
//
// In order to evaluate these conflict markers correctly, we need to
// know whether the `cpu` extra is enabled when we visit the `torch`
// dependency. If we think it's disabled, then we'll erroneously
// include it if the extra is actually enabled. But in order to tell
// if it's enabled, we need to traverse the entire dependency graph
// first to inspect which extras are enabled!
//
// Of course, we don't need to do this at all if there aren't any
// conflicts. In which case, we skip all of this and just do the one
// traversal below.
if !self.lock().conflicts().is_empty() {
let mut activated_extras_set: BTreeSet<(&PackageName, &ExtraName)> =
activated_extras.iter().copied().collect();
let mut queue = queue.clone();
let mut seen = seen.clone();
while let Some((package, extra)) = queue.pop_front() {
let deps = if let Some(extra) = extra {
Either::Left(
package
.optional_dependencies
.get(extra)
.into_iter()
.flatten(),
)
} else {
Either::Right(package.dependencies.iter())
};
for dep in deps {
let additional_activated_extras =
newly_activated_extras(dep, &activated_extras);
if !dep.complexified_marker.evaluate(
marker_env,
activated_projects.iter().copied(),
activated_extras
.iter()
.chain(additional_activated_extras.iter())
.copied(),
activated_groups.iter().copied(),
) {
continue;
}
// It is, I believe, possible to be here for a dependency that
// will ultimately not be included in the final resolution.
// Specifically, carrying on from the example in the comments
// above, we might visit `torch` first and thus not know if
// the `cpu` feature is enabled or not, and thus, the marker
// evaluation above will pass.
//
// So is this a problem? Well, this is the main reason why we
// do two graph traversals. On the second traversal below, we
// will have seen all of the enabled extras, and so `torch`
// will be excluded.
//
// But could this lead to a bigger list of activated extras
// than we actually have? I believe that is indeed possible,
// but I think it is only a problem if it leads to extras that
// *conflict* with one another being simultaneously enabled.
// However, after this first traversal, we check our set of
// accumulated extras to ensure that there are no conflicts. If
// there are, we raise an error. ---AG
for key in additional_activated_extras {
activated_extras_set.insert(key);
activated_extras.push(key);
}
let dep_dist = self.lock().find_by_id(&dep.package_id);
// Push its dependencies on the queue.
if seen.insert((&dep.package_id, None)) {
queue.push_back((dep_dist, None));
}
for extra in &dep.extra {
if seen.insert((&dep.package_id, Some(extra))) {
queue.push_back((dep_dist, Some(extra)));
}
}
}
}
// At time of writing, it's somewhat expected that the set of
// conflicting extras is pretty small. With that said, the
// time complexity of the following routine is pretty gross.
// Namely, `set.contains` is linear in the size of the set,
// iteration over all conflicts is also obviously linear in
// the number of conflicting sets and then for each of those,
// we visit every possible pair of activated extra from above,
// which is quadratic in the total number of extras enabled. I
// believe the simplest improvement here, if it's necessary, is
// to adjust the `Conflicts` internals to own these sorts of
// checks. ---AG
for set in self.lock().conflicts().iter() {
for ((pkg1, extra1), (pkg2, extra2)) in
activated_extras_set.iter().tuple_combinations()
{
if set.contains(pkg1, *extra1) && set.contains(pkg2, *extra2) {
return Err(LockErrorKind::ConflictingExtra {
package1: (*pkg1).clone(),
extra1: (*extra1).clone(),
package2: (*pkg2).clone(),
extra2: (*extra2).clone(),
}
.into());
}
}
}
}
while let Some((package, extra)) = queue.pop_front() {
let deps = if let Some(extra) = extra {
Either::Left(
package
.optional_dependencies
.get(extra)
.into_iter()
.flatten(),
)
} else {
Either::Right(package.dependencies.iter())
};
for dep in deps {
if !dep.complexified_marker.evaluate(
marker_env,
activated_projects.iter().copied(),
activated_extras.iter().copied(),
activated_groups.iter().copied(),
) {
continue;
}
let dep_dist = self.lock().find_by_id(&dep.package_id);
// Add the dependency to the graph.
let dep_index = match inverse.entry(&dep.package_id) {
Entry::Vacant(entry) => {
let index = petgraph.add_node(self.package_to_node(
dep_dist,
tags,
build_options,
install_options,
marker_env,
)?);
entry.insert(index);
index
}
Entry::Occupied(entry) => *entry.get(),
};
// Add the edge.
let index = inverse[&package.id];
petgraph.add_edge(
index,
dep_index,
if let Some(extra) = extra {
Edge::Optional(extra.clone())
} else {
Edge::Prod
},
);
// Push its dependencies on the queue.
if seen.insert((&dep.package_id, None)) {
queue.push_back((dep_dist, None));
}
for extra in &dep.extra {
if seen.insert((&dep.package_id, Some(extra))) {
queue.push_back((dep_dist, Some(extra)));
}
}
}
}
Ok(Resolution::new(petgraph))
}
/// Create an installable [`Node`] from a [`Package`].
fn installable_node(
&self,
package: &Package,
tags: &Tags,
marker_env: &ResolverMarkerEnvironment,
build_options: &BuildOptions,
) -> Result<Node, LockError> {
let tag_policy = TagPolicy::Required(tags);
let HashedDist { dist, hashes } =
package.to_dist(self.install_path(), tag_policy, build_options, marker_env)?;
let version = package.version().cloned();
let dist = ResolvedDist::Installable {
dist: Arc::new(dist),
version,
};
Ok(Node::Dist {
dist,
hashes,
install: true,
})
}
/// Create a non-installable [`Node`] from a [`Package`].
fn non_installable_node(
&self,
package: &Package,
tags: &Tags,
marker_env: &ResolverMarkerEnvironment,
) -> Result<Node, LockError> {
let HashedDist { dist, .. } = package.to_dist(
self.install_path(),
TagPolicy::Preferred(tags),
&BuildOptions::default(),
marker_env,
)?;
let version = package.version().cloned();
let dist = ResolvedDist::Installable {
dist: Arc::new(dist),
version,
};
let hashes = package.hashes();
Ok(Node::Dist {
dist,
hashes,
install: false,
})
}
/// Convert a lockfile entry to a graph [`Node`].
fn package_to_node(
&self,
package: &Package,
tags: &Tags,
build_options: &BuildOptions,
install_options: &InstallOptions,
marker_env: &ResolverMarkerEnvironment,
) -> Result<Node, LockError> {
if install_options.include_package(
package.as_install_target(),
self.project_name(),
self.lock().members(),
) {
self.installable_node(package, tags, marker_env, build_options)
} else {
self.non_installable_node(package, tags, marker_env)
}
}
}