iOS

[Xcode] When debugging with Xcode, “couldn’t create a ClangImporter) Cannot create Swift scratch context” is displayed, and the value cannot be confirmed during debugging.

xcode-cant-debug-en

[Xcode] When debugging with Xcode, “couldn’t create a ClangImporter) Cannot create Swift scratch context” is displayed, and the value cannot be confirmed during debugging.

I got a new M1 and tried to build an existing app using Xcode, but after I set a breakpoint and tried to check the value of a running variable using po, I encountered to error below.

Cannot create Swift scratch context (couldn’t create a ClangImporter)Cannot create Swift scratch context (couldn’t create a ClangImporter)

The error was displayed and I could not confirm the value, so I will summarize the trial and error to solve it as a memorandum.

Reproduction environment

  • MacBook Pro(14inch、2021)/ Apple M1 Max(macOS Monterey v12.0.1)
  • Xcode13.2.1
  • For some reason, the default Shell on Mac was bash, so zsh is installed in /opt/homebrew/bin/zsh with Homebrew.

How to fix

First of all, when I googled the content of the error, according to this site, the Shell setting seemed to be suspicious, so I reviewed the setting (as a result, this was correct)

The cause was that zsh wasn’t in the Shell path referenced by Xcode.
The situation was as follows.

  • Settings app → Users & Groups → Advanced options → Login shell(How to change is written here)
    • /usr/local/bin/zsh <- Xcode reference this path
  • But zsh doesn’t exist in /usr/local/bin/zsh

I don’t know why this is the case, but Xcode was referencing /usr/local/bin/zsh which doesn’t exist.
Since zsh doesn’t exist in /usr/local/bin/zsh, chsh -s /bin/zsh also results in chsh: no changes made.

In other words, the correction policy is either of the following.

①install zsh to /usr/local/bin/zsh.
If you install using Homebrew on M1, it will be installed in /opt/homebrew/bin/zsh, so in my case, I installed it manually by referring to this article (I think that Homebrew can also do it, but the method is I did not know).
After that, I uninstalled zsh installed via Homebrew and changed SHELL to the above path.

②Change the Login Shell path of “Settings app-> Users & Groups” described above.
From the above screen, change the Login Shell to the path of the Shell you are currently using, in your case /opt/homebrew/bin/zsh installed via brew.

I tried both, but I thought it was subtle to change the login shell path, so I took action ①.

After restarting Xcode after the above response, I can now safely set a breakpoint and check the value of the variable with po 🎉.

Wrap up

This time I solved the phenomenon that I could not debug well with Xcode, but at first I did not know the cause at all, so I did not know that Shell was related, so it was good to know that Xcode and Shell are related.
Also, compared to Intel Macs so far, M1 is often clogged around the settings, so I want to make sure that I will not be in trouble with this setup from the next time onwards!

+1

COMMENT

Your email address will not be published. Required fields are marked *

CAPTCHA