xcodebuild returns "Error: No such module"

We have a Mac app that uses several private GitHub hosted Swift Package Manager packages. The app builds file from within Xcode 13.4 whether the builds are Debug (Run) or Release (Archive).

However, when built via xcodebuild in Terminal, we get Error: No such module [our package] on an import [our package] statement in a Swift source file. I've tried many variants of the xcodebuild archive command, but nothing seems to fix the issue. We have two other nearly identical projects that work fine with the packages and build scripts. The command looks like:

xcodebuild \
	 -sdk macosx
	 -target "$target" \
	 -scheme "$scheme" \
	 -archivePath "$archivePath" \
	 -destination "generic/platform=macOS" \
	 archive  

or

xcodebuild \
	 -scheme "$scheme" \
	 -archivePath "$archivePath" \
	 -destination "generic/platform=macOS" \
	 archive  

Again, the app builds fine from within Xcode 100% of the time.

I'm sure we're using the latest tools: xcodebuild -version returns "Xcode 13.4 Build version 13F17a"

I suspect this either has something to do with the Swift packages all referencing a shared package [our package], but I checked the project and Package.swift files and they all use exactly the same name, urls, and capitalization, and the dependencies seem ok, or perhaps it's because we use Schemes to build the apps in three different flavors - dev, qa, and production - all with unique bundleIDs? We're using branch-based .package dependencies if that matters:

.package(name: "OurPackage", url: "https://github.com/account/OurPackage", Package.Dependency.Requirement.branch("main")),

I'd appreciate any suggestions on how to debug/resolve this. I've tried creating a smaller reproducible demo project but of course they all work fine.

Edit: Per a suggestion on iOS-developers, I ran xcodebuild -resolvePackageDependencies and all of the packages downloaded just fine. No errors.

Answered by EricS in 720723022

With the help of Apple engineers during the WWDC, we determined that my primary problem was that I used both --target and --scheme parameters to xcodebuild. Turns out --scheme alone works much better. There was another problem in my bash script and xcodebuild didn't return errors well in that case, but that was a secondary issue.

I am seeing the same issue for our iOS project with a 3rd party Swift Package. I can build and archive in Xcode from the GUI, but xcodebuild fails with 482 errors, and at least the last few are the same "error: no such module" error. Looking forward to anyone else weighing in here with any suggestions!

Accepted Answer

With the help of Apple engineers during the WWDC, we determined that my primary problem was that I used both --target and --scheme parameters to xcodebuild. Turns out --scheme alone works much better. There was another problem in my bash script and xcodebuild didn't return errors well in that case, but that was a secondary issue.

Thanks, the solution with the '-scheme' worked for me but it is odd that the same result can't be achieved with the '-target' parameter. There is only one scheme for a target so what could even go wrong here?

xcodebuild returns "Error: No such module"
 
 
Q