Skip to content

Conversation

bripeticca
Copy link
Contributor

@bripeticca bripeticca commented Sep 16, 2025

  • Explanation:
    Addresses multiple traits issues:
    • Changes the check for trait-guarded target dependencies to determine whether the package dependency from which it derives is used in the parent package; prior to this change, the check was asserting that all traits in the when condition of a target dependency were enabled for the dependency to be considered unguarded, but now the check confirms that at least one trait in the condition is enabled.
    • Fixes some errors in trait computation that hadn't considered when default traits ["default"] were being appended to the EnabledTraitsMap, which should only consider a list of flattened traits + explicitly enabled traits by a user/parent package. The inclusion of default in this dictionary was resulting in an inaccurate computation of which traits were enabled, since pre-computation serves to flatten the list of transitively enabled traits for future reference.
      There were also cases where we were re-computing transitively enabled traits in areas where we had already filled out the enabledTraitsMap, so we now default to simply fetching the entry from the dictionary instead of computing the traits all over again.
  • Scope:
    Package resolution stage; particularly affects pre-computation of transitively enabled traits when determining which target dependencies are/aren't guarded by traits. Also allows for multiple traits to be considered in a target dependency's when condition, any of which being enabled would then include said target dependency.
  • Issues:
  • Original PRs:
    Fix for .when(traits:) condition not working for multiple traits #9015
    Fix pre-computation of traits when loading dependency manifests #9057
  • Risk:
    Low risk; fixes trait-related issues.
  • Testing:
    Fixtures added for end-to-end testing of the issue; regression tests added to multiple suites
  • Reviewers:
    @FranzBusch @jakepetroules @dschaefer2

…ftlang#9015)

The culprit here is the fact that we were essentially doing an AND
boolean check across all the traits in the target dependency condition,
when we should be doing an OR boolean check.

If at least one trait is enabled in the list of traits in the target
dependency condition, then the target dependecy should be considered
enabled as well.
@bripeticca
Copy link
Contributor Author

@swift-ci please test

…tlang#9057)

This fix addresses some issues that arose with trait-based dependency
resolution on Linux and, found through development, a related issue
found involving the `swift package show-dependencies` command. It fixes
an error that was thrown as a discrepancy in state when executing `swift
package show-dependencies` if traits were guarding a dependency and was
omitted from the package graph.

Additionally, this will assure that the `EnabledTraitsMap` guards
against explicitly adding "default" traits to the stored dictionary,
since this wrapper is a mechanism to determine whether traits have been
explicitly set (and therefore have overridden "default" or have
flattened the list of default traits).
@bripeticca
Copy link
Contributor Author

@swift-ci please test

@bripeticca bripeticca changed the title [6.2.1] Cherry-pick: Fix for .when(traits:) condition not working for multiple traits (#9015) [6.2.1] Cherry-pick: Fix traits .when(traits:) condition; pre-computation when loading dependency manifests Sep 16, 2025
@bripeticca
Copy link
Contributor Author

@swift-ci please test windows

@bripeticca bripeticca marked this pull request as ready for review September 18, 2025 17:44
@bripeticca bripeticca requested a review from a team as a code owner September 18, 2025 17:44
@bripeticca bripeticca merged commit 0b6969e into swiftlang:release/6.2.1 Sep 19, 2025
6 checks passed
simonjbeaumont added a commit to swift-otel/swift-otel that referenced this pull request Sep 24, 2025
#357)

## Motivation

We had a workaround in our package manifest for the use of
`.when(traits:)` not using its argument as a boolean-OR. The good news
is that this bug has been addressed and the fix available in the 6.2.0
release. The bad news is that there was still an issue with resolving
dependencies that themselves had trait-conditional transitive
dependencies, and the fix for that did _not_ make the Xcode 26.0
release.

The outcome is that if you were to try to compile a project that depends
on Swift OTel with _only_ the `OTLPHTTP` trait, it will fail to resolve.
We have such an example in our `Examples/` directory:

```console
% rm -rf .build Package.resolved && swift build
...
error: 'swift-otel': product 'SwiftProtobuf' required by package 'swift-otel' target 'OTel' not found in package 'swift-protobuf'.
```

The fix is included in the nightly main, and I have verified that works.
It also has been cherry-picked into 6.2.1 here:
swiftlang/swift-package-manager#9141. So it is
just 6.2.0 that's the problem.

Fortunately, all our trait-conditional dependencies are only used with
`internal import`, which means we can add a workaround without having to
make a future API-breaking change.

We can just unconditionally include these dependencies on the known
problematic toolchain (6.2.0).

## Modifications

- Add 6.2.0-specific workaround for trait-conditional dependencies

## Result

Packages that depend on Swift OTel with only `OTLPHTTP` trait will now
compile with Xcode 26.0 toolchains.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants