Skip to content
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

Test target does not build when RxCocoa Swift Package is used #2057

Closed
4 of 17 tasks
michallaskowski opened this issue Aug 30, 2019 · 44 comments
Closed
4 of 17 tasks

Test target does not build when RxCocoa Swift Package is used #2057

michallaskowski opened this issue Aug 30, 2019 · 44 comments

Comments

@michallaskowski
Copy link

Short description of the issue:

When RxCocoa is used in a project in Xcode 11, and it is imported as RxSwift package, unit tests for that project do not build, with following error:

missing required module 'RxCocoaRuntime'

I am not an expert on Swift Packages, just starting with it. I see RxCocoaRuntime is declared as a target in Package.swift, and RxCocoa depends on it. But it is not declared as a product.
This may be also Xcode's issue. But I don't have issues with other dependencies.

Expected outcome:

Tests build and are executed.

What actually happens:

Build fails.

Self contained code example that reproduces the issue:

Sample, minimal project: https://github.com/michallaskowski/RxCocoaTestsIssue

RxSwift/RxCocoa/RxBlocking/RxTest version/commit

Current master, commit 6b2a406

Platform/Environment

  • iOS
  • macOS
  • tvOS
  • watchOS
  • playgrounds

How easy is to reproduce? (chances of successful reproduce after running the self contained code)

  • easy, 100% repro
  • sometimes, 10%-100%
  • hard, 2% - 10%
  • extremely hard, %0 - 2%

Xcode version:

Xcode 11 beta 7, tested also with beta 6

⚠️ Fields below are optional for general issues or in case those questions aren't related to your issue, but filling them out will increase the chances of getting your issue resolved. ⚠️

Installation method:

  • CocoaPods
  • Carthage
  • Git submodules

other - Swift Packages :)

I have multiple versions of Xcode installed:
(so we can know if this is a potential cause of your issue)

  • yes (Xcode 10.1, Xcode 10.2.1)
  • no

Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)

  • just starting
  • I have a small code base
  • I have a significant code base
@michallaskowski
Copy link
Author

michallaskowski commented Sep 2, 2019

I created a fork of RxSwift, and declared RxCocoaRuntime as a product/library. Linking RxCocoaRuntime with test targets makes it possible to build the target. But tests do not get launched, it seems RxSwift symbols are included in both host app, and test target because RxCocoaRuntime depends on RxSwift. Then the process stops with EXC_BAD_ACCESS.

The same happens when RxCocoa is not imported and tests build, but RxTest or RxBlocking is linked with test target.
This may be Swift Packages issue.

@lijie121210
Copy link

Also troubled by this problem 💔

@mpawlo
Copy link

mpawlo commented Sep 11, 2019

Have you found any solution to this ? @michallaskowski

@laxmorek
Copy link

laxmorek commented Sep 16, 2019

Xcode 11 GM, problem still exists.

UPDATE:
with GM Seed 2 does not work too

@freak4pc
Copy link
Member

Happy to accept a PR around this if anyone's interested to investigate. I'll only have time in a a week or two.

@michallaskowski
Copy link
Author

I tried to reproduce duplicated symbols issue with a separate, small project. But SPM had no issues with it. I am not sure yet what triggers it for RxSwift, but also had not enough time to dig into it.

@andlang
Copy link

andlang commented Sep 20, 2019

I have the same problem with the SPM and XCode 11. I investigated a bit, but I couldn’t solve it.

I think, there are two problems: One with the RxCocoaRuntime, that is not found. And the other, that RxBlocking/RxTest depend also on RxSwift. Therefore the test target contains the RxSwift symbols twice and crashes.
At the moment, I use Carthage for my dependencies. For the test target I don’t link the binary with libraries, I only add the path to the frameworks build by Carthage to “Runpath Search Paths“ and “Framework Search Paths” in the build settings. I tried to add the paths, where SPM builds the dependencies, but this didn’t solve the problem. Probably, because SPM doesn’t build “.framework” files.

I think, the dependencies between the RxSwift targets need to be declared differently, that SPM works also for the test targets.

@FabianTerhorst
Copy link

I have same problem with XCode 11 with spm, but im only getting this error when including RxSwift in a iOS intents extension.

@airslider
Copy link

@lionhylra
Copy link

Same issue here with Xcode 11.0 (11A420a). Is anyone fixing it?

@yusuke024
Copy link

I'm not sure if it's the same case for everyone. But in my case, when I add "RxCocoa" to "Link Binary with Libraries" in the test target's Build Phase tab, it successfully built.

@achernoprudov
Copy link

I have found out that it is a known issue with rdar://problem/54587458.

Work around is to add flag to explicitly append modulemap to the OTHER_SWIFT_FLAGS in the test target:

-Xcc -fmodule-map-file=$(PROJECT_TEMP_ROOT)/GeneratedModuleMaps/iphonesimulator/RxCocoaRuntime.modulemap

@freak4pc
Copy link
Member

freak4pc commented Oct 4, 2019

@achernoprudov where did you find a reference to that radar ?

@achernoprudov
Copy link

@freak4pc I found it in swift-nio library. Issue-1228

@michallaskowski
Copy link
Author

I tested the changes, thanks for the info. While the tests build now, they are crashing on Xcodes 11.0, 11.1 GM, and 11.2 beta. On Xcode 11.1, swift_checkMetadataState throws EXC_BAD_ACCESS.
May be still an issue with Swift Packages, not RxSwift itself.

Sample project updated: https://github.com/michallaskowski/RxCocoaTestsIssue

@FabianTerhorst
Copy link

I had that crash as well and fixed it by not adding a swift package dependency that isn't a dynamic library multiple times on different targets.

@michallaskowski
Copy link
Author

michallaskowski commented Oct 9, 2019

Well, yes. Removing RxTest and RxBlocking from test target stops the crash. But also doesn't solve anything, if I want to test reactive parts of code. Unless you mean something else in this case, @FabianTerhorst ?

RxTest and RxBlocking both depend on RxSwift target, which is linked in the host application.
Because of that, test target seems to also link RxSwift.
Potential fix is linking RxTest and RxBlocking to the host application, but well, it is not a great solution.

@freak4pc
Copy link
Member

freak4pc commented Oct 9, 2019

I don't have time to test this but seems to me the best way would be adding the flags @achernoprudov mentioned above using SPM's unsafeFlag.

More info:

@AJBeltran3
Copy link

AJBeltran3 commented Oct 9, 2019

I was able to get my tests to run by setting up the flag @achernoprudov mentioned

-Xcc -fmodule-map-file=$(PROJECT_TEMP_ROOT)/GeneratedModuleMaps/iphonesimulator/RxCocoaRuntime.modulemap

Also by adding the 5 frameworks from RXSwift directly to my main target only and removing them all from my test target.

Main Target
Screen Shot 2019-10-09 at 11 25 18 AM

Test Target
Screen Shot 2019-10-09 at 11 24 58 AM

@iwheelbuy
Copy link

iwheelbuy commented Oct 10, 2019

May be still an issue with Swift Packages, not RxSwift itself.

I have the swift_checkMetadataState crash with only RxSwift dependency using CocoaPods in a sample project

Снимок экрана 2019-10-10 в 10 52 36

@achernoprudov
Copy link

@iwheelbuy you have crash because RxSwift is imported twice as static library. You have three solutions:

  1. Use Xcode11 and import it once via SPM.
  2. remove pod__Rx from TestTargetCrashTests.
  3. use flag use_frameworks! for Pods that makes RxSwift dynamic.

@iwheelbuy
Copy link

iwheelbuy commented Oct 17, 2019

@achernoprudov I'd love to go the 2nd way, but how do I use RxSwift in the test target?

