简体   繁体   中英

Xcode build reports "no such module" for Swift Package Manager packages. Building again and again eventually fixes it

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, ie 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...

在此处输入图像描述

Second Build...

在此处输入图像描述

Third Build...

在此处输入图像描述

Fourth Build...

在此处输入图像描述

Fifth Build...

在此处输入图像描述

... finally it runs.

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

在此处输入图像描述

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


My workspace structure looks like this...

在此处输入图像描述

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...

在此处输入图像描述

在此处输入图像描述

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

Am I doing something wrong here?

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.

Are you launching the project by clicking on xcodeproj instead of xcworkspace ?

Also, have you researched other approaches like in this question? Getting error "No such module" using Xcode, but the framework is there

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

You can try Xcode CommandLine tool instead:

xcodebuild on the project directory

It usually works without this frustrating issue

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM