Xcode: Own Swift packages missing in Schema selector - swift

I have some SPM swift packages as part of my swift project.
After some Xcode crashes, those packages don't appear in the Schema selector (black arrow in image) any more.
So I can't select and build the swift packages any more separately.
What can I do to fix this?

For me it was solved by deleting .swiftpm/ folder
(Although, maybe just removing xcshareddata would've been enough)
Steps:
Removing dependency + reference from the main project
Delete .swiftpm/ of "problematic" local package
Add the package dependency back to the project
At least it worked.. Hoping there's a better way to solve this as well.

There must be a better way to resolve this, but at least it works:
Usual housekeeping:
Clear derived data
File -> Packages -> Reset Package Caches
restart Xcode
For each package that Xcode doesn't find / build:
Klick on package -> show in Finder
in finder, move the whole package into a temporary folder (package in Xcode gets red)
delete red package in Xcode
Create package again in Xcode with the same name (File -> New -> Package...), add package to appropriate targets again
remove the newly generated swift files in XCode, including Package.swift
drag the files in your copied folder into the corresponding place in the Xcode package
File -> Packages -> Reset Package Caches (for some reason Xcode loses external packages each iteration)
Build -> should be one less missing package in the error messages

Related

Editing a Package Dependency as a Local Package

Overview
I have a project that uses a remote swift package.
I would like to edit this package in the same project so that I can refine it based on the usage.
Aim:
To do this I am trying to add the same package as a local package so that I can edit it.
Reference:
https://developer.apple.com/documentation/swift_packages/editing_a_package_dependency_as_a_local_package
Problem:
When I drag and drop the cloned folder containing Package.swift into the project, I can't see any of the local package source files in the project. (see screenshot)
I have been breaking my head over it. Any help on this would be much appreciated.
Xcode Version:
13.2 (13C90) (downloaded from the developer portal not the AppStore)
The reason you cannot see the content of the package is that you have it open in another Xcode instance.
You can only edit a package in one Xcode instance at a time.
I find closing down Xcode completely, then opening up the particular project that I want to edit the package in solves the problem.

Xcode 13.1 bug editing dependency as local

Just upgraded to Xcode 13.1 and opened a SPM project by clicking the Package.swift file that has some dependencies.
In the former versions I could simply edit one of these dependencies by dragging a local clone of the package into the Project Navigator.
Xcode was then going to ignore the checked out dependency and let me edit the local one.
Apple documents this here.
When trying this with Xcode 13.1, Xcode just copies the folder into the project folder, like it does with normal files/folders that are not SPM projects.
Did I find a bug or missed I something?
Thanks!
Update: I found out how to work around by creating a .xcodeproj file with swift package generate-xcodeproj. There I could drag in the package as always. This works but is not the way to go, as the xcodeproj generation is deprecated by apple.
It looks like there is now an Add Local... button at the bottom of the add package wizard. Selecting the package from your local filesystem yields the same result as the "drag from finder" instructions you've linked.
The wizard is accessible from:
File > Add Packages, or
Project.xcodeproj > Package Dependencies > +.
I was having this problem, too, but after seeing the first comment on the other answer, I tried this:
I used File->Add Packages->Add Local…->navigate to local clone of dependency->Copy Package, which indeed seems to do nothing, but it puts a .package dependency line onto the clipboard.
I then edited my main project’s Package.swift to replace the existing Github dependency with the contents of the clipboard. Now I can edit the dependency’s files directly in the “Package Dependencies” portion of the project contents pane (which I wish you could do for a regular Xcode project too).
This solution kinda sucks, because it forces me to modify Package.swift, which means everyone on the team has to set things up the same way. Fortunately, I'm the only one working on this project, but future me is sure to be bitten by this.

Local Swift Packages Stopped Working in Xcode 13

I've local Swift Packages added to a workspace. They reside in the project subfolders and connected to different git repositories as git submodules. Everything had been working perfectly (the project was able to build, packages were able to resolve, and I could edit the packages within the same workspace).
After I updated Xcode to 13.0, the project started failing to build with multiple errors Missing package product for each local package dependency. Removing derived data, resetting packages, cleaning build folder and restarting Xcode didn't help.
Quit Xcode
Open Terminal
Navigate to the directory where your .xcodeproj is via Terminal (cd path/to/your/apps/folder)
Run xcodebuild -resolvePackageDependencies
After the packages finish resolving, open Xcode and try building again.
This is what worked for me:
Click on the project file then go to Project (not a target) -> Package Dependences tab.
Double-click on any Package and copy the location URL to clipboard.
Remove that Package using the - button.
Add it back, pasting the URL.
Clean Build Folder, then Build.
Alternatively, removing an unused package in the Target -> Build Phases -> Link Binary With Libraries, also removed the errors for me.
Removing package references from workspace and re-adding them (by simple drag-n-drop from Finder) resolved the problem.
I even didn't need to reconfigure corresponding schemes, or re-adding dependencies in targets.
If #lazarevzubov's answer doesn't help, try few additional steps:
Open xcode
Delete one or couple of libs under Build Phases / Link
Binary with Libraries
Discard changes under git
Clean project
Close xcode
Run xcodebuild -resolvePackageDependencies
Open xcode again and try to build
In XCode,
File > Swift Packages > Reset Package Caches

How to import a swift package that is using an older version of Swift tools?

I was trying to open a package, namely trying to import and run the swift folder under this repository.
When I Open->Package.swift file in XCode 11, it actually opened all the files under that directory. So, I thought I am one step close to running it.
But now I’m getting an error when I try to build it:
package at RepositoryName/swift’ is using Swift tools version 3.1.0 which is no longer supported; consider using ‘// swift-tools-version:5.1’ to specify the current tools version
So, I was wondering if there is a way to import and use this swift package that uses the older swift tools version?
Thanks
I solved this problem by recreating the package on XCode 11 with the same name and putting all files in the new package.
XCode provided already the Package.swift file so I didn't touch it. The first line describes the Swift tools version.
While adding it as a new package from File -> New -> Swift Package: I did set the "Add to" and "Group" to project name. And also I created the package inside this project folder.
I also added the package to target, selecting Target name -> General tab -> Frameworks, Libraries and Embedded Content -> + button to select it from the list.
Then within my project viewController I added
import [PackageNameHere]
Later on, I decided not to use it as a package, but in case anyone wants to do that this is how I did it.

Adding Local dependencies in XCode11 using SPM

Is it posible to integrate local dependencies with SPM in xcode 11,
File > Swift Packages > Add Package dependency...
It only seems to populate and let me select git repos hosted on bitbucket
I am just not sure if I am approaching the issue the correct way, maybe I should be using a Package.swift file instead? I have a lot of experience with Cocoapods but I would like to branch out and try other tools, especially if they are going to be supported in xcode as it seams SPM will be (Im using xcode11 beta 5)
This is the way I did it :
Drag and drop your package folder (in my example "DataStructures") from the finder directly into the Frameworks group of your target. You will see that the dropped item take a brown folder color (you can use the arrow to "get into it").
Go to your project target page, in the "Framework and Libraries" click the "+" button. Your package should show up in the "Workspace" area as a library.
Xcode 12
If you have a remote version of your package already in the project and want to work on a local. There are a few extra steps...
1. Make sure your local package's version is higher than the remote version
If you don't do that, it will continue to fetch the remote package
Select your project in Project Navigator
In the main window select Swift Packages
Select the package you want to replace with a local and remove it
Go to File > Swift Packages > Add Package Dependency...
Drag the your local package folder to the package search bar and add the prefix file://
Click on Next
Confirm that the minimum version is higher than the remote one.
Yep, Xcode makes this rudimentary task unnecessary difficult.
The trick is simple, though: in the sheet that pops up when you select 'Add Package Dependency…', in the URL field, you can enter a file:// URL. e.g. drag the package's folder into that field, which will place the path in it - e.g. '/Users/me/Documents/myShinyPackage/', then stick 'file://' to the front of it, and you'll be able to proceed.
Addendum: as some folks note in the comments below, this is problematic if you share the project with others, unless you standardise on paths. For individual use, however, it's fine.
Local Swift Package Manager(SPM)
[Dependency manager]
Key point is: file:// + git which you can use it in URL field
file://<path>
//e.g.
file:///Users/alex/Desktop/MyProject
Notes:
It works with git repository that is why please make sure that you committed your changes beforehand
when you add a SPM dependency File -> Swift Packages -> Add Package Dependency it is added into project -> Swift Packages and implicit dependency[About] is added to Frameworks, Libraries, and Embedded Content
when you committed new changes into framework do not forget File -> Swift Packages -> Update to Latest Package Versions
Product folder
<path_derived_data>/<randomizer>/Build/Products/Debug
//e.g.
/Users/alex/Library/Developer/Xcode/DerivedData/SPMExperiments-bmmqumztjedpekaajffnaegxzwzg/Build/Products/Debug
[Local CocoaPods]
[Local Carthage]
A new way(2022.11):
Simply drag the local package repo to your main project
Like this
Note: if you have that package open in another Xcode project you have to close it first.
By this way, Xcode will ignore the remote package in SPM. You can edit the local package repo directly and use it in main project.