Commit 4bb01f6
authored
Introduce richer
This PR introduces the `EnabledTrait` model and associated
infrastructure to properly track and manage trait enablement throughout
the dependency resolution process. The new system replaces simple
string-based trait tracking with a rich model that maintains provenance
information about how and why each trait was enabled.
Fixes #9286
## Core Changes
### New `EnabledTrait` Model (EnabledTrait.swift)
Introduces three main components for tracking enabled traits for
packages:
**EnabledTrait** - Represents an individual enabled trait with:
- Trait name (used as the identifier)
- Set of `Setter` values tracking how the trait was enabled (via
command-line config, parent package, or another trait)
- Support for unifying traits with the same name by merging their setter
lists - it's important to note that this is usually done with an
`EnabledTrait` for a specific package, and that many packages can define
their own (unique) trait that shares a name with a trait from another
package but are treated as fundamentally different instances.
**EnabledTraits** - Collection wrapper around
`IdentifiableSet<EnabledTrait>` that:
- Automatically unifies traits with the same name when inserted
- Provides convenient set operations (union, intersection)
- Maintains enablement metadata while treating traits with the same name
as equal (merges `Setters` for traits of the same name)
**EnabledTraitsMap** - Thread-safe map storing enabled traits per
package that:
- Maps `PackageIdentity` to `EnabledTraits`, while maintaining a
thread-safe storage box of its properties to accommodate for loading
manifests in parallel
- Implicitly returns `["default"]` for packages with no explicitly
enabled traits
- Uses union behaviour: setting traits for the same package multiple
times accumulates all traits
- Tracks whether default traits have been explicitly disabled by a
parent package or trait configuration (set with `[]`).
- Tracks parent packages that request `default` trait usage for a
package dependency (whether it's been requested through explicitly
listing `default` as a trait, or if a trait specification is omitted)
The introduction of these models have also provided some API
conveniences:
**String Interoperability** - `EnabledTrait` and `EnabledTraits` provide
seamless string integration:
- `ExpressibleByStringLiteral` and `ExpressibleByArrayLiteral` for
natural initialization of enabled traits and sets of enabled traits:
`let enabledTrait: EnabledTrait = "foo"` or `let enabledTraits:
EnabledTraits = ["foo", "bar"]`
- Bidirectional string equality: `enabledTrait == "foo"` and `"foo" ==
enabledTrait` both work
- `EnabledTraitConvertible` protocol allows APIs to accept string
collections and auto-convert them to `EnabledTrait`
**Collection Protocol Support** - `EnabledTraits` conforms to
`Collection` with set-like operations (union, intersection, contains)
and overloaded methods that accept either `Collection<String>` or
`Collection<EnabledTrait>`, enabling flexible APIs that work with both
types
## Refactored Trait Handling (`Manifest+Traits.swift`)
- Replaced all `Set<String>` trait representations with richer
`EnabledTraits` model
- Updated validation methods to work with `EnabledTrait` instead of raw
strings
- Removed parentPackage parameters from validation (now tracked via
`EnabledTrait.Setter`)
- Improved type safety and metadata tracking throughout trait
calculation
### `Workspace+Traits.swift` (new file)
- Extracted trait-specific workspace operations
- `updateEnabledTraits(for:)` method determines enabled traits for
loaded manifests
- `updateEnabledTraits(forDependency:_:)` handles dependency-specific
trait propagation
## Bug Fixes
### Required Dependencies Calculation
**Problem**: During PubGrub dependency resolution, enabled traits were
being reset to defaults instead of propagating previously calculated
values when creating `DependencyResolutionNode` instances, which relied
on an accurate set of `enabledTraits`.
**Impact**: Wrong trait sets were considered when evaluating required
dependencies, leading to incorrect dependency graphs.
**Solution**: Properly propagate enabled traits through the `nodes()`
method for `PackageContainerConstraint`, which creates instances of
`DependencyResolutionNode` from these constraints during PubGrub
dependency resolution.
### IdentifiableSet Intersection
**Problem**: The intersection method created an empty set instead of
starting with self, causing loss of element data.
**Solution**: Changed `var result = Self()` to `var result = self` in
`IdentifiableSet.swift:86`, ensuring proper preservation of element data
during intersections.
### Test Improvements
- Separated traits tests into dedicated files:
- `ModulesGraphTests+Traits.swift`
- `WorkspaceTests+Traits.swift`
- `EnabledTraitTests.swift`
## Additional Improvements
- Enhanced `IdentifiableSet` with `remove(id:)` and `remove(_:)` methods
- Added `Workspace+Traits.swift` for trait-specific workspace operations
- Updated all package container implementations for proper trait
handlingEnabledTrait model to handle thread-safe enabled traits computations (#9269)1 parent 4e878fa commit 4bb01f6
File tree
37 files changed
+4265
-2109
lines changed- Sources
- Basics/Collections
- PackageGraph
- Resolution
- PubGrub
- PackageLoading
- PackageModel
- Manifest
- Workspace
- PackageContainer
- _InternalTestSupport
- Tests
- PackageGraphTests
- PackageModelTests
- WorkspaceTests
37 files changed
+4265
-2109
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
104 | 112 | | |
105 | 113 | | |
106 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
399 | 399 | | |
400 | 400 | | |
401 | 401 | | |
402 | | - | |
| 402 | + | |
403 | 403 | | |
404 | 404 | | |
405 | 405 | | |
| |||
1438 | 1438 | | |
1439 | 1439 | | |
1440 | 1440 | | |
1441 | | - | |
| 1441 | + | |
1442 | 1442 | | |
1443 | 1443 | | |
1444 | 1444 | | |
| |||
1462 | 1462 | | |
1463 | 1463 | | |
1464 | 1464 | | |
1465 | | - | |
| 1465 | + | |
1466 | 1466 | | |
1467 | 1467 | | |
1468 | 1468 | | |
| |||
1485 | 1485 | | |
1486 | 1486 | | |
1487 | 1487 | | |
1488 | | - | |
| 1488 | + | |
1489 | 1489 | | |
1490 | 1490 | | |
1491 | 1491 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
466 | 466 | | |
467 | 467 | | |
468 | 468 | | |
469 | | - | |
| 469 | + | |
| 470 | + | |
470 | 471 | | |
471 | 472 | | |
472 | 473 | | |
| |||
479 | 480 | | |
480 | 481 | | |
481 | 482 | | |
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 | 483 | | |
547 | 484 | | |
548 | 485 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
| 87 | + | |
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
153 | | - | |
| 153 | + | |
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | | - | |
| 166 | + | |
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
| 148 | + | |
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
165 | | - | |
| 164 | + | |
| 165 | + | |
166 | 166 | | |
167 | 167 | | |
168 | | - | |
| 168 | + | |
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
357 | 357 | | |
358 | 358 | | |
359 | 359 | | |
360 | | - | |
| 360 | + | |
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
| |||
431 | 431 | | |
432 | 432 | | |
433 | 433 | | |
434 | | - | |
| 434 | + | |
435 | 435 | | |
436 | 436 | | |
437 | 437 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
388 | 388 | | |
389 | 389 | | |
390 | 390 | | |
391 | | - | |
| 391 | + | |
392 | 392 | | |
393 | 393 | | |
394 | 394 | | |
| |||
414 | 414 | | |
415 | 415 | | |
416 | 416 | | |
417 | | - | |
| 417 | + | |
418 | 418 | | |
419 | 419 | | |
420 | 420 | | |
| |||
1151 | 1151 | | |
1152 | 1152 | | |
1153 | 1153 | | |
1154 | | - | |
| 1154 | + | |
1155 | 1155 | | |
1156 | 1156 | | |
1157 | 1157 | | |
| |||
0 commit comments