4

I get "no such module" errors when trying to build after doing a "Clean Build Folder". Repeatedly trying to build eventually results in no errors.

IMPORTANT: The modules that can not be found are local packages with Swift Package Manager, i.e. not downloaded from git. I'm specifying these dependencies like this:

.package(url: "file:../CoreGraphicsExtensions", from: "0.0.0")

As seen in my /UIKitExtensions/Package.swift file listed here:

// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
        name: "UIKitExtensions",
        products: [
            // Products define the executables and libraries a package produces, and make them visible to other packages.
            .library(
                    name: "UIKitExtensions",
                    targets: ["UIKitExtensions"]),
        ],
        dependencies: [
            // Dependencies declare other packages that this package depends on.
            // .package(url: /* package url */, from: "1.0.0"),
            .package(url: "file:../CoreGraphicsExtensions", from: "0.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: "UIKitExtensions",
                    dependencies: []),
            .testTarget(
                    name: "UIKitExtensionsTests",
                    dependencies: ["UIKitExtensions"]),
        ]
)

Here is what I see in Xcode when I am trying to build.

First build...

enter image description here

Second Build...

enter image description here

Third Build...

enter image description here

Fourth Build...

enter image description here

Fifth Build...

enter image description here

... finally it runs.

But there are some strange warnings about the packages not being used by any target.

enter image description here

How can I get this to build correctly first time after a clean build?


My workspace structure looks like this...

enter image description here

Two projects in the workspace. BlenderViewer has the target I'm building. BlenderViewer has 6 local SPM packages. It also imports the PhyKit project as a framework, no problems there. The SPM packages have some dependencies between each other, but nothing circular.

Everything looks fine to me...

enter image description here

enter image description here

I'm running MacOS 12.5. Xcode 13.4.1. Building for iOS 15.3.

Am I doing something wrong here?

TJez
  • 1,969
  • 2
  • 19
  • 24

4 Answers4

3

I had the same error with an online package (after removing and re-adding it), and also had no luck with the other suggestions to clean derived data, reset package cache, and resolve dependencies from the command line.

What I found eventually, was that the package in question needed to be manually re-added to the app target in Build Phases > Link Binary with Libraries. This solved the issue.

Gsp
  • 401
  • 5
  • 10
1

I had the same with TestTarget which can't resolve the main target local SPM finally, fix with this approach I added all main target dependencies to the test target too and after that find, I had some compiler issues in which the compiler showed me this wrong error, and when errors were fixed this error disappeared

Amir Ardalan
  • 389
  • 3
  • 7
0

When you removed derived data or sometimes Xcode's package cache gets confused. This will usually result in weird build errors that can't really be explained. Here is things that might help:

Resetting the Xcode Package Cache: To reset the package cache, open the File menu, navigate to Packages, and click Reset Package Caches. This will delete all local package data and redownload each package from its source online. Wait till all the packages downloads

Jayesh Patel
  • 938
  • 5
  • 16
  • Thanks, I've reset all caches, etc. No difference. And actually - there are no online packages to download. There are only ***6 local SPM packages***. In fact, I believe this to be the whole problem. I don't think SPM can handle correct resolution of dependencies when using local packages (ie, file urls in dependencies, without git). – TJez Aug 10 '22 at 18:39
0

You can try Xcode CommandLine tool instead:

xcodebuild on the project directory

It usually works without this frustrating issue

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • Thanks for confirming that this is a bug with SPM's local packages. Unfortunately using xcodebuild doesn't work. I know I can make it work by scripting the dependencies above and beyond the dependencies I already specified in the packages. – TJez Aug 15 '22 at 19:07
  • For maintainability, it defeats the point of using Swift Package Manager if it ignores the package dependencies I already specified, and I have to write a script to duplicate this information and resolve dependencies for SPM. In my case I am able to use frameworks, and the dependencies are easy to manage there. When I am no longer building locally, I can then publish those frameworks to a non-local SPM package. Sad in 2022 SPM doesn't cope with non-trivial local packages with dependencies. – TJez Aug 15 '22 at 19:09