Cocoapods: Resources installed using cocoapods not loaded for xctest bundle

Created on 12 Jan 2016  Â·  47Comments  Â·  Source: CocoaPods/CocoaPods

This is my output when trying to run the tests of this project (the separate-builds branch) for the PusherSwiftTests-OSX target (and scheme): https://github.com/pusher-community/pusher-websocket-swift/tree/separate-builds

2016-01-12 12:05:14.304 xctest[37573:1030574] The bundle “PusherSwiftTests” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.
2016-01-12 12:05:14.305 xctest[37573:1030574] (dlopen_preflight(/Users/Hami/Library/Developer/Xcode/DerivedData/PusherSwift-cifrlthdfybbcedmlylirqthvkky/Build/Products/Debug/PusherSwiftTests.xctest/Contents/MacOS/PusherSwiftTests): Library not loaded: @rpath/CryptoSwift.framework/Versions/A/CryptoSwift
  Referenced from: /Users/Hami/Library/Developer/Xcode/DerivedData/PusherSwift-cifrlthdfybbcedmlylirqthvkky/Build/Products/Debug/PusherSwiftTests.xctest/Contents/MacOS/PusherSwiftTests
  Reason: image not found)
Program ended with exit code: 82

I initially thought this was a problem with Nimble (see this issue: https://github.com/Quick/Nimble/issues/232) but it seems to actually be about the frameworks not being loaded for the xctest bundle.

Note that for the iOS and tvOS targets, this works fine, but for the OSX one it doesn't.

Let me know if I can provide any more info.

Thanks!

awaiting validation

Most helpful comment

For me I fixed it by setting Host Application to main target. (In Test Target -> `General)

All 47 comments

Seems like this is implicit dependencies failing because a scoped build output directory is generated for the OS X target. Might be something for @mrackwitz

I'm experiencing this same issue for Nimble, too, as somebody else linked above already. But I didn't use Cocoapods at but Carthage instead – so this is no issue specific to Cocoapods. Instead it's probably a weird XCode build setting.

Started to happen for me as well on Xcode 7.3 beta.

UPD: I found that the value of TARGET_BUILD_DIR is different on 7.3, it points to /build/path/AppName.app/PlugIns/AppNameTests.xctest, not to /build/path as before. But the value of CONFIGURATION_BUILD_DIR (this one is used by Copy Resource script) is not changed, that's why pod resources don't make it to proper test bundle (one located in PlugIns inside the app bundle, which is new in 7.3).

When I run:

xcodebuild -workspace PusherSwift.xcworkspace -scheme "PusherSwiftTests-OSX" -destination "platform=OS X,arch=x86_64" -sdk "macosx10.11" clean test -verbose

this is the end of the output, which is usually the output that occurs just before the tests run:

Touch /Users/Hami/Library/Developer/Xcode/DerivedData/PusherSwift-cifrlthdfybbcedmlylirqthvkky/Build/Products/Debug/PusherSwiftTests.xctest
    cd /Users/Hami/Pusher/pusher-websocket-swift
    /usr/bin/touch -c /Users/Hami/Library/Developer/Xcode/DerivedData/PusherSwift-cifrlthdfybbcedmlylirqthvkky/Build/Products/Debug/PusherSwiftTests.xctest

** TEST FAILED **

After trying seemingly every single combination of Runpath Search Paths it turned out that I needed to add @loader_path/../Frameworks in order for the error to disappear. Hopefully that helps others in future! Closing as it doesn't seem to be an issue with Cocoapods (unless this is something that Cocoapods sets?).

Looks like Cocoapods does set this: https://github.com/CocoaPods/CocoaPods/blob/b611ae93ba120edb5ce69d7797079a4f82245042/lib/cocoapods/generator/xcconfig/aggregate_xcconfig.rb#L187-L204

I'm not sure how this works but it looks like it just didn't choose the correct search paths given the platform and target.

I'm having the same issue with an OS X app. Adding the path like @hamchapman mentioned didn't help. Does anyone have a workaround?

There is a Cocoapods problem with Xcode 7.3 here that has to do with a change in location of the built xctest bundle, as @corristo mentioned above.

I've added the following build phase to the bottom of my test bundle build to work around it:

# Unfortuantely, Xcode 7.3 and Cocoapods are not yet fully compatible. The location of the xctest bundle has changed and so we need to manually copy some things ...
OLD_BUNDLE="${CONFIGURATION_BUILD_DIR}/${CONTENTS_FOLDER_PATH}"
NEW_BUNDLE="${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}"

FRAMEWORKS_PATH="Frameworks"

# For now, we only copy frameworks. This could be simply adapted to also copying Resources directories if needed
OLD_FRAMEWORKS_PATH="${OLD_BUNDLE}/${FRAMEWORKS_PATH}"
if [[ -d "${OLD_FRAMEWORKS_PATH}" ]]; then
    NEW_FRAMEWORKS_PATH="${NEW_BUNDLE}/${FRAMEWORKS_PATH}"
    echo "Copying frameworks from \"${OLD_FRAMEWORKS_PATH}\" to \"${NEW_FRAMEWORKS_PATH}\" ..."
    cp -R "${OLD_FRAMEWORKS_PATH}" "${NEW_FRAMEWORKS_PATH}"
fi

Seems like this will be a high-priority issue for Cocoapods to fix before Xcode 7.3 is final.

@mgorbach I also have a workaround, but on a Podfile level with the following post-instal hook:

installer.aggregate_targets.each do |target|
    # Without this hack resources from module tests won't make it to the unit test bundle
    # See https://github.com/CocoaPods/CocoaPods/issues/4752
    if target.name == "Pods-AwesomeAppTests" then
        %x~ sed -i '' 's/CONFIGURATION_BUILD_DIR/TARGET_BUILD_DIR/g' '#{target.support_files_dir}/#{target.name}-resources.sh' ~
    end
end

This ought to be fixed on master thanks to Marius' recent work.

hey @segiddins ,
Any update on this? just updated to 7.3 and we're getting hit with this now.

@segiddins Which changeset are you referring to when you say "fixed on master thanks to Marius' recent work"? Is this in a release that we can try? I can confirm this problem is still happening on 1.0.0.beta.6.

Scratch that. Deleting the DerivedData subdirectory entirely and rebuilding fixed my problem. Xcode apparently didn't copy the framework for my dependency correctly.

Looks like this has been fixed in the meantime :+1:

This issue is definitely still present on Xcode 7.3.1 and Cocoapods 1.0.1.
When creating an empty new OS X framework project, everything works and tests runs fine.
When installing a pod the test bundle is corrupted and doesn't run any more.

From my troubleshooting I have reason to believe that when hitting:

def generate_ld_runpath_search_paths
          ld_runpath_search_paths = ['$(inherited)']
          if target.platform.symbolic_name == :osx
            ld_runpath_search_paths << "'@executable_path/../Frameworks'"
            ld_runpath_search_paths << \
              if target.native_target.symbol_type == :unit_test_bundle
                "'@loader_path/../Frameworks'"
              else
                "'@loader_path/Frameworks'"
              end
          else
            ld_runpath_search_paths << [
              "'@executable_path/Frameworks'",
              "'@loader_path/Frameworks'",
            ]
          end
          @xcconfig.merge!('LD_RUNPATH_SEARCH_PATHS' => ld_runpath_search_paths.join(' '))
        end

if target.native_target.symbol_type == :unit_test_bundle fail (even though it's a unit test bundle), this is clearly visible because the run path search path are set to @loader_path/Frameworks rather than @loader_path/../Frameworks.

Overriding that value in the target test bundle, as suggested by @hamchapman, although it makes sense, unfortunately, it doesn't make any difference.

As already been discovered by others the issue seems that copying the frameworks inside the test bundle (to be self contained) causes the bundle to not be valid anymore, I am not sure this has been fixed but it is not applied because of if target.native_target.symbol_type == :unit_test_bundle failing.

Could someone from cocoapods confirm the behaviour of target.native_target.symbol_type? Where is that value set?

@neonichu, @segiddins

@valeriomazzeo it comes from the target's product UTI type

Guys this is very easy to reproduce, just create a new MacOSX framework project, cmd + U you can see it runs the tests just fine.

Create a Podfile, install any pod and you will see that tests won't run anymore.

This issue still exists with Xcode 7.3.1 and cocoapods 1.0.1
Would you please reopen?

So, is there some known work-around for this?

I can't repro this issue when adding a Pod to an OS X framework test target, however this does occur when adding a Pod to the framework target itself.

The reason for this is that we don't embed Pod frameworks into user framework targets (because that doesn't fly with iTC when submitting iOS apps) and therefore framework targets are currently unsupported by CocoaPods in general. There's some work in #5502 to change this for iOS, but I don't think that will change the situation for OS X. However, I think we _could_ embed dependent frameworks just fine on OS X, but that's not yet implemented.

Might not relate to the problem.

I use xcode 8.0 beta 2 (8S162m) and also faced the same problem, but I found problem for ios.
Anyway work around is go to the resulting .xctest and from there I copied all frameworks built but they are deep down in another directory for 1 level. So I went through it and copied them to be at the same level of .xctest. It works for me.

For me I fixed it by setting Host Application to main target. (In Test Target -> `General)

unfortunately you cannot do that on OSX Frameworks

do a import for WebKit on any files inside the target and it will test successfully. It happened on Xcode 7.3.1 and that was a rather hilarious fix.

So, are there any updates on the work-arounds? Is the only possible solution - to create a dummy app target and no longer test the framework itself?

Later that day we found out how to solve it. At least in our case. In the build settings, we set Embedded Content contains Swift Code to YES and it started working fine.

@superarts your solution worked for me, Thanks!

Hey!
I'm using Xcode 7.3.1 and CocoaPods 1.0.1

At my Project the test-flow works fine for my target 'projectTests' but at the test-flow for 'projectUITests' i get the same error like hamchapman.

Any ideas how i can resolve this?

@kandelvijaya it worked for me as well, thx

Updating to Xcode 8 (beta) and CocoaPods 1.1.0 (beta) resolved the issue for me.

I had similar issues, upgrading to CocoaPods 1.1.0.beta.1 solved my issue. Thanks, guys!

Still having these issues. Xcode 7.3.1, Cocoapods 0.39 as well as 1.0.1
Any update? Will updating to Xcode 8 help?

I found a workaround - Documented here.
http://iamsensoria.com/post/150887196416/cocoapods-and-xctest

basically a script to copy the right files in the right place

Hello guys,

Here is my (ugly?) solution to this:

  1. In your test target, add a run script at the end
  2. Put the following content as the script content

find ${TARGET_BUILD_DIR} -type d -name "*.framework" -exec cp -Rf {} ${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ \;

That's it. Any better solution is welcomed, but this one worked for me (Xcode 7.3 - cocoapods 1.1.0.rc.2)

Hope this helps!

This is a nasty one. @pierrekilly 's solution worked for me on Xcode 8 with Swift 3

Just set manually your Host Application
screen shot 2016-10-17 at 16 23 37

@pierrekilly 's script worked for me, Xcode 8 Swift 3.

I had to resort to the script @kywix pointed to on 1.2.0.beta.1. This got my UI tests running.

http://iamsensoria.com/post/150887196416/cocoapods-and-xctest

@pierrekilly's run script worked for me, Xcode 8.2 Swift 3 🙌

Just FYI for future people landing on this thread: I tried the things I found on this thread, and others, with Xcode 8.3b2 and CocoaPods 1.2, and nothing worked. Out of desperation, I recreated my UI test target, and it magically worked. So, if you have all the latest things, and nothing else has worked, maybe try that.

(I couldn't determine what the meaningful change between the old and new targets was. Too much pbxproj churn. Sadness.)

FYI for people landing here in future - the solution shown in this link works https://github.com/CocoaPods/CocoaPods/issues/5250

What worked for me (when copying files to the test target) is changing the pod file.

From:

platform :ios, '8.3'
use_frameworks!

target 'LogAnalytics_Example' do
  pod 'LogAnalytics', :path => '../'

  target 'LogAnalytics_Tests' do
    inherit! :search_paths


  end
end

to:

platform :ios, '8.3'
use_frameworks!

target 'LogAnalytics_Example' do
    pod 'LogAnalytics', :path => '../'

end

target 'LogAnalytics_Tests' do
    pod 'LogAnalytics', :path => '../'

end

That fixed it for me ~(and got rid of a warning regarding a framework directory that couldn't be found when running tests)~ (it didn't actually solve the warning).

@SpacyRicochet is LogAnalytics public podspec? Can you please provide a sample app so I can take a look on whats going on?

Sorry, it's not public. I can try to reproduce the problem, though.

Update: Reproduced! Writing it up now.

To reproduce I did the following actions (version 1.2.1);

  1. Create a new Xcode iOS Framework project.
  2. In the project, create one simple source file.
  3. Then in the same project, create one simple test that tests the source file.
  4. Run tests to make sure they work (probably not necessary, but why not).
  5. Create a new CocoaPods library with pod lib create (terminal log below).

    1. swift

    2. yes (demo app)

    3. none (no testing framework)

    4. no (no view-based testing)

  6. Copy the source file to the new library's Classes directory.
  7. Copy the source test to the new library's Example/Tests directory (make sure to change the import if necessary).
  8. Run tests. Result, bundle couldn't be loaded (debugger log below terminal log).
  9. For the fix;

    1. Change the Podfile as mentioned above.

    2. Do pod install in the Example directory.

    3. Run the tests. Now, they work! 🎉

The three Xcode project illustrating this issue can be found in this repository.

Hope this is useful!


Terminal log for pod creation

> pod lib create PodsUnitTestLib
Cloning `https://github.com/CocoaPods/pod-template.git` into `PodsUnitTestLib`.
Configuring PodsUnitTestLib template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide:
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and click links to open in a browser. )


What language do you want to use?? [ Swift / ObjC ]
 >
swift
Would you like to include a demo application with your library? [ Yes / No ]
 >
yes
Which testing frameworks will you use? [ Quick / None ]
 > none

Would you like to do view based testing? [ Yes / No ]
 > no
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `PodsUnitTestLib` from `../`
Downloading dependencies
Installing PodsUnitTestLib (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `PodsUnitTestLib.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Automatically assigning platform ios with version 8.3 on target PodsUnitTestLib_Example because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'PodsUnitTestLib/Example/PodsUnitTestLib.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
>

2017-05-31 19:28:49.390 xctest[82278:5818662] The bundle “PodsUnitTestLib_Tests” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle.
2017-05-31 19:28:49.390 xctest[82278:5818662] (dlopen_preflight(/Users/brunoscheele/Library/Developer/Xcode/DerivedData/PodsUnitTestLib-biynaejphnnyergoxlddsynvxyon/Build/Products/Debug-iphonesimulator/PodsUnitTestLib_Tests.xctest/PodsUnitTestLib_Tests): Library not loaded: @rpath/PodsUnitTestLib.framework/PodsUnitTestLib
  Referenced from: /Users/brunoscheele/Library/Developer/Xcode/DerivedData/PodsUnitTestLib-biynaejphnnyergoxlddsynvxyon/Build/Products/Debug-iphonesimulator/PodsUnitTestLib_Tests.xctest/PodsUnitTestLib_Tests
  Reason: image not found)
Program ended with exit code: 82

Thanks it is helpful

Thanks @SpacyRicochet that temporarily did it for me. Is there a proper fix for this yet?

Thank you @SpacyRicochet. Solved the issue. Happened on version 1.5.3 with AWSCore framework.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iosdev-republicofapps picture iosdev-republicofapps  Â·  3Comments

evermeer picture evermeer  Â·  3Comments

Mingmingmew picture Mingmingmew  Â·  3Comments

pronebird picture pronebird  Â·  3Comments

dawnnnnn picture dawnnnnn  Â·  3Comments