Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Unit Test Error: symbol(s) not found for architecture x86_64 (Swift Package Manager)

I am having trouble getting unit tests to run in Swift projects created with the Swift Package Manager (that is, any unit tests created by the Package Manager... those I create from within Xcode work fine from within Xcode). I am getting the same error on all projects generated from the Package Manager. To keep it simple, I tried on a very basic test project with as little modification from the default setup as possible, but still getting the error. Here are the steps to reproduce:

  1. Create a new project with swift package init --type executable (module name is Hello)
  2. Add the Xcode Project: swift package generate-xcodeproj
  3. In Xcode build settings, ensure Enable Testability is Yes
  4. In swift.main enter this simple test code:
import Foundation

let message = "Hello, world!"
print(message)
  1. In HelloTests.swift:
import XCTest
@testable import Hello

class HelloTests: XCTestCase {
    func testExample() {
        XCTAssert(message == "Hello, world!")
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}
  1. Package.swift and XCTestManifests.swift left as-is.
  2. It builds and runs fine with swift build and swift run Hello (Also, from within in Xcode).
  3. However, when running swift test or running any test in Xcode, the build fails with the following error message:
Undefined symbols for architecture x86_64:
  "Hello.message.unsafeMutableAddressor : Swift.String", referenced from:
      implicit closure #1 : @autoclosure () throws -> Swift.Bool in HelloTests.HelloTests.testExample() -> () in HelloTests.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Somehow, it seems like it's failing to link the main module, so the symbols are not recognized. However, I can't tell what's wrong or how to fix it.

I downloaded one of the sample projects from GitHub, and generated the Xcode project. The tests for this project run perfectly in Xcode and the terminal. I've carefully compared the sample project to mine and can't tell what's different. Almost all setup code (Package.swift, file structure, etc.) and project setting are nearly identical. The only meaningful difference I can tell is that the sample project is a library/framework and mine is an executable (but seems like linking should work the same for both types). Otherwise, I can't tell what they are doing right and I am doing wrong.

like image 322
Justin Reusch Avatar asked Jan 31 '19 06:01

Justin Reusch


1 Answers

I figured it out (thanks to Cristik's help). Executable modules are not testable (at least for now), so the solution was to move all definitions to a library module and leave just the main.swift file in the executable module. That way, all unit tests were run with the library as a dependency vs. the executable. The package.swift now looks like this:

let package = Package(
    name: "HighestNumberPairing",
    products: [
        .executable(name: "HighestNumberPairing", targets: ["HighestNumberPairing"]),
        .library(name: "NumberPairing", targets: ["NumberPairing"]),
    ],
    dependencies: [],
    targets: [
        .target(
            name: "HighestNumberPairing",
            dependencies: ["NumberPairing"]),
        .target(
            name: "NumberPairing",
            dependencies: []),
        .testTarget(
            name: "NumberPairingTests",
            dependencies: ["NumberPairing"]),
    ]
)

The full program is here on Github.

like image 124
Justin Reusch Avatar answered Oct 02 '22 12:10

Justin Reusch