swiftlang / swift-package-manager

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

SPM fails when a dependent package name is same as project directory name #8030

Open sebsto opened 2 weeks ago

sebsto commented 2 weeks ago

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

Description

I'm creating a very simple project using Soto and the Swift Runtime for AWS Lambda as dependencies. When trying to build, I receive this :

$ swift build

Fetching https://github.com/soto-project/soto.git from cache
Fetched https://github.com/soto-project/soto.git from cache (0.07s)
Creating working copy for https://github.com/soto-project/soto.git
Working copy of https://github.com/soto-project/soto.git resolved at 7.0.0
Removing https://github.com/soto-project/soto.git
error: 'soto': product 'SotoS3' required by package 'soto' target 'SotoExample' not found in package 'soto'.
swift --version && uname -a 
Apple Swift version 6.0.1 (swift-6.0.1-RELEASE)
Target: arm64-apple-macosx15.0
Darwin bcd07458a91c 24.0.0 Darwin Kernel Version 24.0.0: Tue Sep 24 23:39:07 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T6000 arm64

To try to start from a clean situation:

Expected behavior

Build should work

Actual behavior

Build fails with

error: 'soto': product 'SotoS3' required by package 'soto' target 'SotoExample' not found in package 'soto'.

Steps to reproduce

To reproduce that problem, be sure to name your project directory exactly as the library you're importing. In this example, I use the soto library

mkdir Soto && cd Soto
swift package init --type executable 

cat << EOF > Package.swift
// swift-tools-version: 6.0
import PackageDescription

#if os(macOS)
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)]
#else
let platforms: [PackageDescription.SupportedPlatform]? = nil
#endif

let package = Package(
    name: "SotoLambdaExample",
    platforms: platforms,
    products: [
        .executable(name: "SotoLambdaExample", targets: ["SotoExample"])
    ],
    dependencies: [
        .package(url: "https://github.com/soto-project/soto.git", from: "7.0.0"),
        .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
        .package(url: "https://github.com/swift-server/swift-aws-lambda-events", branch: "main")
    ],
    targets: [
        .executableTarget(
            name: "SotoExample",
            dependencies: [
                .product(name: "SotoS3", package: "soto"),
                .product(name: "AWSLambdaRuntime",package: "swift-aws-lambda-runtime"),
                .product(name: "AWSLambdaEvents",package: "swift-aws-lambda-events"),
            ]
        )
    ]
)
EOF

cat << EOF > Sources/main.swift
import AWSLambdaRuntime
import AWSLambdaEvents

import SotoS3

let client = AWSClient()
let s3 = S3(client: client, region: .useast1)

