How to add local Swift Package as dependency?

Same here in Xcode 13. When I delete the derived data folder, open Xcode then the local package is not found....

Build failures only appear if you add the package in the scheme editor (scheme it it dependency of) to the build section. But this is strange in times of implicit dependenc resolution...

i see, during this time i had actually solved the original issue.

it seems a default behavior in swift to hide build errors not in the current scheme proper (xcode considers spm external to the selected scheme i guess)

1 Like

This is the way! It's not enough to close and reopen the project. You have to close Xcode. This certainly smells like an Xcode bug.

Let's say you have a remote package dependency that you want to edit locally within the project that depends on it. Clone the package to your file system. Remove the remote package reference from your client project. Drag the folder containing the dependency Package.swift to the project explorer of your client project in Xcode. I usually drop it at root level so I see it front and center rather than in a sub folder. You'll see the package icon and name with no disclosure error allowing you to edit files. Kill Xcode completely. Reopen Xcode and your project. Ta dah! You can now edit as a local package dependency.

1 Like

You don't have to remove the remote package reference. If you add a local package to your project/workspace, Xcode will automatically use it instead of the remote version. Then, if you remove the local package, Xcode will automatically switch back to the remote version.

This is probably because you have the Package open in a separate window. You must close this window before dragging the package into another project.

1 Like

seems the only working solution for local repos. Tested under Xcode Version 13.3.1 (13E500a)

6 Likes

"You must close this window before dragging the package into another project."

Thanks Peter, this is the critical step, apparently never mentioned in the docs.

In my personal experience drag and drop no longer works.

On Xcode 15 (could be even earlier) you can add a local dependency by:

  1. Go to File > Add Package Dependencies
  2. Press the “Add local” button at the button of the window
  3. Choose the folder containing the package

Feedback: FB13306789

I wish the documentation Editing a package dependency as a local package | Apple Developer Documentation is updated to mention the above mentioned steps.

3 Likes

Yes drag and drop is broken. Maybe file a radar @NeoNacho can maybe help?

I have a project with a local SPM producing a bunch of warnings which don't show up when the package is opened in a Workspace containing another project.
I think the "Show all issues" setting could make them appear, but I'm not able to find it.
Opening Workspace Settings just shows the following:

Do you know by chance if they removed / moved it or any way I can enable it?

Great to see that the documentation has been fixed, thanks a lot for the team for updating the documentation!!

Editing a package dependency as a local package | Apple Developer Documentation

Thanks a lot for the information! Unfortunately, even though I followed the exact steps for adding my package to my project, I am still having issues with building and testing my project using xcodebuild. I can build, run, even test using Xcode, but can't with CLI.

xcodebuild -sdk iphonesimulator 
           -configuration Debug 
           -workspace MyProject.xcworkspace 
           -scheme MyScheme \
           -destination platform=iOS\ Simulator,name=iPhone\ 11,OS=17.5 
           -skipMacroValidation 
           clean build test 
           CODE_SIGNING_ALLOWED=NO
Testing failed:
	External macro implementation type 'Macros.TestMacro' could not be found for macro 'TestMember()'
	Type 'FooClass' does not conform to protocol 'FooProtocol'
	Type 'FooClass' does not conform to protocol 'BarProtocol'
	...
	Testing cancelled because the build failed.

I don't seem to be able to add a local git repo with a proper versioning tag, proper as in semantically versioned. Is this not possible?

Like this:

    .remote(url: "../LocalPackage", requirement: .exact("1.1.0")),

There's no idea of "what version" or version constraints when you're using a local package reference - it's explicitly the version of whatever exists (is checked out) at the location that you provide.

I use that to my advantage when making an example app that uses a library - the example App references the current release with the ../ThisPackage kind of setup, and lives in an ExampleApp directory in the repo of the package. Ends up working pretty nicely, all in all.

(There are some quirks, but they're more an Xcode annoyance than anything - if you have both the example App and the package.swift from the local package open, then Xcode seems to think it can't find the local package and throws all kinds of warnings.)

Example for "SwiftLint"

Go to ~/Library/Developer/Xcode/DerivedData/PROJECT/SourcePackages/checkouts/SwiftLint

Copy SwiftLint to ~/Desktop/PROJECT/packages/SwiftLint

Remove ~/Desktop/PROJECT/packages/SwiftLint/.git

Go to ~/Desktop/PROJECT/Package.swift and replace .package(url: "https://github.com/realm/SwiftLint.git", exact: "...") to .package(path: "packages/SwiftLint").