Import Local Swift Package in Xcode 13.3

Hello!

I am struggling with the most basic things :-(

I created a Swift Package with the wizard and left it basically untouched, only added a func:

public struct TestPackage {

    public init() {

    }

    public static func hello()
    {
        print("Hello, World!")
    }
}

It just build fine.

Now I created a second project, this time a SpriteKit iOS app. In the file list on the left, right-clicked on "Packages" -> "Add Packages ..." -> "Add Local" and selected the root directory of TestPackage (containing the Package.swift) The Package now correctly appears in the "Packages" folder.

Opening a random Swift file in said SpriteKit iOS app, I expect to be able to

import TestPackage

But it tells me "No such module TestPackage".

Searching the internet, I somewhere read that I have to add the package also to the "Frameworks, Libraries and Embedded Content" section in the project's target settings, but I can't. Hitting the "+" button there does not give me my library in the list of suggested libraries. Via "Add Other" -> "Add package dependency" -> "Add Local ..." I can again select the root directory of my library, but other than showing up twice in the left-side folder view (and not in said Frameworks, Libraries and Embedded content", I have no luck in importing it to my code.

What am I doing wrong?

Answered by ronm in 709995022

It's a two step process. Load the package locally, then add the framework manually. Adding a remote package does both steps at once. Adding a local package does not.

In the "General" section of the Project setup, did you add "TestPackage" as a Framework? I had to do this for my local packages

Yes, but I always get to the same dialog presenting me all Xcode-internal Swift packages. If I hit the "Local ..." button at the bottom, I can again select the package from Finder, and a duplicate of the package shows up in the left-hand folder view under "Packages", nowhere else. The problem persists.

Either Apple doesn't want me to use local packages (which would be kinda stupid) or this is a serious bug. Can't believe it is THAT hard to include local Swift packages. I am running Xcode 13.3.

What you did is correct. I'm talking about a different place to enter the framework. In Project->General->Target you'll see a list. Identity.. Deployment Info... Frameworks and Libraries.. In here hit the +. Your local library should appear as an option and click it. I tried to insert a photo of what I'm talking about. Couldn't do it. Sorry

Accepted Answer

It's a two step process. Load the package locally, then add the framework manually. Adding a remote package does both steps at once. Adding a local package does not.

Thank you ronm for the great help!

As I found out now, it seems Xcode needs the Package being a Git repo in order to properly work, and the repo root directory has to be the root directory also for the package. (not in a sub-directory) As for now, this is fine with me, as I plan to version-control the package anyway.

I first added the package via General -> Project -> Package Dependencies -> "+" Button And then added it on the target as you suggested via General -> Target(s) -> Frameworks, Libraries and Embedded Content.

AFAICT it's actually just a one step process.

You can add to the target via General -> Target(s) -> Frameworks, Libraries and Embedded Content, and you're good to go.

I don't think you need the add via General -> Project -> Package Dependencies -> "+" step at all. Perhaps this is for local packages that have a local git repo but aren't part of the existing project.

In case anyone found this thread, here is an issue we found in Xcode 14.1. In the past we can add a local Swift package by dragging in the enclosing folder of the package manifest, and then add it to the "Frameworks, Libraries, and Embedded Content" section. The local package will appear under "Workspace" section.

We found that it works fine in Xcode 14.0.1, but in Xcode 14.1, the "Workspace" section disappears. Projects created prior to Xcode 14.1 still works and the framework shows up just fine in "Frameworks, Libraries, and Embedded Content". But if I remove the package from it and try to add it back, it doesn't show up.

This might be a bug in Xcode, or something has changed in the requirement of a local Swift package, we are still looking into it.

If you're building with a Workspace, in addition to making sure it's a local dependency in your project, you're also going to want to add it to the workspace file as a group:

<?xml version="1.0" encoding="UTF-8"?>
<Workspace
   version = "1.0">
   <FileRef
      location = "group:MISSING_PACKAGE">
   </FileRef>
    ...
</Workspace>

This is what allowed it to work in my setup on XCode Cloud where it was failing before.

Xcode 15.4 now says "The selected package cannot be a direct ancestor of the project.". Seems like this is intentional. But personally this feels like a huge oversight as this is kind of a necessary workflow when working on and testing libraries.

Import Local Swift Package in Xcode 13.3
 
 
Q