If I try to import RxSwift:

import XCTest
@testable import TestTargetCrash
import RxSwift

I got Cannot load underlying module for 'RxSwift' error.

If I try not to import RxSwift and start using Observables:

_ = Observable<Int>.just(1)

I got the Use of unresolved identifier 'Observable' error

Edit:
I decided to remove Allow testing Host Application APIs flag. Now test target is an independent one and everything works as expected.

@bupojung
Copy link

May be still an issue with Swift Packages, not RxSwift itself.

I have the swift_checkMetadataState crash with only RxSwift dependency using CocoaPods in a sample project

Снимок экрана 2019-10-10 в 10 52 36

@iwheelbuy my project have the same problem. I download you sample code and remove Allow testing Host Application APIs flag but still crash. what can i do to fix it?

@freak4pc
Copy link
Member

freak4pc commented Jan 5, 2020

Can anyone confirm if this is working in Xcode 11.3 or its still a valid issue? Thanks !

@GabrielHauber
Copy link

Not working for me in Xcode 11.3.1 (that is, I cannot run tests, get the EXC_BAD_ACCESS error, when test target includes RxBlocking)

@kamitchell
Copy link

I think this is causing my test crashes in Xcode 11.3.1 as well, EXC_BAD_ACCESS and SIGABRT due to fatalError. XXX.app (51242) encountered an error (Early unexpected exit, operation never finished bootstrapping - no restart will be attempted. (Underlying error: Crash: XXX (51242): type metadata completion function for CombineLatestSink2_))

Also, lots of console messages like:

objc[66240]: Class _TtC7RxSwift28SerialDispatchQueueScheduler is implemented in both /Users/kevin/Library/Developer/CoreSimulator/Devices/526603CF-6176-4EE4-9FD0-E8C2C6A37A8A/data/Containers/Bundle/Application/DB37749E-5C18-4EEE-AF65-038E9424A9D4/App.app/App (0x104811758) and /Users/kevin/Library/Developer/Xcode/DerivedData/App-amlfdpqbdiuhnsdxncuoocmqdrwl/Build/Products/Debug-iphonesimulator/App.app/PlugIns/AppTests.xctest/AppTests (0x1062b8e28). One of the two will be used. Which one is undefined.

....which I suspect is the problem: RxSwift is static in both the test and the app.

@kamitchell
Copy link

Since my unit tests don't use the UI, I just made them logic tests for now, so they're isolated from the app and don't have duplicate symbols with it.

@freak4pc
Copy link
Member

Hey both. I'm sorry you're having this issue, but as far as I can tell, this is entirely an SPM problem (or it would happen in other package managers, or without one). I suggest filing a repoducible bug at bugs.swift.org and reference this thread, as well.

Thanks !

@kamitchell
Copy link

Thanks. I just set up an RxDataSources project to show the problem, and sent feedback to Apple, since for me, even if I say in a fork that RxSwift is dynamic, it's also including it as static.

@freak4pc
Copy link
Member

Thanks, mind sharing the link here?

@kamitchell
Copy link

@freak4pc
Copy link
Member

I suggest having one in bugs.swift.org specifically as it seems SPM-specific. Feedback would take longer to get noticed IMO

@igorkulman
Copy link

I was able to get my tests to run by setting up the flag @achernoprudov mentioned

-Xcc -fmodule-map-file=$(PROJECT_TEMP_ROOT)/GeneratedModuleMaps/iphonesimulator/RxCocoaRuntime.modulemap

Also by adding the 5 frameworks from RXSwift directly to my main target only and removing them all from my test target.

Main Target
Screen Shot 2019-10-09 at 11 25 18 AM

Test Target
Screen Shot 2019-10-09 at 11 24 58 AM

@AJBeltran3 how did you solve the main app crashing at startup with rxswift dyld: Library not loaded: @rpath/XCTest.framework/XCTest that happens when you include RXBlocking and RxTests to the main app?

