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?

I did create a new user, and saw success in getting debugging to work
properly.

That’s great news.

Making a permanent switch to another user account would be too
difficult at this point, as well.

Indeed, and that’s not what I’m proposing. Rather, this is a diagnostic test which shows that there’s some sort of state in your user account that’s triggering this problem. Now you just have to figure out what it is (-:

Unfortunately I don’t have any obvious suggestions for where to look, but here’s another thing you can try:
  1. Create a new user account.

  2. Log in.

  3. Take a snapshot of the files in that user account’s home directly, for example, by running find ~.

  4. Set up Xcode and run your test.

  5. Redo the snapshot as per step 3.

  6. Compare the two snapshots. This should give you a list of files that Xcode touched while building your project. You can take this knowledge back to your main account and go on a deleting spree (-:

WARNING Make a backup first, obviously (-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks for the guidance on this. I've gone through the steps you've mentioned, deleting any items I found in my existing (broken) user account that I saw created in the new account (please note that I did not delete some items, as they were related to TextEdit, or other apps). However, this did not solve the issue.

Still experiencing this as of Xcode 12.2 beta 4 (12B5044c). Did anyone find a fix or have anything new to try?
The issue still happens for me with Xcode Version 12.3 beta (12C5020f). I will note that, 12.0 - 12.2 Playgrounds will not work with live view, but 12.3 does. Not sure if the issue was connected, but I did see a small "improvement" with my build machine. Will keep on with the print statements.
Decided to update my toolchains today. Moved from Xcode 12.0 to 12.2 and now I'm experiencing the same issue. Very frustrating. The same "Cannot create Swift scratch context (couldn't create a ClangImporter)" for Version 12.2 (12B45b) and Version 12.3 (12C33).
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
JChecn37's advice about switching to bash and back to zsh fixed my problem! You're a lifesaver!
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.

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.

Error when debugging: Cannot create Swift scratch context
 
 
Q