Search code examples
xcodeswift-package-manager

Xcode: how to update a package to the branch last commit via SPM


I'm working on a Swift package and on an app using it, with Xcode 14.2

Due to incremental updates, I reference my package from my app with the branch I'm working on (for example: feature/current-dev)

Then I push a new commit on the package repository. How do I get this very last commit in the app?

Here's what I tried:

  • right click on my package, Update package => doesn't do anything
  • right click on "Package Dependencies", update to latest package versions => doesn't do anything
  • right click on "Package Dependencies", "reset Package Caches" (takes forever when you also use big packages as Firebase) => new commit still missing
  • manually deleted package caches (thanks to https://stackoverflow.com/a/68876107/127493), then update again => commit still missing
  • removing the package from the app, waiting, adding the package again => Nope!
  • ...

At this point I feels Xcode is trolling me. Is this the expected SPM behaviour? How to get the last commit?


Solution

  • Update commit hash in Package.resolved

    It's a pity that this workflow is not supported by UI commands. (Xcode 16.1)
    Discussions on topic (1)

    Solution

    In you Package.resolved you can find your dependency in pins field and
    update state.revision commit hash to latest from your branch.

    Xcode will automatically update dependency and all its own dependencies.
    (At least it works for Xcode 16.1)

    Use V3 of Package.resolved

    If it doesn't work, make sure that your Package.resolved is using V3 Schema because it contains originHash. V3 Schema was introduced in 5.10

    prefer versions in package.resolved file when one exists and matches the original manifest by @tomerd in #6698

    How to make sure that v3 is used?

    You can see current version at the bottom of .resolved file.

    To support v3 you need to make sure that Swift 5.10 is used both by Xcode and any SPM package that is added as a reference to Xcode project/workspace.

    What is SPM package added as a reference to Xcode project/workspace?

    This is a way to edit SPM package along side with Xcode project/workspace.

    To add a reference, just drag and drop package folder anywhere into your workspace project navigator.

    Cases of usage
    • Package stored in same repository (that contains modules of your project)

    • Checkout of external SPM dependency that is already resolved in workspace.

    This is now an official way to enter editing mode for your dependencies on Xcode 16, confirmed by Developer Tools Engineer on apple forums. To exit editing mode, just delete the reference.

    In Xcode 16 "Add local" button is no longer have the same effect
    as described in Apple documentation Editing a package dependency as a local package on 01/02/2025. In Xcode 16 it just replaces external dependency with local one.

    How to switch to Swift 5.10 in Xcode?

    Swift 5.10 became default for Xcode 15.3. You have two options:

    How to switch SPM package to Swift 5.10?

    Make sure you have at least 5.10 in the comment on top of Package.swift manifest.

    // swift-tools-version:5.10
    

    Make sure that you are editing the right Package.resolved

    • If you resolve dependencies for Xcode project MyApp.xcworkspace/xcshareddata/swiftpm/Package.resolved

    • If you resolve dependencies for SPM package file is
      next to manifest Package.swift for this SPM package.