groue added a commit to RxSwiftCommunity/RxGRDB that referenced this issue Apr 4, 2020
Had to fix "missing required module 'RxCocoaRuntime'" error with ReactiveX/RxSwift#2057 (comment)
@psi-gh
Copy link

psi-gh commented Apr 10, 2020

@yusuke024

I'm not sure if it's the same case for everyone. But in my case, when I add "RxCocoa" to "Link Binary with Libraries" in the test target's Build Phase tab, it successfully built.

I'm here from google, tried to resolve another, but a similar bug. When I use Swift Package Manager and set "Host Application" to None for test target's settings, test target stops building with linking issues of SPM's dependencies. So I need to explicitly add those dependencies to test target's both Dependencies and Link Binary With Libraries sections in Build Phases tab. Thanks for suggestion!

Screenshot 2020-04-10 at 19 01 04

@kean
Copy link

kean commented Jun 11, 2020

I installed RxCocoa using Carthage in an empty iOS app target, and I see the same issue when I try to import RxCocoa: Missing required module 'RxCocoaRuntime'.

Xcode 11.5. Carthage 0.34 (latest). RxSwift 5.1.1.

Update: "Clean Build" helped. I initially installed the dependencies using SPM, and then replaced them with frameworks built by Carthage. Somehow, Xcode got confused.

@freak4pc
Copy link
Member

Uhm, I just tried it in Carthage and it worked fine. Could you provide a repro project? @kean

@freak4pc
Copy link
Member

Saw your comment. Thanks for the update. I'll be closing this for now.
IRT SPM specifically, see #2127 (comment)

@RinatAbidullin
Copy link

The path to modulemap file has changed in Xcode 12, the OTHER_SWIFT_FLAGS flag should be like this:
-fmodule-map-file=$(PROJECT_TEMP_ROOT)/GeneratedModuleMaps-iphonesimulator/RxCocoaRuntime.modulemap

@freak4pc
Copy link
Member

@RinatAbidullin Are you positive? If yes, it's worth filing an SPM bug.

@GalCohen
Copy link

Hi all for those of us just joining the party, what's the latest solution to this? i see some compiler flag (that maybe doesn't work?), I see a screenshot recommending adding the dependency manually to the test target, etc. Is this documented somewhere? Thanks!

@freak4pc
Copy link
Member

Hey Gal 👋
There was a suggestion here but I haven't tested it personally. I've been hopeful Apple will fix up their mess but this doesn't seem to be the case.

#2127 (comment)

@bpollman
Copy link

The path to modulemap file has changed in Xcode 12, the OTHER_SWIFT_FLAGS flag should be like this:
-fmodule-map-file=$(PROJECT_TEMP_ROOT)/GeneratedModuleMaps-iphonesimulator/RxCocoaRuntime.modulemap

Depending on what you are building for (simulator, device, archive build) the modulemap path will reference the iphoneos platform and not iphonesimulator. Below is the flag I am using so the path is referencing the current platform:

-fmodule-map-file=$(PROJECT_TEMP_ROOT)/GeneratedModuleMaps-$(PLATFORM_NAME)/RxCocoaRuntime.modulemap

@sulaimankhan7
Copy link

Not working in Xcode Version 14.2 (14C18)

@rbmutai
Copy link

rbmutai commented Dec 18, 2023

@yusuke024

I'm not sure if it's the same case for everyone. But in my case, when I add "RxCocoa" to "Link Binary with Libraries" in the test target's Build Phase tab, it successfully built.

I'm here from google, tried to resolve another, but a similar bug. When I use Swift Package Manager and set "Host Application" to None for test target's settings, test target stops building with linking issues of SPM's dependencies. So I need to explicitly add those dependencies to test target's both Dependencies and Link Binary With Libraries sections in Build Phases tab. Thanks for suggestion!

Screenshot 2020-04-10 at 19 01 04

This worked for me as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests