@@ -184,8 +184,7 @@ public final class Manifest: Sendable {
184
184
/// Returns a list of dependencies that are being guarded by unenabled traits, given a set of enabled traits.
185
185
///
186
186
/// If a trait that is guarding a dependency is enabled (and is reflected in the `enabledTraits` parameter) and
187
- /// results in
188
- /// that dependency being used, then that dependency is not considered trait-guarded.
187
+ /// results in that dependency being used, then that dependency is not considered trait-guarded.
189
188
///
190
189
/// For example:
191
190
///
@@ -203,38 +202,46 @@ public final class Manifest: Sendable {
203
202
return [ ]
204
203
}
205
204
206
- let traitGuardedDeps = self . traitGuardedDependencies ( lowercasedKeys: true )
205
+ let traitGuardedDeps = self . traitGuardedTargetDependencies ( lowercasedKeys: true )
207
206
let explicitlyEnabledTraits = try ? self . enabledTraits ( using: enabledTraits, enableAllTraits: enableAllTraits)
208
207
guard self . toolsVersion >= . v5_2 && !self . packageKind. isRoot else {
209
208
let deps = self . dependencies. filter {
210
- if let guardTraits = traitGuardedDeps [ $0. identity. description] ? . values. flatMap ( { $0 } ) ,
211
- !guardTraits. isEmpty, let explicitlyEnabledTraits
212
- {
213
- return !guardTraits. allSatisfy { explicitlyEnabledTraits. contains ( $0) }
209
+ var result = false
210
+ for guardedTargetDeps in traitGuardedDeps [ $0. identity. description] ?? [ ] {
211
+ if let guardTraits = guardedTargetDeps. condition? . traits, !guardTraits. isEmpty,
212
+ let explicitlyEnabledTraits
213
+ {
214
+ result = result || !guardTraits. allSatisfy { explicitlyEnabledTraits. contains ( $0) }
215
+ }
214
216
}
215
217
216
- return false
218
+ return result
217
219
}
218
220
return deps
219
221
}
220
222
221
223
if let dependencies = self . _requiredDependencies [ . nothing] {
222
224
let deps = dependencies. filter {
223
- if let guardTraits = traitGuardedDeps [ $0. identity. description] ? . values. flatMap ( { $0 } ) ,
224
- let explicitlyEnabledTraits
225
- {
226
- return !guardTraits. allSatisfy { explicitlyEnabledTraits. contains ( $0) }
225
+ var result = false
226
+ for guardedTargetDeps in traitGuardedDeps [ $0. identity. description] ?? [ ] {
227
+ if let guardTraits = guardedTargetDeps. condition? . traits, !guardTraits. isEmpty,
228
+ let explicitlyEnabledTraits
229
+ {
230
+ result = result || !guardTraits. allSatisfy { explicitlyEnabledTraits. contains ( $0) }
231
+ }
227
232
}
228
233
229
- return false
234
+ return result
230
235
}
231
236
return deps
232
237
} else {
233
238
var guardedDependencies : Set < PackageIdentity > = [ ]
234
239
for target in self . targetsRequired ( for: self . products) {
240
+ let traitGuardedTargetDeps = traitGuardedTargetDependencies ( for: target)
241
+
235
242
for targetDependency in target. dependencies {
236
243
guard let dependency = self . packageDependency ( referencedBy: targetDependency) ,
237
- let guardingTraits = traitGuardedDeps [ dependency . identity . description ] ? [ target . name ]
244
+ let guardingTraits = traitGuardedTargetDeps [ targetDependency ]
238
245
else {
239
246
continue
240
247
}
@@ -245,19 +252,6 @@ public final class Manifest: Sendable {
245
252
guardedDependencies. insert ( dependency. identity)
246
253
}
247
254
}
248
-
249
- target. pluginUsages? . forEach {
250
- guard let dependency = self . packageDependency ( referencedBy: $0) ,
251
- let guardingTraits = traitGuardedDeps [ dependency. identity. description] ? [ target. name]
252
- else {
253
- return
254
- }
255
- if let explicitlyEnabledTraits,
256
- guardingTraits. intersection ( explicitlyEnabledTraits) != guardingTraits
257
- {
258
- guardedDependencies. insert ( dependency. identity)
259
- }
260
- }
261
255
}
262
256
263
257
let dependencies = self . dependencies. filter { guardedDependencies. contains ( $0. identity) }
@@ -301,8 +295,6 @@ public final class Manifest: Sendable {
301
295
return dependencies
302
296
}
303
297
304
- // calculate explicitly enabled traits through config:
305
-
306
298
// using .nothing as cache key while ENABLE_TARGET_BASED_DEPENDENCY_RESOLUTION is false
307
299
if var dependencies = self . _requiredDependencies [ . nothing] {
308
300
if self . pruneDependencies {
@@ -877,20 +869,45 @@ extension Manifest {
877
869
return ( knownPackage: known, unknownPackage: unknown)
878
870
}
879
871
880
- /// Returns the set of package dependencies that are potentially guarded by traits, per target. This does not
881
- /// calculate enabled and disabled dependencies enforced by traits.
882
- public func traitGuardedDependencies( lowercasedKeys: Bool = false ) -> [ String : [ String : Set < String > ] ] {
883
- self . targets. reduce ( into: [ String : [ String : Set < String > ] ] ( ) ) { depMap, target in
884
- let traitGuardedTargetDependencies = target. dependencies. filter {
885
- !( $0. condition? . traits? . isEmpty ?? true )
886
- }
872
+ /// Computes the list of target dependencies per target that are guarded by traits.
873
+ /// A target dependency is considered potentially trait-guarded if it defines a condition wherein there exists a
874
+ /// list of traits.
875
+ /// - Parameters:
876
+ /// - lowercasedKeys: A flag that determines whether the keys in the resulting dictionary are lowercased.
877
+ /// - Returns: A dictionary that maps the name of a `TargetDescription` to a list of its dependencies that are
878
+ /// guarded by traits.
879
+ public func traitGuardedTargetDependencies(
880
+ lowercasedKeys: Bool = false
881
+ ) -> [ String : [ TargetDescription . Dependency ] ] {
882
+ self . targets. reduce ( into: [ String: [ TargetDescription . Dependency] ] ( ) ) { depMap, target in
883
+ let traitGuardedTargetDependencies = traitGuardedTargetDependencies (
884
+ for: target
885
+ )
886
+
887
887
traitGuardedTargetDependencies. forEach {
888
- guard let package = lowercasedKeys ? $0. package ? . lowercased ( ) : $0. package else { return }
889
- depMap [ package , default: [ : ] ] [ target . name , default : [ ] ] . formUnion ( $0. condition ? . traits ?? [ ] )
888
+ guard let package = lowercasedKeys ? $0. key . package ? . lowercased ( ) : $0. key . package else { return }
889
+ depMap [ package , default: [ ] ] . append ( $0. key )
890
890
}
891
891
}
892
892
}
893
893
894
+ /// Computes the list of target dependencies that are guarded by traits for given target.
895
+ /// A target dependency is considered potentially trait-guarded if it defines a condition wherein there exists a
896
+ /// list of traits.
897
+ /// - Parameters:
898
+ /// - target: A `TargetDescription` for which the trait-guarded target dependencies are calculated.
899
+ /// - Returns: A dictionary that maps each trait-guarded `TargetDescription.Dependency` of the given
900
+ /// `TargetDescription` to the list of traits that guard it.
901
+ public func traitGuardedTargetDependencies( for target: TargetDescription )
902
+ -> [ TargetDescription . Dependency : Set < String > ]
903
+ {
904
+ target. dependencies. filter {
905
+ !( $0. condition? . traits? . isEmpty ?? true )
906
+ } . reduce ( into: [ TargetDescription . Dependency : Set < String > ] ( ) ) { depMap, dep in
907
+ depMap [ dep, default: [ ] ] . formUnion ( dep. condition? . traits ?? [ ] )
908
+ }
909
+ }
910
+
894
911
/// Computes the enabled traits for a given target dependency
895
912
public func enabledTraits( forDependency dependency: TargetDescription . Dependency ) -> Set < String > ? {
896
913
guard let package = self . packageDependency ( referencedBy: dependency) ,
@@ -910,21 +927,22 @@ extension Manifest {
910
927
enableAllTraits: Bool = false
911
928
) throws -> Bool {
912
929
guard self . supportsTraits, !enableAllTraits else { return true }
913
- guard let package = dependency . package , let target = self . targetMap [ target] else { return false }
930
+ guard let target = self . targetMap [ target] else { return false }
914
931
guard target. dependencies. contains ( where: { $0 == dependency } ) else {
915
932
throw InternalError (
916
933
" target dependency \( dependency. name) not found for target \( target. name) in package \( self . displayName) "
917
934
)
918
935
}
919
- let traitsThatEnableDependency = self . traitGuardedDependencies ( ) [ package ] ? [ target. name] ?? [ ]
920
936
921
- let isEnabled = try traitsThatEnableDependency. allSatisfy { try self . isTraitEnabled (
937
+ let traitsToEnable = self . traitGuardedTargetDependencies ( for: target) [ dependency] ?? [ ]
938
+
939
+ let isEnabled = try traitsToEnable. allSatisfy { try self . isTraitEnabled (
922
940
. init( stringLiteral: $0) ,
923
941
enabledTraits,
924
942
enableAllTraits
925
943
) }
926
944
927
- return traitsThatEnableDependency . isEmpty || isEnabled
945
+ return traitsToEnable . isEmpty || isEnabled
928
946
}
929
947
930
948
/// Determines whether a given package dependency is used by this manifest given a set of enabled traits.
0 commit comments