No such module found with archiving project with xcodebuild

I'm facing a strange issue where attempting to archive an application with xcodebuild produces an error where it can't find a specific module.

Below is a sample from the output from our CI/CD pipelines:

▸ Processing Info.plist
▸ Processing Watch app-Info.plist
    skipping copy phase strip, binary is code signed: /Users/[REDACTED]/Library/Developer/Xcode/DerivedData/App-hahppujjrqxvtqfuypdagaondkkd/Build/Intermediates.noindex/ArchiveIntermediates/app-Enterprise/IntermediateBuildFilesPath/UninstalledProducts/watchos/app Watch Extension.appex/app Watch Extension (in target 'Watch App' from project 'App')
❌  /Users/[REDACTED]/git/App/Classes/WebController.swift:10:8: no such module 'NetworkComponents'
import NetworkComponents
▸ Touching app\ (in target 'Watch App' from project 'App')
▸ Linking Tools.o
▸ Linking NetworkComponents.o

I am unsure as to why this is occurring, but it does not do this when building/archiving with Xcode, only with the xcodebuild command. I am wondering if this could be a race condition of some sort or if the fact that it links NetworkComponents.o after trying to compile a class that imports the module has anything to do with it?

Some additional information:

  • I am on Xcode 14.1 and Swift Package Manager 5.7.1
  • The module it fails to find is a Swift Package Manager dependency
  • Verified the issue is present on both an Intel-based Mac and a M1 Mac.
  • It is the same module that it states it can't find after running the xcodebuild archive command each time and It is for the same file as well. Issue is consistent across multiple runs.

The following are the commands that are executed:

  • xcodebuild -resolvePackageDependencies -project TestProject.xcodeproj -scheme TestProject -scmProvider system

  • xcodebuild "-workspace" "/Users/[REDACTED]/Desktop/TestProject/TestProject.xcworkspace" " scheme" "TestProject" -allowProvisioningUpdates

  • xcodebuild "archive" "-workspace" "/Users/[REDACTED]/Desktop/TestProject/TestProject.xcworkspace" "-scheme" "TestProject" -allowProvisioningUpdates

When I clear all derived data and caches (rm -rf ~/Library/Caches/org.swift.swiftpm/ and
rm -rf /Users/yalmatm/Library/Developer/Xcode/*) then the build will succeed the first time, but rerunning the third command to archive it will continue to produce the issue.

Update: This issue still needs troubleshooting but I have discovered what could be the cause.

Upon further tests, I have discovered the issue to be with the build configuration. With the scheme I have selected, my build configuration for archiving is not "Debug" or "Release", rather it is a custom build configuration. This appears to be similar to an issue that has been discussed in the past. When I switch the archive build configuration to Debug, the issue is no longer there but I do want to use a custom build configuration.

Some discussions state that a fix was made for this with Xcode 12.5, but I am seeing this issue on the latest Xcode as of the date of this post (Xcode 14.1). Additionally, I have not come across any threads out there that specifically detail this issue with the xcodebuild command.