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

Finally fixed this by changing name of my Xcode app from Xcode(12.3) to Xcode_12_3 so it seems that the problem was in characters that needs to be escaped. So if anyone has brackets or quotas in their path to Xcode, check this.
Trying to learn Swift and use debugging. On Xcode Version 12.3 (12C33) any time debugger is entered (on device simulator, or an actual device):

Cannot create Swift scratch context (couldn't create a ClangImporter) (lldb) 

po symbol in the (lldb) prompt doesn't work. And so no Swift objects or symbols are available; only an Objective C-type view of them.

This is very annoying! and makes the debugger and debugging basically worthless in Xcode for Swift.

On an M1 Mac mini. I completely uninstalled Xcode and deleted preferences and other developer directories from ~/Library. and re-installed Xcode (from app store). I scoured here and stack overflow for workarounds or diagnoses and nothing has worked.

Code Block console
$ system_profiler SPSoftwareDataType
Software:
System Software Overview:
System Version: macOS 11.1 (20C69)
Kernel Version: Darwin 20.2.0
Boot Volume: Macintosh HD
Boot Mode: Normal
Computer Name: *
User Name: *
Secure Virtual Memory: Enabled
System Integrity Protection: Enabled
Time since boot: 1:48


this works (swift from command line to run lldb from within) but I have no idea if it's useful info to help diagnose.

Code Block shell
❯ swift
Welcome to Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28).
Type :help for assistance.
1> :
(lldb) expression -l swift -- import Foundation
(lldb)


related:
  • on same M1 Mac mini, create a new test user account and the debugger in Xcode 12.3 starts up normally (without the ClangFormatter message) and Swift variables work properly

  • on an Intel Mac also running macOS 11.1 Big Sur and Xcode 12.3, the debugger starts up normally at breakpoint (without the ClangFormatter message) and Swift variables work properly




Still getting this on Xcode 12.4 Intel Macbook Pro Big Sur 11.1. I've tried removing all my zsh dot files like @idcrook said but it still says "Cannot create Swift scratch context (couldn't create a ClangImporter)". On a brand new user account it does work though. Please let me know if anyone has any other ideas to try.
After hours, days, and weeks of trying to fix this issue, I finally got it to work! Try this:

Change your shell from zsh to bash.
Code Block
chsh -s /bin/bash

Run your project and debugger should work as expected. Then change your shell back to zsh and the debugger should still work.

I’m not sure what happened or why but I hope this can help someone!

Xcode Version: 12.4 (12D4e)

Mac OS Big Sur 11.1
  • Tried this (command entered in Terminal) but changing shell to bash did not fix the problem using Xcode 12.3 on Catalina. When starting terminal the message "The default interactive shell is now zsh" even the window is captioned -bash and current shell obtained by echo $SHELL is bash

  • Running Xcode from terminal with "open project.xcodeproj" in project.xcodeproj folder after changing shell to bash worked ... looks as though Xcode picks up its shell from somewhere else

  • XCode works OK on different account, even when run from Launchpad. Running from Terminal didn't help on main dev account

Add a Comment
JChecn37's advice about switching to bash and back to zsh fixed my problem! You're a lifesaver!
  • Can't believe that this worked. It would be nice to know why this is.

Add a Comment
Marry me JChen37. I can't believe that worked.

I will say though that some custom shells (in my case, fish) return the debugger to its problematic state even after switching back from bash/zsh. So if you're like me and use iTerm, set the custom shell for your profile only and leave bash/zsh as the system default to please Xcode.
  • Seeing this answer solve the problem for others had me disheartened because a new user didn't fix it for me. I then decided to launch my project from the command line and then I saw variables in the debugger and no ClangImporter error. I did happen to switch from bash to zsh a few times before launching from the command line, so that was likely still an important step.

Add a Comment

I managed to get this fixed, in my case the root cause was a library that was distributed in binary form and had somehow managed to put extra framework searchpaths in the library configuration. The search paths were relative to the developers computer home folder so they did not work properly. There were also some extra flags that were used for their internal debugging.

I managed to find the root cause by using swift-healthcheck on the debugger and comparing it to a version that did not have this problem.

There is not too much information about the swift-healthcheck available currently, but you can just type it in the debugger and see the output. Debug Swift with LLDB (WWDC 2022) session had some information.

Post not yet marked as solved Up vote reply of PGHM Down vote reply of PGHM