swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.7k stars 1.33k forks source link

Dependency resolution with registry identity substitution does not work correctly with tools-version >= 5.8 #7912

Open robinkunde opened 3 weeks ago

robinkunde commented 3 weeks ago

Is it reproducible with SwiftPM command-line tools: swift build, swift test, swift package etc?

Description

Dependency resolution with registry identity substitution does not work correctly if any of the packages in the graph use a tools-version >= 5.8.

Example error:

error: 'swiftpackagemanagerbug': product 'Alamofire' required by package 'swiftpackagemanagerbug' target 'SwiftPackageManagerBug' not found in package 'Alamofire'. Did you mean 'Alamofire'?

With the aliasing rules introduced in 5.8, SPM uses package identities instead of the names, but generates an invalid package graph when trying to apply registry lookup rules. However, I'm not using aliasing feature in this case and do not know how it would impact this particular issue. Additional issues can be caused when specifying Target.Dependency using a string literal, but I don't know when this feature was introduced. Please note that different commands can exhibit different behaviors. In my testing, swift package show-dependencies is most susceptible to these issues. swift build, swift package resolve, and swift package update are less impacted for some reason.

--use-registry-identity-for-scm and --replace-scm-with-registry are also not impacted in the same way, but I need to do more testing here.

I believe these issue where missed because the relevant tests only run against tools-version < 5.8. I will open a PR with some of these tests update to reproduce the problem.

Expected behavior

Output with tools-version 5.7

$ swift package show-dependencies --use-registry-identity-for-scm
Updating https://github.com/Alamofire/Alamofire.git
Updated https://github.com/Alamofire/Alamofire.git (0.29s)
Computing version for https://github.com/Alamofire/Alamofire.git
Computed https://github.com/Alamofire/Alamofire.git at 5.9.1 (0.01s)
Removing https://github.com/Alamofire/Alamofire.git
Fetching https://github.com/Alamofire/Alamofire.git from cache
Fetched https://github.com/Alamofire/Alamofire.git from cache (0.00s)
Creating working copy for https://github.com/Alamofire/Alamofire.git
Working copy of https://github.com/Alamofire/Alamofire.git resolved at 5.9.1
.
└── default.alamofire<https://github.com/Alamofire/Alamofire.git@5.9.1>

Actual behavior

Output with tools-version 5.8

$ swift package show-dependencies --use-registry-identity-for-scm
error: 'swiftpackagemanagerbug': product 'Alamofire' required by package 'swiftpackagemanagerbug' target 'SwiftPackageManagerBug' not found in package 'Alamofire'. Did you mean 'Alamofire'?

Steps to reproduce

Reproduction steps

  1. Publish Alamofire 5.9.1 to registry (I used AWS CodeArtifact) with package-metadata.json below
  2. Create package with the Package.swift file below
  3. Set registry locally for package or globally
  4. Run swift package show-dependencies --use-registry-identity-for-scm

Package.swift

// swift-tools-version: 5.8
import PackageDescription

let package = Package(
    name: "SwiftPackageManagerBug",
    products: [
        .library(name: "SwiftPackageManagerBug", targets: ["SwiftPackageManagerBug"]),
    ],
    dependencies: [
        .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.9.1")),
    ],
    targets: [
        .target(
            name: "SwiftPackageManagerBug",
            dependencies: [
                .product(name: "Alamofire", package: "Alamofire"),
            ]
        ),
    ]
)

package-metadata.json

{
  "repositoryURLs": [
    "https://github.com/Alamofire/Alamofire.git",
    "https://github.com/Alamofire/Alamofire"
  ]
}

Swift Package Manager version/commit hash

Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)

Swift & OS version (output of swift --version ; uname -a)

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-macosx14.0 Darwin nalendar.local 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:46 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6031 arm64

SomeRandomiOSDev commented 2 weeks ago

+1. I'm having this same issue