xcodebuild and swift package dependencies

Hello,

I have an Xcode project (not a workspace), that depends upon a swift package found on GitHub. You can find the PR here that demonstrates the problem. https://github.com/bolsinga/MissingArt/pull/2

My project has a Swift Package dependency. It's a macOS target. If I Cmd-B in Xcode it will build and run just fine (finding the package and building it too). If I xcodebuild -verbose from the command line in CI, it will fail.

The problem is that when the application files compile and import the module found in the Swift Package, the module is not found.

If this were ObjC, I'd know that the search path was not found. I do not see anything about my module on the link line. I have followed the instructions about adding a Swift Package Mgr dependency, and it works locally.

I'm sure that I'm missing one small thing; I'm not sure if xcodebuild with no options (thus getting the default behavior) is the proper thing to do.

Any ideas?

Thank you, -- Greg

Answered by Bolsinga in 743018022

Thanks to the GitHub issue, I learned that SPM and legacy builds (which I think is the -configuration) do not work together. The trick is to xcodebuild -list, gather the scheme name you want to build, and then use xcodebuild -scheme <name-of-scheme>.

I just created a new macOS SwiftUI Project, and imported another SwiftUI library (using File → Swift Packages → Add Package Dependency). In the simple app's ContentView, I imported the module and added the new random SwiftUI view. I did Command-R, which is a Debug configuration by default, and it builds and runs, and shows the library UI.

If I then xcodebuild -verbose -configuration Debug it has the same problem. It has the same problem if I remove -configuration Debug. When compiling app Swift code, the compiler cannot find the module defined by the swift package manager.

/Users/bolsinga/Desktop/SPMText/SPMText/ContentView.swift:9:8: error: no such module 'MovingNumbersView'
import MovingNumbersView
       ^

How do I "inspect" a swift module? I may be able to learn it is being built incorrectly. How do I inspect where the swift compiler is "looking" for swift modules? I may be able to learn why it isn't looking in the right places.

Thanks for any tips! Perhaps there is a better place to ask this question?

Accepted Answer

Thanks to the GitHub issue, I learned that SPM and legacy builds (which I think is the -configuration) do not work together. The trick is to xcodebuild -list, gather the scheme name you want to build, and then use xcodebuild -scheme <name-of-scheme>.

Our project had already been using xcodebuild with '-scheme <name-of-scheme>', but fails now from the command line after we have added SPM dependencies. The build failures are all from trying to resolve GitHub repo dependencies for swift packages we're now using. Building from within the Xcode IDE works just fine opening the same 'project_name.xcworkspace' and building the same schemes. The first thing the IDE does when opening the workspace is resolve the SPM dependencies, before any attempt to do a debug or archive build. There is probably an xcodebuild option that does the same pre-flight SPM dependency check before trying to build the specified scheme. Unfortunately, I've not discovered what that magic incantation is yet.

A work-a-round we're using for now is our build scripts open the *.xcworkspace first to let the IDE gather together the SPM dependencies, then we can use xcodebuild to produce our archive and debug builds.

xcodebuild and swift package dependencies
 
 
Q