func handler(event: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response {

    var response: APIGatewayV2Response
    do {
        context.logger.debug("Reading list of buckets")

        // read the list of buckets 
        let bucketResponse = try await s3.listBuckets()
        let bucketList = bucketResponse.buckets?.compactMap { $0.name }
        response = APIGatewayV2Response(statusCode: .ok, body: bucketList?.joined(separator: "\n"))
    } catch {
        context.logger.error("\(error)")
        response = APIGatewayV2Response(statusCode: .internalServerError, body: "[ERROR] \(error)")
    }
    return response
}

let runtime = LambdaRuntime.init(body: handler)

try await runtime.run()
try await client.shutdown()
EOF
$ tree ../soto

../soto
├── Package.resolved
├── Package.swift
└── Sources
    └── main.swift

2 directories, 3 files

$ swift build 
Fetching https://github.com/swift-server/swift-aws-lambda-events from cache
Fetched https://github.com/swift-server/swift-aws-lambda-events from cache (0.47s)
Fetching https://github.com/swift-server/swift-aws-lambda-runtime.git from cache
Fetched https://github.com/swift-server/swift-aws-lambda-runtime.git from cache (0.51s)
Fetching https://github.com/apple/swift-nio.git from cache
Fetching https://github.com/apple/swift-log.git from cache
Fetching https://github.com/apple/swift-http-types.git from cache
Fetched https://github.com/apple/swift-http-types.git from cache (0.50s)
Fetched https://github.com/apple/swift-log.git from cache (0.53s)
Fetched https://github.com/apple/swift-nio.git from cache (0.58s)
Computing version for https://github.com/apple/swift-log.git
Computed https://github.com/apple/swift-log.git at 1.6.1 (0.57s)
Computing version for https://github.com/apple/swift-nio.git
Computed https://github.com/apple/swift-nio.git at 2.74.0 (0.90s)
Fetching https://github.com/apple/swift-system.git from cache
Fetching https://github.com/apple/swift-collections.git from cache
Fetching https://github.com/apple/swift-atomics.git from cache
Fetched https://github.com/apple/swift-system.git from cache (0.42s)
Fetched https://github.com/apple/swift-atomics.git from cache (0.42s)
Fetched https://github.com/apple/swift-collections.git from cache (0.43s)
Computing version for https://github.com/apple/swift-atomics.git
Computed https://github.com/apple/swift-atomics.git at 1.2.0 (0.67s)
Computing version for https://github.com/apple/swift-system.git
Computed https://github.com/apple/swift-system.git at 1.3.2 (0.57s)
Computing version for https://github.com/apple/swift-http-types.git
Computed https://github.com/apple/swift-http-types.git at 1.3.0 (0.71s)
Computing version for https://github.com/apple/swift-collections.git
Computed https://github.com/apple/swift-collections.git at 1.1.4 (0.89s)
Creating working copy for https://github.com/apple/swift-atomics.git
Working copy of https://github.com/apple/swift-atomics.git resolved at 1.2.0
Creating working copy for https://github.com/apple/swift-log.git
Working copy of https://github.com/apple/swift-log.git resolved at 1.6.1
Creating working copy for https://github.com/apple/swift-collections.git
Working copy of https://github.com/apple/swift-collections.git resolved at 1.1.4
Creating working copy for https://github.com/apple/swift-nio.git
Working copy of https://github.com/apple/swift-nio.git resolved at 2.74.0
Creating working copy for https://github.com/apple/swift-system.git
Working copy of https://github.com/apple/swift-system.git resolved at 1.3.2
Creating working copy for https://github.com/swift-server/swift-aws-lambda-runtime.git
Working copy of https://github.com/swift-server/swift-aws-lambda-runtime.git resolved at main (83bd667)
Creating working copy for https://github.com/apple/swift-http-types.git
Working copy of https://github.com/apple/swift-http-types.git resolved at 1.3.0
Creating working copy for https://github.com/swift-server/swift-aws-lambda-events
Working copy of https://github.com/swift-server/swift-aws-lambda-events resolved at main (ec7e66d)
error: 'soto': product 'SotoS3' required by package 'soto' target 'SotoExample' not found in package 'soto'.

Swift Package Manager version/commit hash

Apple Swift version 6.0.1 (swift-6.0.1-RELEASE)

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

Apple Swift version 6.0.1 (swift-6.0.1-RELEASE) Target: arm64-apple-macosx15.0 Darwin bcd07458a91c 24.0.0 Darwin Kernel Version 24.0.0: Tue Sep 24 23:39:07 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T6000 arm64

sebsto commented 2 weeks ago

I found a workaround. This behaviour seems to be triggered by the name of the directory.

➜  Soto pwd
/Users/stormacq/Desktop/Soto

➜  Soto swift build
error: 'soto': product 'SotoS3' required by package 'soto' target 'SotoExample' not found in package 'soto'.

Then

➜  Soto cd ..
➜  Desktop mv Soto SotoS3
➜  Desktop cd SotoS3/
➜  SotoS3 rm -rf .build 
➜  SotoS3 swift build

....

Build complete! (89.44s)
sebsto commented 2 weeks ago

Actually, when the project directory name is identical as the name of the package, SPM fails

➜  Desktop mv Soto swift-aws-lambda-runtime
➜  Desktop cd swift-aws-lambda-runtime/
➜  swift-aws-lambda-runtime swift build
Fetching https://github.com/soto-project/soto.git from cache
Fetched https://github.com/soto-project/soto.git from cache (0.07s)
Creating working copy for https://github.com/soto-project/soto.git
Working copy of https://github.com/soto-project/soto.git resolved at 7.0.0
Removing https://github.com/swift-server/swift-aws-lambda-runtime.git
error: 'swift-aws-lambda-runtime': product 'AWSLambdaRuntime' required by package 'swift-aws-lambda-runtime' target 'SotoExample' not found in package 'swift-aws-lambda-runtime'. Did you mean 'AWSLambdaEvents'?