Cannot resolve dependencies when using a test only target from Swift Package

I'm having an issue resolving some dependencies in the test suite for my app when using a Swift Package in my test target.

Here's the setup: We have a Swift Package that contains two products. One product is the main library that is used throughout our app. The other product is a library of testing utilities, like mocks and such. This is a simplified version of its Package.Swift file with actual package/target/library/dependency names changed.

let package = Package(
  name: "Library",
  platforms: [
    .iOS(.v14)
  ],
  products: [
    // Products define the executables and libraries a package produces, and make them visible to other packages.
    .library(
      name: "Library",
      targets: ["Library"]),
    .library(
      name: "LibraryTestUtilities",
      targets: ["LibraryTestUtilities"]
    ),
  ],
  dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: <DependencyA URL>, .upToNextMajor(from: "1.0.0")),
    .package(url: <DependencyB URL>,.upToNextMajor(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: "Library",
      dependencies: [
        .product(name: "DependencyA", package: "DependencyA"),
        .product(name: "DependencyB", package: "DependencyB")

      ])
    ,
    .target(
      name: "LibraryTestUtilities",
      dependencies: [
        .target(name: "Library")
      ]
    ),
    .testTarget(
      name: "LibraryTests",
      dependencies: ["Library", "LibraryTestUtilities"]),
  ]
)

In our App, our target is linked to the "Library" product. Our test target is linked to "LibraryTestUtilities". When we try to build the test target, we're getting build errors in the form No Such Module DependencyA when compiling the "Library" product. Swift Package Manager is not able to link the dependencies from "Library" even though "LibraryTestUtiltities" is dependent on "Library". There are also no such issues resolving the "Library" dependencies when building the main app target or when building/testing the "Library" package directly. We are only having issues when trying to link the "LibraryTestUtilities" target to our app's test target through Build Phases -> Link Binary With Libraries.

I couldn't find any similar issues encountered online so perhaps our setup is misguided, though there doesn't seem to be any obvious reason why it wouldn't work. We would appreciate any help, thanks!

Answered by artclark in 740516022

I was able to solve this transitive dependency resolution issue by adding the testing utilities product target via the test target's "Target Dependencies" section within "Build Phases", instead of through "Link Binary With Libraries"

I forgot to include in the post, but we found a hack that works but it is pretty annoying. If we duplicate all of "Library"'s dependencies in "LibaryTestUtilities" we can get it to work, though it requires building the host application's target otherwise we will get the error No such module Library when building LibraryTestUtilities. Obviously this is not ideal so we would like to find a solution that allows us to use the testing utilities package without needing to add them to the main app. Ideally, they should only be linked in the test target.

Accepted Answer

I was able to solve this transitive dependency resolution issue by adding the testing utilities product target via the test target's "Target Dependencies" section within "Build Phases", instead of through "Link Binary With Libraries"

My "accepted" answer was actually a false positive as this still leads to linker errors when trying to build the test target.

Cannot resolve dependencies when using a test only target from Swift Package
 
 
Q