Search code examples
swiftswiftuiswift-package-manager

Module Not Found for Swift Package referenced by another Swift Package


We have two swift packages that we've created. One has some bare bones swift code and the second has some Swift UI code. When we create a Swift UI application, it can see classes from either of the swift packages. However, we can't get the Swift UI package to see code from the other bare bones swift code package.

Both of the packages are being hosted on github. In the Swift UI package, we added a package dependency to package.swift like this:

dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: "https://github.com/myorg/myswiftpackage", from: "1.0.1") 
],

When we try to use the classes from myswiftpackage, we get a "Module Not Found" error on the import statement.

What are we missing that allows a package to use code from another package?


Solution

  • Thanks Joakim, that was the answer. We had only listed it in the dependencies list, but both the targets also needed to have the package name listed as a dependency. Here's an example both references in case anyone needs it.

    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/myorg/myswiftpackage", from: "1.0.1")
    ],
    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: "MySwiftUiPackage",
            dependencies: ["MySwiftPackage"]),
        .testTarget(
            name: "MySwiftUiPackageTests",
            dependencies: ["MySwiftUiPackage", "MySwiftPackage"]),
    ]