Xcode can't find my target's module after I changed the executable name in Package.swift

Viewed 220

I wanted to change the name of the executable produced from my Swift Package Manager project. Following this answer, my Package.swift now looks like this:

let package = Package(
    name: "Foo", // my package is called "Foo"...
    products: [
        // and I want my executable to be called "bar"
        .executable(name: "bar", targets: ["Foo"])
    ],
    targets: [
        .executableTarget(
            name: "Foo",
            dependencies: []),
        .testTarget(
            name: "FooTests",
            dependencies: ["Foo"]),
    ]
)

In main.swift in the "Foo" target, I have written a function to test in the "FooTests" target:

// in main.swift
func foo() -> Int {
    1
}

// in FooTests.swift
import XCTest
@testable import Foo

final class FooTests: XCTestCase {
    func testFoo() throws {
        XCTAssertEqual(foo(), 1)
    }
}

If I build the project by pressing command + B at this point, Xcode would complain that the module "Foo" cannot be found, highlighting the "@testable import" line in FooTests.swift. It also says that the foo method cannot be found in scope.

I'm quite confused. I've only changed the executable name, not the module name (which I think is the same as the target name).

I tried running this on the command line with swift test, and it works perfectly.

I've also tried @testable import bar (because I might as well), and the error message changes to:

Undefined symbol: bar.foo() -> Swift.Int

How can I fix this? I still want to use Xcode to test my project. After all, a big feature of XCTest is that it's nicely integrated with Xcode.

Note that I'm using Xcode 13.1 and I'm opening the Swift Package Manager project by opening the folder containing Package.swift with Xcode.

0 Answers
Related