Skip to content

Instantly share code, notes, and snippets.

@liamnichols
Last active April 14, 2020 13:35
Show Gist options
  • Save liamnichols/3507faf70ecca201cb4300645caa9b74 to your computer and use it in GitHub Desktop.
Save liamnichols/3507faf70ecca201cb4300645caa9b74 to your computer and use it in GitHub Desktop.
Fix for missing libswiftXCTest.dylib when trying to run Unit Tests in Xcode 11.1 on an iOS 11 simulator
sudo mkdir /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS\ 11.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift
sudo ln -s /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS\ 11.4.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/Swift/libswiftXCTest.dylib /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS\ 11.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift
@liamnichols
Copy link
Author

When running unit tests on an iOS 11 simulator in Xcode 11.1, I was getting the following:

Testing failed:
	GlobalTests:
		Global.app (50625) encountered an error (Failed to load the test bundle. (Underlying error: The bundle “GlobalTests.xctest” couldn’t be loaded because it is damaged or missing necessary resources. The bundle is damaged or missing necessary resources. dlopen_preflight(/opt/j/workspace/ios-global-pull-request/derivedData/Build/Products/Debug-iphonesimulator/Global.app/PlugIns/GlobalTests.xctest/GlobalTests): Library not loaded: /usr/lib/swift/libswiftXCTest.dylib
  Referenced from: /opt/j/workspace/ios-global-pull-request/derivedData/Build/Products/Debug-iphonesimulator/Global.app/PlugIns/GlobalTests.xctest/Frameworks/RxTest.framework/RxTest
  Reason: no suitable image found.  Did find:
	/usr/lib/swift/libswiftXCTest.dylib: mach-o, but not built for iOS simulator))
	PersistenceTests:
		xctest (50628) encountered an error (Failed to load the test bundle. (Underlying error: The bundle “PersistenceTests” couldn’t be loaded because it is damaged or missing necessary resources. The bundle is damaged or missing necessary resources. dlopen_preflight(/opt/j/workspace/ios-global-pull-request/derivedData/Build/Products/Debug-iphonesimulator/PersistenceTests.xctest/PersistenceTests): Library not loaded: /usr/lib/swift/libswiftXCTest.dylib
  Referenced from: /opt/j/workspace/ios-global-pull-request/derivedData/Build/Products/Debug-iphonesimulator/PersistenceTests.xctest/Frameworks/RxTest.framework/RxTest
  Reason: no suitable image found.  Did find:
	/usr/lib/swift/libswiftXCTest.dylib: mach-o, but not built for iOS simulator))

** TEST FAILED **
ERROR [2019-10-09 01:06:25.05]: Exit status: 65

Not sure why exactly, but for some reason libswiftXCTest.dylib isn't correctly located in the iOS 11.4 Simulator Runtime and as a result the test bundle wouldn't load. I guess it could be a mixture of Swift ABI related things and a bug in Xcode 11 but by linking the framework into the correct location the bundle can run now so its good enough for me

@muehlemann
Copy link

This is awesome! I was stuck on this issue for weeks and creating the symbolic link fixed it for me. Thank you!

@schroepf
Copy link

schroepf commented Nov 12, 2019

OMG! Thanks!

@liamnichols
Copy link
Author

@muehlemann: Glad it helped! I haven't tried it, but I also spotted this comment which might be the real "fix":

CocoaPods/CocoaPods#9165 (comment)

@muehlemann
Copy link

@liamnichols Great thanks for the info, will be watching that cocoapods issue.

@1oo7
Copy link

1oo7 commented Jan 13, 2020

@muehlemann: Glad it helped! I haven't tried it, but I also spotted this comment which might be the real "fix":

CocoaPods/CocoaPods#9165 (comment)

This cocoapods "fix" doesn't really help because it means you have to modify the framework's .xcodeproj and then rebuild the framework. If that framework is a third-party dependency that you're including via CocoaPods or Carthage, then actually the kind of solution you're suggesting is a much better idea.

But one thing sticks out from Apple's suggested fix mentioned on that thread: they are saying that libswiftXCTest.dylib is obsolete and has been replaced by libXCTestSwiftSupport.dylib, which "should" have all the same symbols in it. So, if you want to use this newer .dylib, then you would want to set up your symlinks like this:

sudo mkdir /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS\ 11.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift
sudo ln -s /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS\ 11.4.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/Swift/libswiftXCTest.dylib /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS\ 11.4.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift 

Personally I think this is due to a bug in XCode... it should be able to find that .dylib. But Apple has created a literal hell of duplicated directory trees throughout their development apps like XCode and Simulator. It's gotten so out of control that it's no wonder this kind of problem is happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment