Search code examples
swiftxcodelldb

How can I fix the error "Cannot create Swift scratch context" when using LLDB


Whenever I run an LLDB expression in swift mode I get the following error.

Cannot create Swift scratch context (couldn't load the Swift stdlib)Cannot create Swift scratch context (couldn't load the Swift stdlib)Stack dump: 0. Program arguments: /Applications/Xcode.app/Contents/Developer/usr/bin/lldb [1] 77539 segmentation fault lldb

it does not matter what I try to import.

~ ❯❯❯ lldb
(lldb) expression -l swift -- import AppKit
Cannot create Swift scratch context (couldn't load the Swift stdlib)Cannot create Swift scratch context (couldn't load the Swift stdlib)Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/usr/bin/lldb
[1]    6665 segmentation fault  lldb
~ ❯❯❯ lldb                                                                                                                                                        ✘ 139
(lldb) expression -l swift -- import Foundation
Cannot create Swift scratch context (couldn't load the Swift stdlib)Cannot create Swift scratch context (couldn't load the Swift stdlib)Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/usr/bin/lldb
[1]    7122 segmentation fault  lldb
~ ❯❯❯ lldb                                                                                                                                                        ✘ 139
(lldb) expression -l swift -- import UIKit
Cannot create Swift scratch context (couldn't load the Swift stdlib)Cannot create Swift scratch context (couldn't load the Swift stdlib)Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/usr/bin/lldb
[1]    7225 segmentation fault  lldb
~ ❯❯❯                                                                                                                                                             ✘ 139

What's interesting is the very same commands work when using the LLDB within Xcode.


Solution

  • The following conversation works in my Terminal:

    themini:~ mattneubelcap$ swift
    Welcome to Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15).
    Type :help for assistance.
      1> :
    (lldb) expr -lSwift -- import Foundation
    (lldb) expr -lSwift -- "howdy" as NSString
    

    The success of the second command proves that Foundation was successfully imported. If not, we'd have received error: use of undeclared type 'NSString'.

    Note the lack of space between -l and Swift. I do not know why "the very same commands" work using LLDB in Xcode, i.e. with the space. Certainly if you type help expr, you get the impression that -l Swift should work. Nevertheless, experimentation shows that in the Terminal it has to be either -lSwift (no space) or --language Swift (space), just like most command-line commands. Could it be that the Xcode LLDB is more forgiving or follows different syntax rules (uses a different parser)? I have no idea.