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.

1 Like

I see! Thank you! This is resolved then!

Terms of Service

Privacy Policy

Cookie Policy