The wrong version of Swift is used when dumping packages
## Summary
Internally, `rules_swift_package_manager` uses `xcrun swift package` (or equivalent) to inspect Swift packages. This uses the Xcode version selected with `xcode-select`. Bazel normally doesn't work like this (or at least not always). The Xcode version can be selected with `--xcode_version_config` and be different from the one reported by `xcode-select -p`.
The result is that the Bazel build files generated from the Swift package dump can use the wrong package file (when multiple are provided for different versions of the tool) and be incompatible with the Xcode version that Bazel then uses. When these targets are built, it results in build failures.
Overall, Swift package authors expect that the Swift version used to resolve the package and the one used to compile the code is the same. When this is not guarantied, build failures can happen.
Take for example the [swift-collections](https://github.com/apple/swift-collections) package. If Xcode 16 is selected with `xcode-select` and used to construct the build files, but the project is then compiled with Xcode 26, compilation fails. The reason is that [LifetimeOverride.swift](https://github.com/apple/swift-collections/blob/1.4.1/Sources/InternalCollectionsUtilities/LifetimeOverride.swift) has a `#if compiler(>=6.2)`, and the code inside this check expects a number of Swift experimental features to be enabled (in this case Lifetimes), but if the package was resolved with an older version of the tools, then the feature is not enabled and compilation fails.
4 条评论