Library/SPM issue with latest Xcode 11.4 beta

I have a library I created with Swift Package Manager, and I'd like to use it in both my Containing App and Safari App Extension.


- I've added the library to the Project using the new "Swift Packages" tab in Xcode.

- The Safari App Extension Target has my library added under "Frameworks and Libraries".


With this configuration I get "Undefined symbol" compiler errors. If I add the library to the App under "Frameworks, Libraries, and Embedded Content", then I get:


"Swift package product '{SWIFT PACKAGE}' is linked as a static library by '{APP NAME}' and '{EXTENSION NAME}'. This will result in duplication of library code."


The odd thing is that the project compiles fine in Xcode 11.3.1. Did something change with SPM in the Xcode 11.4 beta? Maybe I should use a dynamic library? Or am I doing something else wrong?

Answered by rudedogg in 415218022

This appears to be fixed in Xcode 11.4.1. The release notes in feedback assistant say:


Swift Packages


Resolved Issues

Fixed an issue where an error like "Swift package product A is linked as a static library by B and C. This will result in duplication of library code." was incorrectly emitted if an app and an embedded app extension or helper tool statically linked the same package product. If you previously set the

DISABLE_DIAMOND_PROBLEM_DIAGNOSTIC 
build setting to work around this issue, you can delete this setting now. (59310009, 61227255)



I wish the communication from Apple was better about things like this. Do I ask about this bug in the SPM section of the Swift.org forums? Probably not, I'd be told that Xcode issues should be posted here. But then nothing really happens. I wish we could just tag teams like @Xcode @SPM to get their attention about issues like this.


Another gripe is that the Xcode 11.4.1 release notes inside the Mac App Store just say "Bug fixes and stability improvements". Why don't they at least mention to check the Feedback Assistant for the full release notes? Since the release notes in the App Store were empty, and the new iPhone SE was released that day, I figured Xcode 11.4.1 was just to add the iPhone SE simulator. It was luck that I tried compiling the project again - and dug deeper looking for actual release notes.


I just feel like this could have easily been resolved before Xcode 11.4 was even released if we had a better way to communicate with people working on these tools.


Anyway, thank you to everyone who offered workarounds.

Hi, exactly same issue !

I'm experiencing the same issue.

I think that you should indeed be using a dynamic library - by setting the product type of your SPM library to `.dynamic`.


However, I have a similar example where I have a framework and a command-line app, both of which use a couple of SPM libraries. I've got both a static and a dynamic product defined in the SPM libraries, and have added the dynamic versions to the Link Binary With Libraries panel for both the framework and the command line target.


And I'm still getting the above error with Xcode 11.4b1!


Xcode 11.3.1 will build successfully, but I'm getting a runtime error about symbols being duplicated, so it looks like Xcode is actually ignoring the fact that I've added the dynamic versions to my targets, and is using the static versions instead.


😟

Actually, after further investigation, I think that XC11.4 was correct in saying that I had a library linked twice. I was depending on other libraries which were pulling in the static version of the shared library. This is a real mess for any non-trivial graph 😟.


What we really need here is for SPM's `.automatic` product type to work, and do the right thing!

Happening to me as well 😟

Hey,

I think i had the same problem.. it worked in XC 11.3.1 and 1.4 beta broke it...


Now I'm not a 100% on this but i think it was because i used SwiftPackages by locally importing them to XC (via Drag&Drop, as shown @ WWDC). Now for some reason XC didn't quite get that I wanted to use those local packages INSTEAD of the remote ones so in some projects it kept the remote ones wheras in other targets (SiriKit extension, if that matters) it used the local ones and therefore had two different, yet same packages...

So I removed the remote ones in Project Settings > Swift Packages and added the local ones to all my targets again.. Now it works...



Hope it helps someone ^^

This error still appears in Xcode 11.4 beta 4. It seems to happen when the main target A imports another target B, a dynamic framework, and both of them import a static library C.


Dependency graph:

A -> B

A -> C

B -> C


It seems like removing the static library C dependency from the main target A solves the issue.


Updated dependency graph:

A -> B

B -> C


Hope this helps!

This is still a problem with Xcode 11.4 beta 3 (11N132i)


I use SPM to import two 3rd party packages.

They are each needed both in the iOS App and an accompanying share extension.


I do not know how to resolve this.

Same issue. Trying to add Alamofire 4.9.1 (fairly well-known library supporting both Cocoapods and SPM).

Same here :/ Is there any solutions ?


error: Swift package product 'Disk' is linked as a static library by 'Project' and 'Widget'. This will result in duplication of library code.

I solved thanks to cocoapods. I installed them with cocoapods instead of package manager. Now it is building without errors.

Still not fixed in final Xcode 11.4, this is definitely a bug, either in SPM or Xcode.

I had the same problem, here's what fixed it for me in Xcode 11.4:


1. explicitly set `type` to `.dynamic` for all `products` in "Package.swift":

.library(
     name: "***",
     type: .dynamic, // add this
     targets: ["***"]
),


2. re-add SPM libraries in Project / Target / Frameworks and Libraries:

- what used to be "***" - "Do Not Embed"

- now becomes "***" - "Embed & Sign"


After these changes, build & run on device and simulator works, along with archive & export, although I had some errors when exporting until I restarted Xcode and started over, after which it succeeded.


Btw, my setup is a workspace which contains:

- Single Swift package ("XXKit") with multiple targets (modules), defining multiple products (libraries)

- Main project with multiple targets (iOS app, Widget, Extensions) linking libraries from the "XXKit" package

- Pods project (for 3rd party stuff which still doesn't support SPM)


UPDATE:

- with the above solution, Xcode actually can't export IPA without errors

- real solution is to leave Package.swift as is, but to add a custom build setting DISABLE_DIAMOND_PROBLEM_DIAGNOSTIC = YES which will prevent incorrectly emitting build errors if both app and embedded app extension statically link the same library

- just remember to remove this build setting after this is fixed in Xcode since it might hide the real problems if you don't

- source: https://forums.swift.org/t/adding-a-package-to-two-targets-in-one-projects-results-in-an-error/35007/2

My project in an existing (pre Xcode 11.4 and pre-SPM). I used Xcode 11.4 to open the project, and added a package via the Xcode UI.


I couldn't find anywhere a generated Package.swift file to edit. It seems as these references are in the .xcodeproj file.

Thanks for the help, I used the

type: .dynamic

tips, and it works, thankfully because the two libraries I used was mine, so it's ok. But I think it's not a solution.

If you don't precise a type, Xcode should be smart enough to guess if it has to use either a dynamic or a static library, right? So why it's mendatory to explicit that in the package.swift of the library itself.

I would rather like to have an option in Xcode to use the library as dynamic rather than putting that in the library definition 😟

Library/SPM issue with latest Xcode 11.4 beta
 
 
Q