Why Xcode skips libswiftCompatibilitySpan.dylib on a newer simulator
I am writing this down after chasing a launch crash on an iOS 18.6 simulator in a project that targets iOS 18 and depends on swift-collections (OrderedCollections uses OutputSpan), because most advice you find online points at the wrong build settings. The app compiled cleanly and launched fine on iOS 26.5, then died at startup in dyld on iOS 18.6 with Library not loaded: @rpath/libswiftCompatibilitySpan.dylib because one of the bundle's binaries carried a non-weak load command for it while Frameworks/libswiftCompatibilitySpan.dylib had not been copied into the .app.
Why embedding flags are not the problem
Most answers for a missing Swift compatibility library tell you to set ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES, or in newer Xcode releases to set ENABLE_DEBUG_DYLIB = NO so the executable stays one binary instead of splitting into a stub and a *.debug.dylib. Neither of those caused the missing library here, and I noticed that early because the Mac Catalyst Debug build in the same project had ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = NO (which only controls whether a target with no Swift code still embeds Swift libraries) and ENABLE_DEBUG_DYLIB = YES (where CopySwiftLibs inspects EXECUTABLE_DEBUG_DYLIB_PATH as well), yet still bundled libswiftCompatibilitySpan.dylib.
What decides whether CopySwiftLibs passes --back-deploy-swift-span is inside Xcode's build system (SWBCore and SWBTaskConstruction). In the newer iOS SDK, SDKSettings.json sets SwiftSpanMinimumDeploymentTarget to "26.0", meaning OS releases from 26.0 onward include Swift Span in the system runtime and do not need the compatibility dylib shipped inside the .app. When SWBTaskConstruction sets up the CopySwiftLibs invocation, it calls SWBCore.Platform.supportsSwiftSpanNatively(_:forceNextMajorVersion:considerTargetDeviceOSVersion:) with forceNextMajorVersion: false and considerTargetDeviceOSVersion: true. Because considerTargetDeviceOSVersion is true, Xcode reads TARGET_DEVICE_OS_VERSION ahead of IPHONEOS_DEPLOYMENT_TARGET, so when you pass a concrete simulator destination that property beats your deployment target.
You can see the resolved settings directly with xcodebuild -showBuildSettings by comparing a concrete simulator destination against a generic one:
xcodebuild -project MyApp.xcodeproj -scheme MyApp -showBuildSettings \ -destination "platform=iOS Simulator,name=iPhone 17" \ | grep -E 'TARGET_DEVICE_OS_VERSION|IPHONEOS_DEPLOYMENT_TARGET' In the build script I was using, passing -destination "platform=iOS Simulator,name=iPhone 17" set TARGET_DEVICE_OS_VERSION to 27.2 from that simulator's runtime while IPHONEOS_DEPLOYMENT_TARGET remained 18.0. Because 27.2 >= 26.0, CopySwiftLibs skipped --back-deploy-swift-span and left libswiftCompatibilitySpan.dylib out of Frameworks/ even though OrderedCollections still linked @rpath/libswiftCompatibilitySpan.dylib, which worked on iOS 26.5 and failed on iOS 18.6.
Switching to a generic simulator destination
If we switch -destination to generic/platform=iOS Simulator, TARGET_DEVICE_OS_VERSION is left unset, so CopySwiftLibs falls back to evaluating IPHONEOS_DEPLOYMENT_TARGET (18.0 < 26.0), passes --back-deploy-swift-span, and embeds libswiftCompatibilitySpan.dylib into MyApp.app/Frameworks/ for both Debug and Release builds:
xcodebuild \ -project MyApp.xcodeproj \ -scheme MyApp \ -configuration Debug \ -destination "generic/platform=iOS Simulator" \ EXCLUDED_ARCHS=x86_64 \ build Adding EXCLUDED_ARCHS=x86_64 on the xcodebuild command line is needed if your app links a binary framework or static library whose simulator slice only contains arm64, such as a Rust or C++ FFI xcframework. With a concrete simulator destination, non-Debug builds could rely on passing ONLY_ACTIVE_ARCH=YES on the command line so Swift package targets would skip building x86_64 and match the link slice, whereas a generic destination has no active host architecture to narrow and SwiftPM package targets ignore project-level EXCLUDED_ARCHS[sdk=iphonesimulator*] settings in your .xcodeproj. You just want to scope EXCLUDED_ARCHS=x86_64 to simulator builds and leave it off Mac Catalyst or device invocations, because Mac Catalyst builds may still link a fat arm64 plus x86_64 archive.
Using
generic/platform=iOS Simulatorties Swift compatibility library back-deployment to your configured deployment target rather than whichever simulator runtime happens to be named in the script.
Checking the built bundle
Before installing or archiving a build, running otool -L only on the top-level executable misses load commands inside MyApp.debug.dylib (when ENABLE_DEBUG_DYLIB is on) as well as embedded frameworks and extensions. Walking every non-symlink Mach-O binary in the .app catches all of them in one pass:
import os, subprocess, sys app = sys.argv[1] machos = [ path for root, _, files in os.walk(app) for name in files for path in (os.path.join(root, name),) if os.path.isfile(path) and not os.path.islink(path) and "Mach-O" in subprocess.check_output(["file", "-b", "--", path], text=True, errors="replace") ] linked = [ path for path in machos if "@rpath/libswiftCompatibilitySpan.dylib" in subprocess.check_output(["otool", "-L", path], text=True, errors="replace") ] embedded = any( os.path.isfile(os.path.join(app, rel)) for rel in ( "Frameworks/libswiftCompatibilitySpan.dylib", "Contents/Frameworks/libswiftCompatibilitySpan.dylib", ) ) missing = int((not machos) or (bool(linked) and not embedded)) print(f"machos={len(machos)} span_linked={len(linked)} span_embedded={int(embedded)} missing={missing}") sys.exit(missing) Checking both Frameworks/ and Contents/Frameworks/ lets you run the same check against iOS and Mac Catalyst bundles alike.
When you invoke xcodebuild from a script and need your app to launch on older OS releases down to your deployment target, avoid passing a concrete newer simulator in -destination so CopySwiftLibs evaluates IPHONEOS_DEPLOYMENT_TARGET instead of TARGET_DEVICE_OS_VERSION. Pairing generic/platform=iOS Simulator with EXCLUDED_ARCHS=x86_64 on the command line keeps SwiftPM targets compiling for arm64-only simulator binaries, and running an all-Mach-O otool -L check over the built .app verifies that libswiftCompatibilitySpan.dylib was copied into Frameworks/.