Cocoapods: Bug with Unit Tests on Mojave: "The bundle couldn’t be loaded because it is damaged or missing necessary resources"

Created on 27 Sep 2018  Â·  35Comments  Â·  Source: CocoaPods/CocoaPods

Report

Since I upgraded to macOS Mojave, I can't run unit test of modules including cocoapods. Please check the sample app to understand/reproduce.

What did you do?

  • Download sample project at https://cl.ly/990d99adec49
  • Unzip
  • In CLI, go to the project directory and run pod install
  • Open the UnitTestIssue.xcworkspace file with Xcode 10 on macOS Mojave
  • Run unit tests on App scheme

ℹ The sample project is a workspace composed by an iOS application target (App) and a framework (AppModule) imported by the app. Both target includes RxSwift (as example) through CocoaPods.

What did you expect to happen?

âś… AppTests is executed and all its tests correctly pass
âś… AppModuleTests is executed and all its tests correctly pass

What happened instead?

âś… AppTests is executed and all its tests correctly pass
❌ AppModuleTests is not executed and the console displays the following error:

2018-09-27 16:49:34.580550+0200 xctest[3033:761430] The bundle “AppModuleTests” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.
2018-09-27 16:49:34.580849+0200 xctest[3033:761430] (dlopen_preflight(/Users/jeromealves/Library/Developer/Xcode/DerivedData/UnitTestIssue-akzscdcjjahzpmdxkzgmyhdxntrl/Build/Products/Debug-iphonesimulator/AppModuleTests.xctest/AppModuleTests): Library not loaded: @rpath/RxSwift.framework/RxSwift
  Referenced from: /Users/jeromealves/Library/Developer/Xcode/DerivedData/UnitTestIssue-akzscdcjjahzpmdxkzgmyhdxntrl/Build/Products/Debug-iphonesimulator/AppModuleTests.xctest/AppModuleTests
  Reason: image not found)
Program ended with exit code: 82

CocoaPods Environment

Stack

   CocoaPods : 1.6.0.beta.1
        Ruby : ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
    RubyGems : 2.5.2.3
        Host : Mac OS X 10.14 (18A391)
       Xcode : 10.0 (10A254a)
         Git : git version 2.15.1
Ruby lib dir : /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib
Repositories : master - https://github.com/CocoaPods/Specs.git @ 69dd9cd4156aa6343da3ad63c150897bc30fa7da

Installation Source

Executable Path: /usr/local/bin/pod

Plugins

cocoapods-deintegrate : 1.0.2
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.0
cocoapods-stats       : 1.0.0
cocoapods-trunk       : 1.3.1
cocoapods-try         : 1.1.0

Podfile

source 'http://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'

inhibit_all_warnings!
use_frameworks!

workspace 'UnitTestIssue.xcworkspace'

def podsRx
    pod 'RxSwift', '~> 4.0'
end

target :AppModule do
    project 'AppModule/AppModule.xcodeproj'
    podsRx
    target :AppModuleTests do
        inherit! :search_paths
    end
end


target :App do
    project 'App/App.xcodeproj'
    podsRx
    target :AppTests do
        inherit! :search_paths
    end
end


post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
    end
end

Project that demonstrates the issue

Notes

  • In sample project podfile, comment line podsRx under target :AppModule do
  • run pod install again
  • run unit tests again
    → Now everything works fine and all tests are executed. This makes me thank that the issue is somewhat related to CocoaPods.
workaround available defect

Most helpful comment

i was able to workaround this by reverting to the legacy build system for now, to get the UI tests running again. This might not be desirable if you need some feature from the new build system.

You can switch it by going to File > Workspace Settings... and under Shared Workspace Settings set Build System to Legacy Build System. There are per-user settings there but I haven't messed with those yet.

All 35 comments

The problem is that target :AppModuleTests do do not have an app host and you are using inherit! :search_paths. This means that this target would find the dependencies to load them from the host but in this case there is none.

Change to:

target :AppModule do
    project 'AppModule/AppModule.xcodeproj'
    podsRx
    target :AppModuleTests
end

Worked for me.

@jegnux I closed this but I _will_ re-open it if we can find an issue with CocoaPods so far this seems to be a configuration issue.

@dnkoutso I wish it worked but it didn't :(

New Sample Project with podfile changes suggestion

https://cl.ly/4dd8ff4c3a36

Error

2018-09-27 18:19:44.953526+0200 xctest[5235:832167] The bundle “AppModuleTests” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.
2018-09-27 18:19:44.953812+0200 xctest[5235:832167] (dlopen_preflight(/Users/jeromealves/Library/Developer/Xcode/DerivedData/UnitTestIssue-akzscdcjjahzpmdxkzgmyhdxntrl/Build/Products/Debug-iphonesimulator/AppModuleTests.xctest/AppModuleTests): Library not loaded: @rpath/libswiftSwiftOnoneSupport.dylib
  Referenced from: /Users/jeromealves/Library/Developer/Xcode/DerivedData/UnitTestIssue-akzscdcjjahzpmdxkzgmyhdxntrl/Build/Products/Debug-iphonesimulator/AppModuleTests.xctest/Frameworks/RxSwift.framework/RxSwift
  Reason: image not found)
Program ended with exit code: 82

CocoaPods Environment

Stack

   CocoaPods : 1.6.0.beta.1
        Ruby : ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
    RubyGems : 2.5.2.3
        Host : Mac OS X 10.14 (18A391)
       Xcode : 10.0 (10A254a)
         Git : git version 2.15.1
Ruby lib dir : /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib
Repositories : master - https://github.com/CocoaPods/Specs.git @ 69dd9cd4156aa6343da3ad63c150897bc30fa7da

Installation Source

Executable Path: /usr/local/bin/pod

Plugins

cocoapods-deintegrate : 1.0.2
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.0
cocoapods-stats       : 1.0.0
cocoapods-trunk       : 1.3.1
cocoapods-try         : 1.1.0

Podfile

source 'http://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'

inhibit_all_warnings!
use_frameworks!

workspace 'UnitTestIssue.xcworkspace'

def podsRx
    pod 'RxSwift', '~> 4.0'
end

target :AppModule do
    project 'AppModule/AppModule.xcodeproj'
    podsRx
    target :AppModuleTests
end


target :App do
    project 'App/App.xcodeproj'
    podsRx
    target :AppTests
end


post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
    end
end

Sorry I did not get this error. I got the tests to run. Best advise is to post this in StackOverflow under cocoapods tag.

@dnkoutso are you on macOS Mojave? I think it works well on High Sierra.

@dnkoutso when you say that tests run, you mean all tests?

EXPECTED RESULTS | ACTUAL RESULTS
--------|---------
capture d ecran 2018-09-27 a 18 27 44 | capture d ecran 2018-09-27 a 18 27 04

Setting ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES to YES in post_install fixes the issue.

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES'] = 'YES'
        end
    end
end

I had the same issue.
On Mojave I had to set ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES in post-install hook.
I had to make sure _not_ to set this setting for resource bundle targets though.
Since resources bundle targets are usually named as Target-Target I've used regex check like this:

      unless target.name =~ /^(.+)-\1/
        config.build_settings["ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES"] = "YES"
      end

Depending on the naming pattern may be a bit hacky, but works nonetheless.

This fix works for Podfile both with and without redundant inherit! :search_paths.
Just removing unneeded inherit! :search_paths doesn't fix the issue, it only changes the error message. Instead of failing to link agains concrete pod, it gives me stuff like libswiftSwiftOnoneSupport - the weirdest thing where optimisation flag gets injected in the middle of libswiftSwiftSupport somehow.

The "workaround" I posted before didn't work out so good.
As a result each and every framework has a full copy of Swift standard library it its Frameworks folder, which exploded the size of the Archive to around 5Gb in out case.

@mgrebenets we set ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES to YES only in Debug mode, and that helps the app size not get bloated. Our workaround is

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            # This works around a unit test issue introduced in Xcode 10.
            # We only apply it to the Debug configuration to avoid bloating the app size
            if config.name == "Debug" && defined?(target.product_type) && target.product_type == "com.apple.product-type.framework"
                config.build_settings['ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES'] = "YES"
            end
        end
    end
end

I also submitted the PR referenced above to have CocoaPods do this for us

@icecrystal23
Thanks a ton! That definitely does the trick 👍

We ended up with ugly fix until I saw your comment.
Since the first framework reporting dylib issues was Alamofire, we enabled this flag for Alamofire only. Magically this seemed to have fixed issues with all our unit tests. Most likely because Alamofire is used by all targets in our case. Still feels weird, why enabling this build setting for just one framework, fixes the issue for all the other pods.

I got hit by this issue after upgrading my build machine to Mojave (and Xcode 10). Everything runs fine locally, for all our developers, however.

I think the suggested edits resolved the issue, however, I'm not sure I actually understand the root cause. Should I create a separate issue with my configuration and errors?

My understanding is the root cause is a bug in Xcode 10 where it fails to copy some Swift libraries needed by embedded frameworks. This only seems to happen for unit test targets. Apple claims it doesn't happen if all frameworks and the app are using Swift version 4.2, but I haven't verified that myself since that solution doesn't work for us right now anyway (we use some third party code that isn't on Swift 4.2 yet).

i was able to workaround this by reverting to the legacy build system for now, to get the UI tests running again. This might not be desirable if you need some feature from the new build system.

You can switch it by going to File > Workspace Settings... and under Shared Workspace Settings set Build System to Legacy Build System. There are per-user settings there but I haven't messed with those yet.

Just a heads-up that I'm seeing this issue on High Sierra too.

I'm also seeing this on High Sierra with Xcode 10. Switching to legacy build system (as outlined above) fixed the issue, but it would be great to not have to resort to that.

Just to add a data point: Using the new build system, I do not hit the failure when recording UI test actions, but if I try to just run the same UI test target/class/testMethod, it occurs. I can alternate back and forth between these two execution modes without cleaning build folders and get consistent results.

I tried to diff the derived data directories after building for each mode but couldn't find meaningful differences (which doesn't mean there aren't any; lots of binary files differed, I just don't know enough to inspect them all).

I've been seeing very similar issues (XCode 10.0 & 10.1; macOS 10.13.6; CocoaPods 1.6.0.beta.2).

Above given post-install hook did _not_ solve the issue. In fact, I'm seeing

ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES

in Pods/Targets Support Files/Pods-TestTarget/Pods-TestTarget.debug.xcconfig so I'm not sure what it does effect.

What _did_ work was removing nested blocks from Podfile. Bug?

This is duplicate of which appears to be fixed in Xcode 10.2 Beta. Closing this as a dup and use https://github.com/CocoaPods/CocoaPods/issues/8318 to 100% verify my claim is true.

Verified Xcode 10.2 Beta works.

@dnkoutso I'm still seeing this.

Stack

   CocoaPods : 1.6.1
        Ruby : ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
    RubyGems : 3.0.3
        Host : Mac OS X 10.14.3 (18D109)
       Xcode : 10.2.1 (10E1001)
         Git : git version 2.20.1 (Apple Git-117)
Ruby lib dir : /usr/local/Cellar/ruby/2.6.3/lib
Repositories : master - https://github.com/CocoaPods/Specs.git @ 4e26bd0562d076c7633d95a83902d4fc2a2be5b5
               topit-cocoapods-spec - ssh://[email protected]:33022/cocoapods-spec.git @ 65440ad418c2adbc0de98e4772787b00c1fabbc8

Installation Source

Executable Path: /usr/local/lib/ruby/gems/2.6.0/bin/pod

Plugins

cocoapods-deintegrate : 1.0.4
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.0
cocoapods-stats       : 1.1.0
cocoapods-trunk       : 1.3.1
cocoapods-try         : 1.1.0

With test target nested into main target:

xctest (10137) encountered an error (Failed to load the test bundle. (Underlying error: The bundle “$tests” couldn’t be loaded because it is damaged or missing necessary resources. The bundle is damaged or missing necessary resources. dlopen_preflight(/Users/raphael/Library/Developer/Xcode/DerivedData/$main-atakmbfbssklqyhbldovylqqkuec/Build/Products/Debug/$tests.xctest/Contents/MacOS/$tests): Library not loaded: @rpath/CocoaLumberjack.framework/Versions/A/CocoaLumberjack
  Referenced from: /Users/raphael/Library/Developer/Xcode/DerivedData/$main-atakmbfbssklqyhbldovylqqkuec/Build/Products/Debug/$tests.xctest/Contents/MacOS/$tests
  Reason: image not found))

Tests run fine when the test target is declared separately.

I'm also still seeing this, but only when running the UI tests on iOS versions below 12.2. Applying the post_install script to set ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES to true did the trick here.

I am also experiencing this issue with Cocoapods 1.6.1 using Xcode 10.2.1.
Switching back to Cocoapods 1.5.3 fixes the issue.

I also faced with this issue with CocoaPods 1.5.3 and Xcode 10.2.1
Reason was in installed flag MACH_O_TYPE=staticlib for test target in build settings when it should be a mh_bundle
I've fixed it by the following hook

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if target.name.include? "Test"
            target.build_configurations.each do |config|
                config.build_settings['MACH_O_TYPE'] = "mh_bundle"
            end
        end
    end
end

I'm seeing this again with Xcode 11 beta 4

Please upload a sample app then and we can see if we re-open this.

I'm seeing this again with Xcode 11 beta 5

Seeing this as well, both XCode 10 and 11 beta 5, Mojave, new build system, nested targets in the podfile, neither my code nor dependency's using Swift. What helped is to substitute inherit! :search_paths for inherit! :complete for the UI tests target only so that it looks like
````
target 'MyProject' do
project 'MyProject.xcodeproj'
sharedLibraries

target 'MyProjectTests' do
inherit! :search_paths
end

target 'MyProjectUITests' do
inherit! :complete
end
end
````
As a result the Runpath Search Paths build setting of the UI tests target changed slightly and started to inherit from pods Config.File. From now everything works. Hope it helps, maybe it's not about Swift.

Does anyone else following have a sample app for the CocoaPods guys? I am reproducing it in proprietary code. The old sample app attached to this issue isn't reproducing it.

I ran into the same issue after updating from Xcode 10.x to Xcode 11.0. Nothing else was changed.

Test project for XCode 11: https://github.com/booiiing/FrameworkTestDemo

  • New Framework project generated by XCode
  • Podfile generated by pod init

To get the error:

  • pod install
  • XCode -> Product -> Test

OK, in my case, to reproduce the problem I had pretty much the same setup as @booiiing had, except I had platform :ios, '11.0' and RxTest dependency for the test target.

What I found out is that the error only comes up when running on emulator with iOS 11.4. When running with 13.0, the tests actually work.

Update:

  1. The solution by @AndrewMcDrew works for me now - I updated the demo project in a separate commit for that.
  2. The production project where I ran into this has iOS 10 as target, so I am close to @NeverwinterMoon. I also updated the demo project for that. Without 1., though, I get the error for iOS 11 and 13 on simulators (using XCode GM).

I still have to do some more testing - I get the impression that the results are not completely reproducible.

I have this issue on podspec validation. What helped was complete deletion of all simulators/runtimes. I left only sims for last iOS runtime.

Was this page helpful?
0 / 5 - 0 ratings