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.

Using 3rd party packages, can't fix by the dynamic type since packages are added in Xcode UI. Definitely a bug in Xcode 11.4.

It would be great to get official advice on this. Having an extension that depends on the same third party lib as the main target isn't unheard of. It seems this should have never worked in the first place (which is fine), but then it's not nice to break this without a workaround.


I've moved most dependecies to SwiftPM, but still have cocoapods around so I've brought all dependecies that I use in extensions back to cocoapods.

This is still happening with the official release of Xcode 11.4. Obviously we don't have control over 3rd party packages to change static to dynamic, and of course many libraies are shared across targets and extensions. Please don't say this is a feature 😟

You can also create an internal library that merge all your static SPM package. It's a nicer workaround IMO.

Step by step explained here: https://github.com/renaudjenny/Swift-Package-Manager-Static-Dynamic-Xcode-Bug

I answer to my own comment. It worked yes, but my Unit Tests was broken because I used the same library (SnapshotTesting) for both my main Project and one of my library, so I still have double dependencies... even with .dynamic...

I found a workaround by adding an internal Framework who's dealing with SPM libraries. You can find a step by step here: https://github.com/renaudjenny/Swift-Package-Manager-Static-Dynamic-Xcode-Bug

Updating to Xcode 11.4 resulted in :


"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.".


My project contains an application (A) and a helper (H), both sharing some libraries. Some of these libraries are under my control: my own packages (X). Some libraries are however not under my control: github libraries (Y). Xcode needs dynamic libraries to build my project, not the static libraries I had been using until 11.4. If your project resembles mine, the follow may fix the problem:


Step 1 - Fix the libraries under my control (X)


In the libraries that have a package.swift that is under my control, look up the .library and add type: .dynamic, as described by tadija (see above)


Step 2: Fix the github libraries (Y)


Fixing external libraries needs a (simple) workaround. Create a fresh framework (Z) for your project. Relocate libraries that are (still) causing problems from A and H to Z.


Detailed instructions on how to do this: https://github.com/renaudjenny/Swift-Package-Manager-Static-Dynamic-Xcode-Bug (in particular step 4).


Step 3: Relax


Rebuild and run!



PS: Apple: when are you going to grow up to a responsible IT company, documenting your stuff and proving decent guidance? Instructions to avoid breaking stuff should be provided with release notes. Stop wasting our time to find needles in hackstacks.

That's work for me, thanks!

Accepted Answer

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.

While type: .dynamic works, and still needed on XC 11.4.1

Does anyone try to upload to the app store? I'm having invalid bundle issues.


Or for iOS projects, it's better just stick on 11.3?

Since XC 11.4 breaks embed frameworks, and SPM is not really ready for iOS.

I'm having the same issue but on Xcode 12 seed and all of my libraries have are dynamic. It looks like Xcode does not respect that.

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

This is still happening with Xcode Version 12.2 :(


Library/SPM issue with latest Xcode 11.4 beta
 
 
Q