Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MacOS Swift Framework testing fail

I have a framework written in obj-c and swift.

Now i try to run a related unit test target, but I get this error:

2014-07-10 07:45:54.064 xctest[4908:303] The test bundle at /Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest could not be loaded because an unanticipated error occurred: Error Domain=NSCocoaErrorDomain Code=3587 "The bundle “SOGraphDB-Mac Tests” couldn’t be loaded because it is damaged or missing necessary resources." (dlopen_preflight(/Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest/Contents/MacOS/SOGraphDB-Mac Tests): Library not loaded: @rpath/libswiftAppKit.dylib
  Referenced from: /Users/steve/Temporary/Build/Products/Debug/SOGraphDB.framework/Versions/A/SOGraphDB
  Reason: image not found) UserInfo=0x10011c640 {NSLocalizedFailureReason=The bundle is damaged or missing necessary resources., NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSFilePath=/Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest/Contents/MacOS/SOGraphDB-Mac Tests, NSDebugDescription=dlopen_preflight(/Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest/Contents/MacOS/SOGraphDB-Mac Tests): Library not loaded: @rpath/libswiftAppKit.dylib
  Referenced from: /Users/steve/Temporary/Build/Products/Debug/SOGraphDB.framework/Versions/A/SOGraphDB
  Reason: image not found, NSBundlePath=/Users/steve/Temporary/Build/Products/Debug/SOGraphDB-Mac Tests.xctest, NSLocalizedDescription=The bundle “SOGraphDB-Mac Tests” couldn’t be loaded because it is damaged or missing necessary resources.}

Any idea what the root cause can be? The error seems realated to "Library not loaded: @rpath/libswiftAppKit.dylib"

Both (framework and test bundle) compile without any error or warning (in Beta2)

like image 498
Stephan Avatar asked Jul 10 '14 05:07

Stephan


2 Answers

I had a similar problem, though mine was an iOS test target, linking to a Swift framework, that failed to run on Xcode 6 GM. The test target had run successfully on an early beta of Xcode 6, but the final version reported the runtime error: Library not loaded: @rpath/libswiftCore.dylib

I noticed that a newer project did not have the failure, so I compared the build settings and test code. I was able to resolve the problem with three steps:

  1. The test target needs the "Embedded Target Contains Swift Code" setting to be YES. This tells the linker to add the Swift runtime libraries to the executable.

  2. The test target needs an explicit value for the "Runpath Search Paths" build setting. This tells the loader where to find the Swift runtime libaries. I copied the following setting from a fresh new test target:

    LD_RUNPATH_SEARCH_PATHS = $(inherited) @executable_path/../Frameworks @loader_path/../Frameworks
    
  3. The test cases need to explicitly import any modules that are used by the linked framework. In my project, the framework used UIKit but the test cases only used my framework. When I added an explicit import UIKit to the test cases, the link problem went away.

like image 176
Jay Lieske Avatar answered Nov 15 '22 06:11

Jay Lieske


Since I keep running into this issue whenever I mess with build settings, here's the cleanest answer I can provide as of Xcode 8b5:

If unit tests don't run on iOS, make sure you have:

Runpath Search Paths: @loader_path/Frameworks

If unit tests don't run on macOS, make sure you have:

Runpath Search Paths: @loader_path/../Frameworks

This will show up as LD_RUNPATH_SEARCH_PATHS in your pbxproj file. You can also add $(inherited) to make sure project-wide paths are added as well, but those are probably empty.

Lastly, I didn't need the executable_path/... settings, doesn't make a difference for me whether they're there or not for unit tests.

like image 35
Pascal Avatar answered Nov 15 '22 07:11

Pascal