Swift 5 - Building from Package manifest resulting in error stating an incorrect number of targets for executable?

First, this is for the Swift 5 development branch. The error does not occur in Swift 4. What I'm not sure about is if this is a bug in the development branch, or if I'm just missing something that needs to be changed to work in Swift 5.

I am trying to build my project using my Package manifest with the Swift 5 toolchain. But my swift build -v is resulting in:

xcrun --sdk macosx --show-sdk-path
xcrun --sdk macosx --show-sdk-platform-path
xcrun --sdk macosx --find xctest
'MyProject' /Users/username/Code/MyProject: error: executable product 'MyProject' should have exactly one executable target

When switching to the Swift 4 toolchain (and updating the Package manifest language comment) the code builds correctly. Building the targets directly in Xcode works fine even with the Swift 5 toolchain (however, this is not being built with the Package manifest).

The Package manifest appears as:

// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyProject",
    products: [
        .executable(
            name: "MyProject",
            targets: ["MyProject"])
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "MyProject",
            dependencies: []),
    ]
)

If I remove the individual target from the executable target list, I get the error: product 'MyProject' doesn't reference any targets.

Is there some change to how the package manifest is laid out in Swift 5 that I'm missing that's resulting in this error? Or is this a possible bug in the Swift 5 development toolchain? Or is there something else I'm missing? Thank you!

(This is using the Swift 5.0 Snapshot 2018-12-13 (a) toolchain)
(Original Swift Reddit posting)

1 Like

I wasn't able to reproduce this. How does your package's file system structure look like?

I receive the error on a newly created package with the default package structure.

The minimal case I can produce is by doing the following:

mkdir Check
cd Check
swift package init
# Here edit Package.swift replacing `.library` with `.executable`
swift package generate-xcodeproj
error: executable product 'Check' should have exactly one executable target

Ah, I see what is going wrong here now. We consider a target to be executable based on the presence of a "main.swift" file in its sources. Since you're generating a library package, there won't be such a file, hence the error message. We should improve the error message here.

To resolve this, you can either use swift package init --type executable or place a "main.swift" file in the target's sources.

6 Likes

I see! Thank you! This is resolved then!

This just bit me also so I think improving the error message would be helpful ("no file 'main.swift' found" or something)

I also did the same create a new Swift Package Manager package (via File > New > Swift Package in Xcode 11.2.1). There was no option to specify an executable or library or anything like that in this UX flow.

1 Like

Yah, that's right. In Xcode, we only support using the default template (which is for a library package).

Thanks for the reply. Created a Feedback to request support for all types of Swift Package Manager packages in Xcode. FB7674119

cheers.

1 Like