-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix for .when(traits:) condition not working for multiple traits #9015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,7 +95,7 @@ extension Manifest { | |
_ parentPackage: PackageIdentifier? = nil | ||
) throws { | ||
guard supportsTraits else { | ||
if explicitlyEnabledTraits != ["default"] /*!explicitlyEnabledTraits.contains("default")*/ { | ||
if explicitlyEnabledTraits != ["default"] { | ||
throw TraitError.traitsNotSupported( | ||
parent: parentPackage, | ||
package: .init(self), | ||
|
@@ -116,7 +116,7 @@ extension Manifest { | |
let areDefaultsEnabled = enabledTraits.contains("default") | ||
|
||
// Ensure that disabling default traits is disallowed for packages that don't define any traits. | ||
if !(explicitlyEnabledTraits == nil || areDefaultsEnabled) && !self.supportsTraits { | ||
if !areDefaultsEnabled && !self.supportsTraits { | ||
// We throw an error when default traits are disabled for a package without any traits | ||
// This allows packages to initially move new API behind traits once. | ||
throw TraitError.traitsNotSupported( | ||
|
@@ -449,15 +449,18 @@ extension Manifest { | |
|
||
let traitsToEnable = self.traitGuardedTargetDependencies(for: target)[dependency] ?? [] | ||
|
||
let isEnabled = try traitsToEnable.allSatisfy { try self.isTraitEnabled( | ||
// Check if any of the traits guarding this dependency is enabled; | ||
// if so, the condition is met and the target dependency is considered | ||
// to be in an enabled state. | ||
let isEnabled = try traitsToEnable.contains(where: { try self.isTraitEnabled( | ||
.init(stringLiteral: $0), | ||
enabledTraits, | ||
) } | ||
) }) | ||
|
||
return traitsToEnable.isEmpty || isEnabled | ||
} | ||
/// Determines whether a given package dependency is used by this manifest given a set of enabled traits. | ||
public func isPackageDependencyUsed(_ dependency: PackageDependency, enabledTraits: Set<String>/* = ["default"]*/) throws -> Bool { | ||
public func isPackageDependencyUsed(_ dependency: PackageDependency, enabledTraits: Set<String>) throws -> Bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: can we write some automated tests that would validate this function in isolation. This would allow us to validate various scenarios, include fault injection et al. If we are unable to control the test the was we want, we can rework the function to make it testable. |
||
if self.pruneDependencies { | ||
let usedDependencies = try self.usedDependencies(withTraits: enabledTraits) | ||
let foundKnownPackage = usedDependencies.knownPackage.contains(where: { | ||
|
@@ -478,8 +481,8 @@ extension Manifest { | |
|
||
// if target deps is empty, default to returning true here. | ||
let isTraitGuarded = targetDependenciesForPackageDependency.isEmpty ? false : targetDependenciesForPackageDependency.compactMap({ $0.condition?.traits }).allSatisfy({ | ||
let condition = $0.subtracting(enabledTraits) | ||
return !condition.isEmpty | ||
let isGuarded = $0.intersection(enabledTraits).isEmpty | ||
return isGuarded | ||
}) | ||
|
||
let isUsedWithoutTraitGuarding = !targetDependenciesForPackageDependency.filter({ $0.condition?.traits == nil }).isEmpty | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: can we write some automated tests that would validate this function in isolation. This would allow us to validate various scenarios, include fault injection et al. If we are unable to control the test the was we want, we can rework the function to make it testable.