'How to trigger Xcode's 'Update to Latest Package Versions' from command line?
Goal:
I want to programmatically update the versions of Swift Packages consumed by a sample project.
Problem:
Xcode > 11 offers a menu option:
File > Swift Packages > Update to Latest Package Versions
This will update the Package.resolved
file to point to a specific revision.
This file is located at:
MyProject.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
How can I trigger this action from the command line for a CI build?
Solution 1:[1]
Try out xcodebuild -resolvePackageDependencies
Solution 2:[2]
xcodebuild -resolvePackageDependencies
does not seem to work reliably. It will often stubbornly refuse to update to the latest version, particularly when an SPM dependency is pointed to a branch rather than a version tag. Obviously Xcode is caching this information somewhere, but I could not figure out exactly which file it is. Even nuking the SourcePackages
folder in the derived data did not do the trick.
The only way I have found to do this reliably is to nuke the entire DerivedData
subfolder for the Xcode project.
derived=$(xcodebuild -showBuildSettings | grep -m 1 BUILD_DIR | grep -oE "\/.*" | sed 's|/Build/Products||')
# The above will return a folder with a name like /Users/you/Library/Developer/Xcode/DerivedData/MyProject-ehdkocavaenextdcrsaszjdmyssx
rm -rf "$derived"
xcodebuild -resolvePackageDependencies
It is completely safe to delete the DerivedData subfolder for a project. Xcode will simply regenerate it on demand.
Solution 3:[3]
If you have your rules set to next minor and the next version is major, clicking update won't work, you will have to change your package settings.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | joshbillions |
Solution 2 | Gregory Higley |
Solution 3 | ScottyBlades |