SwiftPM package build via xcodebuild with specified derived data path

I have a build process that worked with Xcode 12.4 that no longer seems to work with Xcode 13.4. It seems specifying a derived data path breaks the SwiftPM package resolution.

$ xcodebuild -version
Xcode 13.4
Build version 13F17a

Broken

xcodebuild \
    -disableAutomaticPackageResolution \
    -usePackageSupportBuiltinSCM \
    -derivedDataPath "$PWD/derivedData" \
    -enableCodeCoverage YES \
    -scheme "$XCODE_SCHEME" \
    -destination "$DESTINATION" \
    build-for-testing

Works

xcodebuild \
    -disableAutomaticPackageResolution \
    -usePackageSupportBuiltinSCM \
    -enableCodeCoverage YES \
    -scheme "$XCODE_SCHEME" \
    -destination "$DESTINATION" \
    build-for-testing

Error

2022-07-18 17:44:18.870 xcodebuild[5927:47957] [MT] IDEFileReferenceDebug: [Load] <IDESwiftPackageCore.IDESwiftPackageDependencyFileReference, 0x600000f13900: name:Dependency path:absolute:/Users/buildbot/SwiftPMProject/derivedData/SourcePackages/checkouts/Dependency> Failed to load container at path: /Users/buildbot/SwiftPMProject/derivedData/SourcePackages/checkouts/Dependency, Error: Error Domain=com.apple.dt.IDEContainerErrorDomain Code=6 "Cannot open "Dependency" as a "Swift Package Proxy" because it is already open as a "Swift User Managed Package Folder"." UserInfo={NSLocalizedDescription=Cannot open "Dependency" as a "Swift Package Proxy" because it is already open as a "Swift User Managed Package Folder".}

Additionally there are notes that it failed to resolve the package product.

/Users/buildbot/SwiftPMProject/Package.swift: error: Missing package product 'Dependency' (in target 'SwiftPMProjectTests' from project 'SwiftPMProject')
/Users/buildbot/SwiftPMProject/Package.swift: error: Missing package product 'Dependency' (in target 'SwiftPMProjectPerformanceTests' from project 'SwiftPMProject')

It seems that xcodebuild no longer can resolve SwiftPM dependencies when -derivedDataPath is specified.

Are there any other new flags I should be aware of?

1 Like

The answer was found over on the Apple Developer forums in a thread titled Xcodebuild error Swift Package with -derivedDataPath. That thread links back to this forum and got me to this post: Xcode and Swift Package manager - #6 by simonpierreroy.

It seems that -derivedDataPath cannot be in the current working directory or if it is then the SwiftPM cache should not be. I am using -clonedSourcePackagesDirPath to move the SwiftPM cache outside the current working directory.

Solution

xcodebuild \
    -disableAutomaticPackageResolution \
    -usePackageSupportBuiltinSCM \
    -derivedDataPath "$PWD/derivedData" \
    -clonedSourcePackagesDirPath "$HOME/Library/Developer/Xcode/DerivedData/$XCODE_SCHEME"
    -enableCodeCoverage YES \
    -scheme "$XCODE_SCHEME" \
    -destination "$DESTINATION" \
    build-for-testing
1 Like

Hey, I just to let you know that they can be in the same folder. The derived data folder needs to be hidden.

In you example you can build with the following derived data path:
-derivedDataPath "$PWD/.derivedData" \