Skip to content

Commit adf64d4

Browse files
authored
Fix registry resolution of major alternate package manifests (#8188)
### Motivation: Let's take the following `Package.swift`: ```swift // swift-tools-version: 5.7 import PackageDescription let package = Package( name: "Library", dependencies: [ .package(id: "AliSoftware.OHHTTPStubs", exact: "9.0.0") ] ) ``` You can find the `OHHTTPStubs` dependency at that version [here](https://github.com/AliSoftware/OHHTTPStubs/tree/e92b5a5746ef16add2a1424f1fc19529d9a75cde). The dependency has two manifests: - `Package.swift` with `swift-tools-version` at `4.0` - `[email protected]` with `swift-tools-version` at `5.0` When resolving packages via the registry, SwiftPM should use the `[email protected]`. However, instead SwiftPM fails with: ``` Running resolver because the following dependencies were added: 'AliSoftware.OHHTTPStubs' (AliSoftware.OHHTTPStubs) Computing version for AliSoftware.OHHTTPStubs info: 'AliSoftware.OHHTTPStubs': retrieving AliSoftware.OHHTTPStubs metadata from http://localhost:8080/api/accounts/tuist/registry/swift/AliSoftware/OHHTTPStubs info: 'AliSoftware.OHHTTPStubs': retrieving AliSoftware.OHHTTPStubs 9.0.0 metadata from http://localhost:8080/api/accounts/tuist/registry/swift/AliSoftware/OHHTTPStubs/9.0.0 info: 'AliSoftware.OHHTTPStubs': retrieving available manifests for AliSoftware.OHHTTPStubs 9.0.0 from http://localhost:8080/api/accounts/tuist/registry/swift/AliSoftware/OHHTTPStubs/9.0.0/Package.swift info: 'AliSoftware.OHHTTPStubs': retrieving AliSoftware.OHHTTPStubs 9.0.0 metadata from http://localhost:8080/api/accounts/tuist/registry/swift/AliSoftware/OHHTTPStubs/9.0.0 info: 'AliSoftware.OHHTTPStubs': retrieving AliSoftware.OHHTTPStubs 9.0.0 manifest from http://localhost:8080/api/accounts/tuist/registry/swift/AliSoftware/OHHTTPStubs/9.0.0/Package.swift?swift-version=5.0.0 error: failed locating placeholder manifest for 5.0.0 ``` ### Modifications: The reason for the error is because when looking up the alternate package manifests from the downloaded source archive, SwiftPM doesn't try to find `[email protected]` – only `[email protected]` and `[email protected]`. When both minor and patch versions of the swift tools version are 0, SwiftPM should try to use the `[email protected]` alternate manifest. ### Result: `swift package resolve` will work for packages in a registry that have alternate manifests in the format of `[email protected]`.
1 parent 307316d commit adf64d4

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Sources/Workspace/PackageContainer/RegistryPackageContainer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ public class RegistryPackageContainer: PackageContainer {
200200
return true
201201
} else if preferredToolsVersion.patch == 0, file == Manifest.basename + "@swift-\(preferredToolsVersion.major).\(preferredToolsVersion.minor).swift" {
202202
return true
203+
} else if preferredToolsVersion.patch == 0, preferredToolsVersion.minor == 0, file == Manifest.basename + "@swift-\(preferredToolsVersion.major).swift" {
204+
return true
203205
} else {
204206
return false
205207
}

Tests/WorkspaceTests/RegistryPackageContainerTests.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ final class RegistryPackageContainerTests: XCTestCase {
226226
let v5_3_3 = ToolsVersion(string: "5.3.3")!
227227

228228
func createProvider(_ toolsVersion: ToolsVersion) throws -> PackageContainerProvider {
229-
let supportedVersions = Set<ToolsVersion>([ToolsVersion.v5_3, v5_3_3, .v5_4, .v5_5])
229+
let supportedVersions = Set<ToolsVersion>([ToolsVersion.v5, .v5_3, v5_3_3, .v5_4, .v5_5])
230230
let registryClient = try makeRegistryClient(
231231
packageIdentity: packageIdentity,
232232
packageVersion: packageVersion,
@@ -334,6 +334,14 @@ final class RegistryPackageContainerTests: XCTestCase {
334334
let manifest = try await container.loadManifest(version: packageVersion)
335335
XCTAssertEqual(manifest.toolsVersion, .v5_5)
336336
}
337+
338+
do {
339+
let provider = try createProvider(.v5) // the version of the alternate
340+
let ref = PackageReference.registry(identity: packageIdentity)
341+
let container = try await provider.getContainer(for: ref) as! RegistryPackageContainer
342+
let manifest = try await container.loadManifest(version: packageVersion)
343+
XCTAssertEqual(manifest.toolsVersion, .v5)
344+
}
337345
}
338346

339347
func makeRegistryClient(
@@ -492,7 +500,13 @@ final class RegistryPackageContainerTests: XCTestCase {
492500
guard let registryIdentity = identity.registry else {
493501
preconditionFailure("invalid registry identity: '\(identity)'")
494502
}
495-
let versionString = version.patch == 0 ? "\(version.major).\(version.minor)" : version.description
503+
let versionString = if version.patch == 0 && version.minor == 0 {
504+
"\(version.major)"
505+
} else if version.patch == 0 {
506+
"\(version.major).\(version.minor)"
507+
} else {
508+
version.description
509+
}
496510
return "<http://localhost/\(registryIdentity.scope)/\(registryIdentity.name)/\(version)/\(Manifest.filename)?swift-version=\(version)>; rel=\"alternate\"; filename=\"\(Manifest.basename)@swift-\(versionString).swift\"; swift-tools-version=\"\(version)\""
497511
}
498512
}

0 commit comments

Comments
 (0)