-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to see XCode/SwiftUI Previews within CocoaPods frameworks #9275
Comments
Have no idea here sorry. |
I think Interface Builder doesn't work with static libs, does this work without setting |
It partially works with non-static libs. In the example at https://github.com/jayayres/cocoapodsswiftuipreviewbugreport , Pods/Development Pods/MyPod/MyDynamicPodSwiftUIView.swift is in a dynamic lib. If the first file previewed is in a dynamic lib, previews work in that one dynamic lib, but subsequently fail in the main app + other dynamic libs with the "failed to load library at path" error. If the first file previewed is in the main app, then subsequently previews fail in all dynamic libs. So it entirely depends on which dynamic lib has the file which is previewed first. |
So is this a cocoapods library issue? |
I see. There's definitely some weirdness in this, unfortunately there isn't much documentation on how we are supposed to set things up here. For example, I noticed that It's possible there's something else we could do here but I'm not 100% sure at the moment what we would need to change to make this work more reliably Edit: |
A bunch of fixes in Xcode 11.2 for Previews https://developer.apple.com/documentation/xcode_release_notes/xcode_11_2_release_notes |
This is interesting, would love to see how this manifests. I haven't tried the 11.2 beta at all yet |
I think I have a similar issue trying to use Xcode previews with UIKit. I got this error even on Xcode 11.2.1
I think this isn't a cocoapods issue, is from Xcode previews, looks like others are having similar problems with static frameworks that contains objective-c code see JetBrains/kotlin-native#3059 |
Hi, I'm able to reproduce 100% of the times this problem, with any pod, statically or dynamically linked. I'm trying with Xcode 11.2, 11.2.1 and 11.3 beta, and if the iOS Framework has a SwiftUI and at least one Pod, the preview crashes even if the project is completely empty: I just open a radar but maybe it could be some misconfiguration on the Pods project. It compiles correctly and runs correctly, only the SwiftUI Preview doesn't work. Any ideas on how to debug this to understand better the problem? |
I would recommend attempting to embed the framework natively in Xcode to see if that fixes the issue. We can then try to narrow down what CocoaPods is doing differently |
Thanks @amorde , I'm gonna try later today and share my findings. So far my workaround is creating the preview in any file that is in the Main App target, that works. But what's more interesting about this workaround is that, if I split the Xcode screen and open the View source code side-by-side to the preview running on main app, then it crashes again! |
Hi @amorde, I've added 2 branches to my repo:
Both work as expected, I even added some Rx logic to be sure the 3rd party is working. |
Thank you! I'll try to look into this when I get the chance |
Thanks for the support. From my side I'll try to understand better the issue when I have some time and will share here if I learn something. |
@luizmb This is more of a workaround I guess but I had success by copying the required frameworks in a folder called |
Hi @nicerice Thanks to your suggestion I managed to make preview work again. Very nice workaround. I think I didn't follow exactly what you suggested, because my logs pointed me to a slightly different direction (in my real-world project, I mean). Probably because my DerivedData is located in a custom place (~/code/DerivedData) and the intermediates and builds are subfolders of it. So in my case, logs pointed me to: Which is my custom Intermediates folder + "/Previews/MyProject/Products/Debug-iphonesimulator" From there I open MyViewLib.framework/Frameworks and noticed that only some frameworks were there, basically only the ones I use Xcode to link. Initially I tried to copy Pods_MyViewLib.framework into this folder, but that didn't help. So, instead, I copied all the dependencies that should be there, individually: From: To: And the SwiftUI Preview started working immediately. As you suggested, I'm gonna try to automate this process in my build phases. Thanks again for the help! |
Same issue, can also confirm placing the framework in another Frameworks folder fixed the Previews for me as well. |
Any luck in automating this? |
For now I am using a Xcode Build Phase of type run script, and running this:
Not super elegant but it's what I could come up with in a few minutes. One interesting thing that I found during writing this script, and it could be the root cause of this problem, is that "Build" and "Build for SwiftUI Preview" are two distinct things, and ALL environment variables are different, including the "/Previews" part between "Intermediates" and "PRODUCT_NAME". My assumption is that the Embed Pods Framework phase has some hardcoded path that's not working for the Previews because it's assuming the path incorrectly. I'm not familiar with CocoaPods source code but I can try to find something. |
|
Because the env vars are different when building for preview, I believe there should be a way to write this script that works regardless if it's build or preview. I didn't have time to check the Ruby code you sent me, but I'm gonna try when I have some time. |
Hmm, it looks like the embed frameworks script only runs for the app itself, and we need the frameworks copied to the correct folders for the frameworks. |
Anyone know if there are any mechanisms in cocoapods now to add script phases to the framework targets? |
You can add script phases both from |
Right, I was thinking internally in the cocoapods tool itself, but I'll check out how that works, should be similar. I think we need a script like the Embed Frameworks script, but that runs for every pod framework. Script will look similar to the one here. I think we should be able to just simlink and not copy though. |
Hello, |
Xcode : Version 11.4.1 (11E503a) |
Would appreciate someone open a PR here with the proposed fix and a test. This is a long issue thread and its hard to follow which answer is the right one. |
I used Gemfile for cocoapods and Ruby 2.6.4p104. so I had this problem. then
I resolve this problem. This is diff.
|
can someone verify with Xcode 12 Beta 5? https://twitter.com/NeoNacho/status/1295793181871599616 |
Unfortunately still happens for us (Firebase) using Xcode 12 beta 6, workaround of disabling code coverage still works. Repro steps for us: firebase/firebase-ios-sdk#6219 (comment) |
Tried running a preview that depends on AsyncDisplaykit (Texture pod) with Xcode 12 beta 6 the following workaround 👍 class Pod::Target::BuildSettings::AggregateTargetSettings
alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths
def ld_runpath_search_paths
return ld_runpath_search_paths_original unless configuration_name == "Debug"
return ld_runpath_search_paths_original + framework_search_paths
end
end
class Pod::Target::BuildSettings::PodTargetSettings
alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths
def ld_runpath_search_paths
return (ld_runpath_search_paths_original || []) + framework_search_paths
end
end
I tried disabling code coverage and that didn't work as well. |
also crashes in Xcode 11.6 |
I'm having this issue with Xcode 12 Beta 6. Didn't succeed with the workaround posted by @mycroftcanner / @jameshurst but it's possible that I'm doing something wrong here. Code coverage seems already disabled (maybe from earlier Podfile configuration) so workaround from @dannypier (hi!) can't work for me either. |
Hello! Based on @jameshurst and @kerrmarin, I fixed my issue with this code in my Podfile: class Pod::Target::BuildSettings::AggregateTargetSettings
alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths
def ld_runpath_search_paths
return ld_runpath_search_paths_original unless configuration_name == "Debug"
return (ld_runpath_search_paths_original || []) + (framework_search_paths || [])
end
end
class Pod::Target::BuildSettings::PodTargetSettings
alias_method :ld_runpath_search_paths_original, :ld_runpath_search_paths
def ld_runpath_search_paths
return (ld_runpath_search_paths_original || []) + (framework_search_paths || [])
end
end It removes all errors about Then, as I was using Firebase, I found in the crash logs that it also doesn't run the ApplicationDelegate methods and so I also missed the configuration of my FirebaseApp. So I needed to add also a manual configuration in my preview code like this: let options = FirebaseOptions(googleAppID: "my_id_from_ GoogleService-Info.plist file", gcmSenderID: "my_id_from_ GoogleService-Info.plist file")
FirebaseApp.configure(options: options) BTW, I'm using Xcode 11.7 |
I dont know why or how but the first answer here fixed this issue |
The problem is caused by Code Coverage being enabled. Above StackOverflow contains a fix. Thank you! |
The problem with static libraries is resolved according to CocoaPods/CocoaPods#9275 (comment) Signed-off-by: Hassan Shahbazi <hassan@ninchat.com>
The "AggregateTargetSettings" script doesn't seem to work with ruby 2.6, but I managed to get the similar results with a post install script.
apparently |
Slightly reworded solution from @FredericRuaudel (that employs prepend vs alias_method), that does not break when Podfile is sourced twice, e.g. when using https://github.com/square/cocoapods-generate:
Just in case, the net result is literally the same as with the original solution. |
Related StackOverflow question. |
In Xcode 14 our previews are now failing due to a missing symbols error, so it wasn't enough by modifying the linker search paths, we needed to explicitly set the transitive framework dependencies too, this is how we solved it but there must be a better solution:
|
Unfortunately, we're facing a similar problem quite a some time. Some of the solutions above helped us in Xcode 13, however since we're Xcode 14, previews doesn't seem to work.
Somehow if we manually, remove the pod framework Pods_XXX from linked libraries in app target and add our pod add our framework (CustomUIFramework) manually, it renders the Previews but with the pod install, it originally links the pod framework and we're going back to error case. Exact same configuration works in Xcode 13 though. |
Working perfectly for previews, but it block the debug running on rosseta simulators. |
This issue still occured in xcode 15.1 beta 3. common pods into main target and framework and framework embed into main target. failing SwiftUI Preview. |
Disabling this setting helped me |
Report
What did you do?
Sample project https://github.com/jayayres/cocoapodsswiftuipreviewbugreport
git clone https://github.com/jayayres/cocoapodsswiftuipreviewbugreport.git
cd cocoapodsswiftuipreviewbugreport ; bundle install --binstubs ; bundle exec pod install
Open in Xcode 11.1, on Mac OS Catalina
Try using SwiftUI preview functionality for Pods/Development Pods/MyStaticPod/MyStaticPodSwiftUIView.swift , Pods/Development Pods/MyPod/MyDynamicPodSwiftUIView.swift, and MyPodTest/MyPodTest/MyMainAppSwiftUIView.swift
What did you expect to happen?
XCode previews should work for all 3 files.
What happened instead?
XCode previews always fail for MyStaticPodSwiftUIView.swift (which is set up with static_framework = true in its podspec) with:
noPreviewInfos(arch: "x86_64", sdkRoot: "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.1.sdk")
XCode previews fail most of the time for MyDynamicPodSwiftUIView.swift (unless a clean build is done, and that is the only dynamic pod in which previews are done, and previews are not done first for MyMainAppSwiftUIView.swift) with:
Xcode Previews rendering agent: Error Domain=com.apple.dt.UITestingAgent Code=-1 "failed to load library at path "
/DerivedData/MyPodTest-hcoikzfhpffncqcobtylgtykwvvb/Build/Intermediates.noindex/Previews/MyPodTest/Products/Debug-iphonesimulator/MyStaticPod/MyStaticPod.framework/MyStaticPod": Optional(dlopen(/DerivedData/MyPodTest-hcoikzfhpffncqcobtylgtykwvvb/Build/Intermediates.noindex/Previews/MyPodTest/Products/Debug-iphonesimulator/MyStaticPod/MyStaticPod.framework/MyStaticPod, 0): no suitable image foundor sometimes:
MyPodTest.app: Error Domain=com.apple.dt.UITestingAgent Code=-1 "Preview provider "9MyPodTest32MyDynamicPodSwiftUIView_PreviewsV" does not exist" UserInfo={NSLocalizedDescription=Preview provider "9MyPodTest32MyDynamicPodSwiftUIView_PreviewsV" does not exist}
It looks like Xcode internally uses preview-thunk.dylib for previews. It appears that the preview functionality is not working at present within CocoaPods for either static or dynamic frameworks however.
CocoaPods Environment
Installation Source
Plugins
Podfile
Project that demonstrates the issue
https://github.com/jayayres/cocoapodsswiftuipreviewbugreport.git
The text was updated successfully, but these errors were encountered: