Valid Package but Import Gives "Module not found" Error

Hello. My package compiles successfully but when I try to import it I get the "Module not found" error.

The purpose of the package is to bundle a set of Swift bindings for a C++ API and make it as convenient as possible for other developers to use those bindings. The package supports both macOS and iOS, and is composed of the bindings along with several xcframeworks and a set of test modules. Each xcframework encapsulates a pair of precompiled C++ dylibs, one for macOS and another for iOS.

I use Xcode to add the package to my project via File->Add Packages... And then I add the package to Targets->Frameworks, Libraries and Embedded Content. The package also includes a bridging header, which I specify in Targets->Build Settings->Swift Compiler General->Objective-C Bridging Header.

Here is my package:

// swift-tools-version:5.3
import PackageDescription

let package = Package(
    name: "BrainFlow",
    platforms: [
        .macOS(.v10_14), .iOS(.v13)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "BrainFlow",
            targets: ["BoardController", "DataHandler", "MLModule", "BrainBitLib"])
    ],
    dependencies: [
        .package(name: "swift-numerics",
                 url: "https://github.com/apple/swift-numerics.git", .upToNextMajor(from: "1.0.0"))
    ],
    targets: [
        .target(
            name: "BrainFlow",
            dependencies: [.product(name: "Numerics", package: "swift-numerics")],
            sources: ["BoardDescription.swift",
                      "BoardShim.swift",
                      "BrainFlowConstants.swift",
                      "BrainFlowException.swift",
                      "BrainFlowExtensions.swift",
                      "BrainFlowInputParams.swift",
                      "DataFilter.swift",
                      "MLModule.swift"]
        ),
        .binaryTarget(
            name: "BoardController",
            path: "BoardController.xcframework"
        ),
        .binaryTarget(
            name: "DataHandler",
            path: "DataHandler.xcframework"
        ),
        .binaryTarget(
            name: "MLModule",
            path: "MLModule.xcframework"
        ),
        .binaryTarget(
            name: "BrainBitLib",
            path: "BrainBitLib.xcframework"
        ),
        .testTarget(
            name: "BrainFlowTests",
            dependencies: ["BrainFlow", .product(name: "Numerics", package: "swift-numerics")],
            sources: ["BoardShimTests.swift",
                      "BrainFlowCItests.swift",
                      "BrainFlowTests.swift",
                      "DataFilterTests.swift"]
        )
    ]
)

And here is a screenshot of Xcode showing the package contents and the error:

Thanks in advance,
Scott

1 Like

Is it intentional that your BrainFlow library product does not include your BrainFlow target?

I tried that but I got an error about cyclical references. Perhaps I did it wrong? I will try again.

That fixed it. Thanks. I can now do "import BrainFlow" without error.

The fix, per @lukasa, is:

    .library(
        name: "BrainFlow",
        targets: ["BrainFlow", "BoardController", "DataHandler", "MLModule", "BrainBitLib"])

Previously I had mistakenly added "BrainFlow" to the dependencies of target "BrainFlow", which was dumb.