Error when debugging: Cannot create Swift scratch context

When I run my app from Xcode 12b1 and hit a break point, LLDB says "Cannot create Swift scratch context (couldn't load the Swift stdlib)" and any subsequent commands like "po" seem to do nothing. Who can I fix this?
Post not yet marked as solved Up vote post of mhoeller Down vote post of mhoeller
16k views

Replies

Hello,

Have you set your system's active developer directory to your new Xcode 12 Beta 1 install ?

If it is still pointing to a previous version of Xcode, like Xcode 11.5, that might be the cause of the error.

You can switch from one Xcode install to another with the following commands:

Code Block sh
# Print current active developer directory.
xcode-select --print-path
# Set active developer directory to a new Xcode install
sudo xcode-select --switch <path/to/Xcode-beta.app>


Once you have updated your active developer directory, I invite you to rebuild your project from scratch (Xcode -> Product -> Clean and Xcode -> Project -> Build), and try debugging it again.

If everything is set correctly and the error is still showing up, could you please submit a feedback with the code that triggers the error and specify your project's deployment target version ?

Thanks.


  • This answer doesn't work at all. I am using XCode 14.3.1 and still facing this issue from a year.

Add a Comment
Thank you for your response. Unfortunately your suggestion did not fix the issue.

I can submit a feedback, but I can't attach the code because it is a confidential client project. It might be related to my specific project setup or 3rd party dependencies (managed via CocoaPods). A quick test with a new project created in Xcode 11.4 and then opened in Xcode 12b1 showed nothing unusual.. it just worked as expected. I will do some more experimentation.

Any more detailed hints or pointers as to what may cause the issue are very welcome 😉
I have the same issue/error in my project.
my project was also failing on the previous Xcode and I had submitted a feedback bug report but never got an answer. (the error was different but referred also to swift context). I will post a new bug report.

My project is public/open source, so I can post the step to reproduce:

git clone https://github.com/roznet/connectstats
pod install
open ConnectStats.xcworkspace
Ctrl-Shift O : to open "GCActivity+Import.swift"
put a break point in the constructor (line 44 for example)
Ctrl-U (run the tests)
you hit the break point, it will print the error and trying to "po var" will fail. note the the variable explorer has the correct value for the variables, only the lldb interactive shell fails.


Correction my previous post: Command-Sift-O to open the file and Command-U to run the test, (not Ctrl)
Could you capture a types log by putting "log enable lldb types -f /tmp/types.log" into ~/.lldbinit-Xcode an relaunching Xcode and reproducing the error?

If it doesn't show up in the LLDB console the types usually contains the underlying error. You can either post it here, or look through it yourself and read the blocks around "LogConfiguration" closely. They show how the Swift and Clang compilers that are embedded into LLDB were initialized for your app. Usually they contain a hint why things go wrong.
Thanks for this. I did as requested (attaching the types.log). Around the LogConfiguration near the error it shows all the search paths, import path, etc up to the same error as in the console Cannot create Swift scratch context (couldn't load the Swift stdlib)

I paste here the list of system path it list (and truncate the path that seem related to my app, but I attached the whole file as well. I am not sure where it's supposed to find the swift stdlib and which path maybe missing/incorrect?

Code Block
SwiftASTContextForExpressions::LogConfiguration(SwiftASTContext*)0x7ff801a514c0:
Architecture : x86_64-apple-ios13.5.0-simulator
SDK path : /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.16.sdk
Runtime resource path : /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift
Runtime library paths : (2 items)
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator
/usr/lib/swift
Runtime library import paths : (2 items)
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator
/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.16.sdk/usr/lib/swift
Framework search paths : (8 items)
/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Products/Debug-iphonesimulator
/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Products/Debug-iphonesimulator/Flurry-iOS-SDK
/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Products/Debug-iphonesimulator/GenericJSON-iOS
/Users/brice/Development/connectstats/Pods/GoogleMaps/Base/Frameworks
/Users/brice/Development/connectstats/Pods/GoogleMaps/Maps/Frameworks
/Users/brice/Development/connectstats
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator14.0.sdk/Developer/Library/Frameworks
Import search paths : (6 items)
/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Products/Debug-iphonesimulator
/Users/brice/Development/connectstats/fit-sdk-swift/RZFitFile/sdk
/Users/brice/Development/connectstats/fit-sdk-swift/RZFitFile/src
/Users/brice/Development/connectstats/fit-sdk-swift/RZFitFile/RZFitFilePrivate
/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Products/Debug-iphonesimulator/GenericJSON-iOS
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/lib
Extra clang arguments : (85 items)
-I/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Intermediates.noindex/ConnectStats.build/Debug-iphonesimulator/ConnectStats.build/swift-overrides.hmap
-I/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Intermediates.noindex/ConnectStats.build/Debug-iphonesimulator/ConnectStatsXCTests.build/DerivedSources-normal/x86_64
[....]
-I/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Intermediates.noindex/ConnectStats.build/Debug-iphonesimulator/ConnectStatsXCTests.build/DerivedSources/x86_64
-I/Users/brice/Library/Developer/Xcode/DerivedData/ConnectStats-cxkmyrvkqnsurebgsrhaudywenlp/Build/Intermediates.noindex/ConnectStats.build/Debug-iphonesimulator/ConnectStatsXCTests.build/DerivedSources
SwiftASTContextForExpressions::() -- Failed to create scratch context - couldn't load the Swift stdlib






The fundamental problem appears to be the mismatch between the target triple
SwiftASTContextForExpressions::LogConfiguration(SwiftASTContext*)0x7ff801a514c0: Architecture : x86_64-apple-ios13.5.0-simulator

and the chosen SDK:

SDK path : /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.16.sdk

For that triple we should have picked an iPhoneSimulator SDK. It would be interesting to see the rest of the log to understand why this happened. Would you be able to attach the full log to a Feedback Assistant ticket?
I have submitted a ticket in feedback assistant with the same title as this thread, and included the types.log as well as the steps to reproduce. The ticket has the id FB7830933

thanks!



Just for the records: I do get the same message ("Cannot create Swift scratch context (couldn't load the Swift stdlib)") when running logic tests for a swift package on an iOS 14.0 simulator, tests using iOS 14 features (i.e. resource copying) will also fail.

Selecting the Mac as target for running the tests solves the problem.
Additional info: When debugging the app (which is pulling the package mentioned above) I do get the same "Cannot create Swift scratch context" message. The app seems to run ok though, only debugging is a bit of a pain.

Code Block language
 SwiftASTContext("MyAPP")::CreateInstance() -- Corresponding source not found for /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/clang_compiler_rt/clang-1200.0.22.7/compiler-rt/lib/profile/InstrProfiling.c, loading module is unlikely to succeed
 SwiftASTContext("MyAPP")::SetTriple("arm64-apple-ios14.0.0") setting to "arm64-apple-ios14.0.0"
 SwiftASTContext("MyAPP")::SetTriple("arm64-apple-ios14.0.0") setting to "arm64-apple-ios14.0.0"
 SwiftASTContext("MyAPP")::DeserializeAllCompilerFlags() -- Found 3 AST file data entries.
 SwiftASTContext("MyAPP")::DeserializeAllCompilerFlags() -- SDK path from module "MyAPP" is "/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.0.sdk".
 SwiftASTContext("MyAPP")::DeserializeAllCompilerFlags() -- SDK path from module "MyLIB" is "/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.0.sdk".
 SwiftASTContext("MyAPP")::DeserializeAllCompilerFlags() -- SDK path from module "MyLIB" is "/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.0.sdk".
 SwiftASTContext("MyAPP")::DeserializeAllCompilerFlags() -- Picking SDK path "/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.0.sdk".
 SwiftASTContext("MyAPP")::CreateInstance() -- Found serialized triple arm64-apple-ios14.0.
 SwiftASTContext("MyAPP")::SetTriple("arm64-apple-ios14.0") setting to "arm64-apple-ios14.0"
 SwiftASTContext("MyAPP")::CreateInstance() -- Serialized SDK path is /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.0.sdk.
 SwiftASTContext("MyAPP")::CreateInstance() -- Host SDK path is .
 SwiftASTContext::GetResourceDir() -- trying toolchain path: 
 SwiftASTContext::GetResourceDir() -- trying Xcode path: /Applications/Xcode-beta.app/Contents
 SwiftASTContext::GetResourceDir() -- trying Xcode-based lib path: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift
 SwiftASTContext::GetResourceDir() -- found Swift resource dir via Xcode contents path + default toolchain relative dir: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift
 SwiftASTContext("MyAPP")::GetASTContext() -- Using Clang module cache path: /Users/MySELF/Library/Developer/Xcode/DerivedData/ModuleCache.noindex
 SwiftASTContext("MyAPP")::GetASTContext() -- Using prebuilt Swift module cache path: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/prebuilt-modules
 SwiftASTContext("MyAPP")::RegisterSectionModules("MyAPP") retrieved 3 AST Data blobs from the symbol vendor.
 SwiftASTContext("MyAPP")::RegisterSectionModules() -- parsed module "MyAPP" from Swift AST section 1 of 3.
 SwiftASTContext("MyAPP")::RegisterSectionModules() -- parsed module "MyLIB" from Swift AST section 2 of 3.
 SwiftASTContext("MyAPP")::RegisterSectionModules() -- parsed module "MyLIB" from Swift AST section 3 of 3.
 SwiftASTContext("MyAPP")::GetModule("MyAPP")
 SwiftASTContext("MyAPP")::GetModule("MyAPP") -- found MyAPP
 SwiftASTContext("MyAPP")::GetModule("MyLIB")
 SwiftASTContext("MyAPP")::GetModule("MyLIB") -- found MyLIB
 SwiftASTContext("MyAPP")::GetModule("MyLIB")


thanks.

Yes. the app runs fine, as you mention debugging is a pain, and that's the main reason I am not migrating more of the logic to swift and sticking to Objective C. It makes you appreciate how useful having a debugger is :):)

It could be something in the build settings, but I haven't figure out what....



This is a real issue for me as well, for two of my projects. I've created FB7850694 describing the issue.
We've been seeing this issue with team members that upgraded to Xcode 12 beta 1 this past week. Feedback filed: FB7844232. Happy to provide additional information to anyone looking to debug further.
Just checked Xcode 12 Beta 2 and same error. Same step to reproduce as above.

Code Block
git clone https://github.com/roznet/connectstats
pod install
  • open ConnectStats.xcworkspace

  • Command-Shift O : to open "GCActivity+Import.swift"

  • put a break point in the constructor (line 44 for example)

  • Command-U (run the tests)

you then hit the break point, it will print the error and trying to "po var" will fail. note the the variable explorer has the correct value for the variables, only the lldb interactive shell fails.
There is a known issue where an iPhoneSimulator SDK is misdetected as MacOSX SDK — unfortunately the fix for this didn't make it into beta 2. When you capture a types log and LogConfiguration shows a macOS SDK being chosen for a simulator binary that's that issue. If you are seeing this symptom and don't see a macOS SDK selected in the types log, please let us know through FeedbackAssistant (ideally with the types log attached)!