Skip to content
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

Fix resolve failing when package from registry is referenced by name #8166

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

fortmarek
Copy link

@fortmarek fortmarek commented Dec 9, 2024

Fix resolve failing when package from registry is referenced by name

Motivation:

When running swift package --replace-scm-with-registry --default-registry-url {our-registry-url} resolve, the command fails when depending on packages that reference their transitive dependencies by name.

I created a demo where you can reproduce the issue by running swift package --replace-scm-with-registry --default-registry-url {our-registry-url} resolve. You should see the following error:

error: 'cpisciotta.xcbeautify': unknown dependency 'Colorizer' in target 'XcbeautifyLib'; valid dependencies are: 'swift-argument-parser' (from 'https://github.com/apple/swift-argument-parser.git'), 'getGuaka.Colorizer', 'MaxDesiatov.XMLCoder'
error: 'cpisciotta.xcbeautify': unknown dependency 'XMLCoder' in target 'XcbeautifyLib'; valid dependencies are: 'swift-argument-parser' (from 'https://github.com/apple/swift-argument-parser.git'), 'getGuaka.Colorizer', 'MaxDesiatov.XMLCoder'

The issue boils down to:

// Root Package.swift
import PackageDescription

let package = Package(
    name: "package-test",
    dependencies: [
        // We're depending on `xcbeautify` from our root `Package.swift`
        .package(url: "https://github.com/cpisciotta/xcbeautify", exact: "2.13.0")
    ],
    targets: [
        .target(name: "package-test"),
    ]
)

// Package.swift at https://github.com/cpisciotta/xcbeautify/blob/main/Package.swift
import PackageDescription

let package = Package(
    name: "xcbeautify",
    products: [
        .library(name: "XcbeautifyLib", targets: ["XcbeautifyLib"]),
    ],
    dependencies: [
        .package(
            url: "https://github.com/getGuaka/Colorizer.git",
            .upToNextMinor(from: "0.2.1")
        ),
        .package(
            url: "https://github.com/MaxDesiatov/XMLCoder.git",
            .upToNextMinor(from: "0.17.1")
        ),
    ],
    targets: [
        .target(
            name: "XcbeautifyLib",
            dependencies: [
                // Transitive package products referenced by name
                "Colorizer",
                "XMLCoder",
            ]
        ),
    ]
)

The issue is that when swizzling the package product names to their identity counterparts, only dependencies referenced via .product(...) are swizzled. Indeed, when we change the XcbeautifyLib's dependencies to:

.product(name: "Colorizer", package: "Colorizer"),
.product(name: "XMLCoder", package: "XMLCoder"),

the resolution succeeds.

Modifications:

To fix the above issue, we swizzle also dependencies referenced by name iff the product name is the same as the package name the root depends on. This aligns with how byName is treated in other parts of the codebase, such as here.

Result:

swift package --replace-scm-with-registry --default-registry-url {our-registry-url} resolve succeeds when run in the repro sample.

Comment on lines 263 to 264
// makes sure we use the updated package name for target based dependencies
let modifiedPackageName = targetDependencyPackageNameTransformations[packageName] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: formatting seems off by one whitespace here

Suggested change
// makes sure we use the updated package name for target based dependencies
let modifiedPackageName = targetDependencyPackageNameTransformations[packageName] {
// makes sure we use the updated package name for target based dependencies
let modifiedPackageName = targetDependencyPackageNameTransformations[packageName] {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indented with Xcode's Editor > Structure > Re-indent. Are you sure you'd prefer it to be re-indented as you suggested instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per CONTRIBUTING.md we prefer SwiftFormat instead of Xcode or swift-format in this repository. It's also very surprising that Xcode suggests 3-spaces formatting even if for a few lines. Regardless, I don't insist applying SwiftFormat here unless you can easily avoid mass reformatting the whole file to keep the diff of this PR manageable.

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo some formatting nits. Thanks!

@MaxDesiatov MaxDesiatov added bug package manifests changes to package manifest APIs registry SwiftPM Registry-related changes labels Dec 9, 2024
@MaxDesiatov
Copy link
Contributor

@swift-ci test

@fortmarek
Copy link
Author

Thanks @MaxDesiatov for the speedy review (I don't think I've ever had my PR reviewed faster in an open source repository 😅). I applied the SwiftFormat changes, let's see what the CI has to say about this PR 👀

@MaxDesiatov
Copy link
Contributor

@swift-ci test

@fortmarek
Copy link
Author

Hey @MaxDesiatov 👋

Are we waiting for some additional reviews or is there anything else that needs to be done before this PR can be merged?

@MaxDesiatov
Copy link
Contributor

@swift-ci test windows

@MaxDesiatov
Copy link
Contributor

This can't be merged currently due to https://forums.swift.org/t/holiday-schedule-2024/75954

@fortmarek
Copy link
Author

This can't be merged currently due to https://forums.swift.org/t/holiday-schedule-2024/75954

Got it, thanks for the quick response. Happy holidays 🎄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug package manifests changes to package manifest APIs registry SwiftPM Registry-related changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants