@main and executableTarget use case

So attempting to play with the new executableTarget feature with @main I noticed some things

  1. "-parse-as-library" as library is required in the swift package manager per https://bugs.swift.org/browse/SR-12683
  2. You still cannot use the same source between executable and a library with the same source per https://forums.swift.org/t/two-libraries-with-shared-source-overlapping-error/32448

Hopefully the bug will be fixed soon. It does feel like #2 somewhat defeats the purpose of being able to specify main at any point. Are there plans to change this? At least for swift-only libraries & executables.

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

import PackageDescription

let package = Package(
    name: "CoolKids",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "CoolKids",
            targets: ["CoolKids"]),
         .executable(
            name: "tall",
            targets: ["TallKids"]),
         .executable(
            name: "cool",
            targets: ["CoolKidsExecutable"]),
           
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "CoolKids",
            dependencies: []),
        .executableTarget(
            name: "TallKids",
            dependencies: [],
            swiftSettings: [.unsafeFlags(["-parse-as-library"], .when(configuration: .debug))]
        ),
        // Swift is not happy with this because the source is the same as the Cool Kids library
        .executableTarget(
            name: "CoolKidsExecutable",
            dependencies: [],
            path: "Sources/CoolKids",
            swiftSettings: [.unsafeFlags(["-parse-as-library"], .when(configuration: .debug))]
        ),
        .testTarget(
            name: "CoolKidsTests",
            dependencies: ["CoolKids"]),
    ]
)
Terms of Service

Privacy Policy

Cookie Policy