swift: Swift script importing Cocoa frameworks (Cocoa, AppKit, CryptoKit) fails with Symbols not found (macOS 14)

Description Swift script that imports AppKit fails with “JIT session error: Symbols not found”.

By “swift script” I mean a swift source file that is run directly with /usr/bin/swift, xcrun swift, or as an executable script with a shebang line like #!/usr/bin/env swift.

Using swiftc on the same source file compiles cleanly and the executable works as expected.

Steps to reproduce

 $ printf 'import AppKit\n\nprint( NSScreen.main!.frame.size )' > a.swift && swift a.swift
JIT session error: Symbols not found: [ _OBJC_CLASS_$_NSScreen ]
Failed to materialize symbols: { (main, { _$sSoMXM, __swift_FORCE_LOAD_$_swiftDarwin_$_a, __swift_FORCE_LOAD_$_swiftObjectiveC_$_a, __swift_FORCE_LOAD_$_swiftDispatch_$_a, __swift_FORCE_LOAD_$_swiftXPC_$_a, _$ss5print_9separator10terminatoryypd_S2StFfA0_, _$ss27_finalizeUninitializedArrayySayxGABnlF, _got.$s12CoreGraphics7CGFloatVMn, __swift_FORCE_LOAD_$_swiftos_$_a, _symbolic _____ So6CGSizeV, _symbolic _____ 12CoreGraphics7CGFloatV, _$ss5print_9separator10terminatoryypd_S2StFfA1_, _$sSo6CGSizeVwst, __swift_FORCE_LOAD_$_swiftCoreImage_$_a, _$sSa12_endMutationyyF, __swift_FORCE_LOAD_$_swiftCoreGraphics_$_a, _$sSo6CGSizeVMF, $.a.__inits.0, __swift_FORCE_LOAD_$_swiftQuartzCore_$_a, __swift_FORCE_LOAD_$_swiftUniformTypeIdentifiers_$_a, __swift_FORCE_LOAD_$_swiftMetal_$_a, _$sSo6CGSizeVMn, _$sSo6CGSizeVMf, __swift_FORCE_LOAD_$_swiftCoreFoundation_$_a, _$sSo6CGSizeVML, _$sSo6CGSizeVMB, __swift_FORCE_LOAD_$_swiftFoundation_$_a, _main, _$sSo6CGSizeVwet, _$sSo6CGSizeVMa, __swift_FORCE_LOAD_$_swiftOSLog_$_a, _$sSo6CGSizeVWV, ___swift_noop_void_return, ___swift_memcpy16_8, __swift_FORCE_LOAD_$_swiftIOKit_$_a }) }

 $ swiftc a.swift && ./a
(2560.0, 1440.0)

Expected behavior Should work when run directly with swift or as an executable with #!/usr/bin/env swift.

Environment swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1) Target: arm64-apple-macosx14.0

Xcode 15.0 Build version 15A240d

Related Issues Similar to https://github.com/apple/swift/issues/59354

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Reactions: 11
  • Comments: 19 (3 by maintainers)

Commits related to this issue

Most upvoted comments

So for others on the thread, a workaround — if you have xcode installed — is to change your shebang line:

#!/usr/bin/env DYLD_FRAMEWORK_PATH=/System/Library/Frameworks /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift

I found a workaround. If you manually set the system framework search path, everything works:

swift -Fsystem /System/Library/Frameworks path_to_script.swift

+1 - Sonoma has bricked my lil’ command line scripts I was using fine on Ventura

Except with import Cocoa.

swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1) Target: arm64-apple-macosx14.0

JIT session error: Symbols not found: [ _OBJC_CLASS_$_NSPasteboard, _NSPasteboardTypeString ]

I have many similar little scripts that fail from importing Cocoa.

Started off as an easy way to get familiar with the language. Now that they’re failing, I realize I relied on them a LOT!

So for others on the thread, a workaround — if you have xcode installed — is to change your shebang line:

@rdj thank you for this! 🙌🏼 Confirmed it works for me.

I am also seeing this issue when trying to import Core Data in a script that has worked for years.

Since this may be a macOS / Swift integration issue and not a pure Swift issue, I have filed a Feedback with Apple: ‘FB13453942 Regression: Scripts written in Swift fail when importing system frameworks’

@ausdrew xcrun -sdk macosx swiftc YourScript.swift -o YourScript. from, its work for me.

That command is invoking the Swift compiler and instead of running the script directly using the swift command. @rdj also mentioned that it’s possible to compile the source file…

Based on my experience, this issue still exists. Hopefully someone from Apple will see this and provide a fix real soon. 🤞 🙏