SPM Support for `@main` Executable Targets

The release of Swift 5.3 supports @main as an entry point for for applications, including command line. It looks like SPM does not support this.

I started a simple executable using swift package init --type executable and implemented print("Hello World") in main.swift. At that point swift run` executes fine.

Changing main.swift to HelloWorld.swift

@main
struct HelloWorld {
    static func main() {
        print("Hello, world!")
    }
}

Executes fine from Xcode, but results in " error: no executable product available" from swift run. Checking the output of both variants from swift run -v shows that swiftc is never executing for the @main version of the implementation.

I feel like both the CLI and Xcode should result in the same behavior.

This is a known limitation. See SR-12683.

I think SE-0294 fixes it. Not sure if it gets in Swift 5.4, though.

1 Like

Edit: Sorry, @wowbagger. Your post first appeared on my screen after I posted. I had no intention of stomping your answer. Had I seen it first, I would not have posted.


Parts of the implementation of @main were ready in time for 5.3, others weren’t. SwiftPM’s support fell in the second category. At the cutoff, SwiftPM still determined what was and wasn’t an executable based on the presence or absence of a file named main.swift. That has changed since.

1 Like

I saw your post right after I posted too. I guess we just happened to post within seconds of each other.

I think your post still adds value. At least, it explains better than mine. So I'm glad that you posted.

Thanks! I'll look forward to the update.

Terms of Service

Privacy Policy

Cookie Policy