无法在 Mac M1 上使用 VSCode 运行调试 Go 代码。

huangapple go评论103阅读模式
英文:

Cannot run debug Go using VSCode on Mac M1

问题

我找到了一个遇到相同问题的主题(https://stackoverflow.com/questions/67637872/cant-debug-golang-in-vscode-apple-m1),但我不确定这是否是一个旧的解决方案,因为我正在使用的Go版本是go1.17.1 darwin/arm64,搭配dlv版本1.7.2和VSCode版本1.60.2(arm64),在Mac M1 BigSur(11.6)上运行调试(fn+f5)时,调试控制台显示:

Starting: /Users/username/go/bin/dlv-dap dap --check-go-version=false --listen=127.0.0.1:53115 --log-dest=3 from /Users/username/go/src/project-name
DAP server listening at: 127.0.0.1:53115

同时出现了一个弹出错误:

Failed to launch: could not launch process: can not run under Rosetta, check that the installed build of Go is right for your CPU architecture

我尝试将Go版本降级到1.16.8 arm64,并使用dlv 1.6.1,但仍然遇到相同的错误。

我可以在这两个版本上成功使用go build命令。

英文:

I found a topic that encounter the same problem (https://stackoverflow.com/questions/67637872/cant-debug-golang-in-vscode-apple-m1) but I'm not sure it's an old solution or not because I'm using the Go version

go1.17.1 darwin/arm64 

with

dlv version 1.7.2 

and

VSCode version 1.60.2 (arm64)

on

Mac M1 BigSur (11.6)

when I run debug (fn+f5) a Debug console shows:

Starting: /Users/username/go/bin/dlv-dap dap --check-go-version=false --listen=127.0.0.1:53115 --log-dest=3 from /Users/username/go/src/project-name
DAP server listening at: 127.0.0.1:53115

and there is a Pop-up error:

Failed to launch: could not launch process: can not run under Rosetta, check that the installed build of Go is right for your CPU architecture

I have tried downgrade go version to 1.16.8 arm64 with dlv 1.6.1 but still got the same error.

I can use go build successfully on both versions.

答案1

得分: 32

以下是我解决这个问题的步骤:

  1. 卸载 Golang(AMD-64 版本)。
  2. 安装 Golang(ARM-64 版本),并通过 go env GOARCH 确认安装成功。
  3. $GOPATH/bin 目录中删除 VS-Code 使用的工具二进制文件(重要)。
  4. 重新启动 VS-Code,并安装 VS-Code 需要的工具(如果未安装,VS-Code 将会提示你安装)。

问题的根本原因:我在我的 Apple Mac M1 上意外安装了 amd-64 版本而不是 arm-64 版本。

感谢 Painhardcore 的回答指导我找到了正确的解决方案。

英文:

Here is how I resolved this issue.

  1. Uninstall Golang(AMD-64 version).
  2. Install Golang(ARM-64). Confirm it with go env GOARCH
  3. Remove(tools that VS-Code uses) binaries from $GOPATH/bin (Important)
  4. Restart your VS-Code and install tools that VS-Code needed(if not installed vscode will complain and will ask you to install them)

Root cause of the problem: I accidentally installed amd-64 version instead of arm-64 version in my Apple Mac M1.

Thanks to Painhardcore answer for pointing me in right direction.

答案2

得分: 27

确保您的VSCode使用arm64版本。(它可以使用与系统不同的go版本)

运行Go: install/update tools命令。它将使用arm64 go版本重新构建所有工具。

英文:
  1. Ensure your VSCode uses the arm64 version. (it can use a different
    go version from the system)

  2. Run Go: install/update tools. It will rebuild all tools with the arm64 go version.

答案3

得分: 8

对我有效的方法:

  1. 检查你的 Mac 使用的芯片,你可以在“关于本机”>“概览”>“芯片”中查看。
  2. 如果使用的是 Apple 芯片,则下载“arm64”类型的文件,例如“go-darwin-arm64.pkg”、“vscode-arm64”等。你明白我的意思。
  3. 如果使用的是 Intel 芯片,则下载“amd”类型的文件。
  4. https://github.com/go-delve/delve/tree/master/Documentation/installation 下载并安装“DELVE”。
  5. 运行你的调试器。

如果你想要调试的话,也可以告诉我“launch.json”文件的配置。

编辑 11-04-22

分享“launch.json”文件:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/main.go",
            "args": [],
        }
    ]
}
英文:

What worked for me:

  1. Check for "chip" your mac is using, you can check that from "About This Mac" > "Overview" > "Chip"
  2. If Apple chip is used, then download "arm64" type of files, such as "go-darwin-arm64.pkg", "vscode-arm64" etc. You get my point.
  3. If Intel chip is used, then download with "amd" type.
  4. Download and install "DELVE" from https://github.com/go-delve/delve/tree/master/Documentation/installation
  5. Run your debugger.

Let me know, if you want the "launch.json" file config too, to debug.

EDIT 11-04-22

Sharing the launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
		{
            "name": "Debug",
            "type": "go",
            "request": "launch",
            "mode": "auto",
			"program": "${workspaceFolder}/main.go",
            "args": [],
        }
    ]
}

答案4

得分: 2

这个答案对我有用。我安装了ARM64并再次尝试。

https://github.com/go-delve/delve/issues/2604#issuecomment-1069740132

英文:

This answer worked for me. Installed ARM64 and tried again.

https://github.com/go-delve/delve/issues/2604#issuecomment-1069740132

答案5

得分: 1

从https://github.com/go-delve/delve/tree/master/Documentation/installation下载并安装DELVE

对我来说有效。

英文:

Download and install DELVE from https://github.com/go-delve/delve/tree/master/Documentation/installation

It worked for me

答案6

得分: 1

✅ 解决方案 ✅

问题在于您安装了非 M1 版本的 GoLang 和/或 VSCode。
Go 和 VSCode 都有适用于 Intel 和 Apple 芯片的版本。您需要两者都是 arm64(Apple Silicon)版本。

如果您没有使用 arm64 分发版安装 GoLang 和 VSCode,那么问题就出在这里。

解决此问题的方法

  • 完全从系统中删除 GoLang 和 VSCode:
    • 完全删除 GoLang
      • sudo rm -rf /usr/local/go
      • sudo rm /etc/paths.d/go
      • sudo rm -rf ~/donald/go(将 donald 替换为您自己的帐户名)
        • (删除此部分是解决问题的关键)
    • 完全删除 VSCode
      • rm -fr ~/Library/Preferences/com.microsoft.VSCode.helper.plist
      • rm -fr ~/Library/Preferences/com.microsoft.VSCode.plist
      • rm -fr ~/Library/Caches/com.microsoft.VSCode
      • rm -fr ~/Library/Caches/com.microsoft.VSCode.ShipIt/
      • rm -fr ~/Library/Application\ Support/Code/
      • rm -fr ~/Library/Saved\ Application\ State/com.microsoft.VSCode.savedState/
      • rm -fr ~/.vscode/
  • 下载 GoLang 和 VSCode 的 arm64 版本。
  • 安装 GoLang
  • 安装 VSCode
  • 打开 VSCode。
  • 安装 Go 扩展
  • 打开一个 GoLang 项目/目录。
  • 它会要求您安装 Go 工具(大约有 8 个工具)。只需点击“全部安装”
    在模块模式下安装 8 个工具到 /Users/donald/go/bin。
    gotests
    gomodifytags
    impl
    goplay
    dlv
    staticcheck
    gopls
    go-outline
    
  • 打开您想要运行的 .go 文件。
  • 使用 VS Code 运行它。

就是这样!现在您应该可以正常使用了 👍

英文:

✅ SOLUTION ✅

The issue is that you had installed the non-M1 version of GoLang and/or VSCode.
There are versions for Intel and Apple chips of both Go and VSCode. You need both to be the arm64 (Apple Silicon) version

If you didn't install both GoLang and VSCode using the arm64 distribution then that is where the problem is.

To fix this issue:

  • Completely remove GoLang and VSCode from your system:
    • Remove GoLang completely:
      • sudo rm -rf /usr/local/go
      • sudo rm /etc/paths.d/go
      • sudo rm -rf ~/donald/go (replace donald with your own account name)
        • (removing this is the 🔑 to fixing your issue)
    • Remove VSCode completely:
      • rm -fr ~/Library/Preferences/com.microsoft.VSCode.helper.plist
      • rm -fr ~/Library/Preferences/com.microsoft.VSCode.plist
      • rm -fr ~/Library/Caches/com.microsoft.VSCode
      • rm -fr ~/Library/Caches/com.microsoft.VSCode.ShipIt/
      • rm -fr ~/Library/Application\ Support/Code/
      • rm -fr ~/Library/Saved\ Application\ State/com.microsoft.VSCode.savedState/
      • rm -fr ~/.vscode/
  • Download the arm64 version of each GoLang and VSCode.
  • Install GoLang
  • Install VSCode
  • Open VSCode.
  • Install the Go extension
  • Open a GoLang project/directory.
  • It will ask you to install the Go tools (like 8 or so tools). Just click "Install All"
    Installing 8 tools at /Users/donald/go/bin in module mode.
    gotests
    gomodifytags
    impl
    goplay
    dlv
    staticcheck
    gopls
    go-outline
    
  • Open a .go file that you want to run.
  • Run it using VS Code.

That's it! You should be good to go now 👍

答案7

得分: 0

我正在为你翻译以下内容:

为了让那些在迁移到 M1 Mac 时遇到相同问题的人能够在一个地方获得所有所需的信息,我写下了这篇文章。

在使用 Apple 迁移助理将数据从我的 Intel Mac 迁移到 M1 Mac 后,我遇到了这个问题。基本上,go 二进制文件和用于 amd64 的 vscode 被复制到了 M1 Mac(arm64)上。

因此,我不得不卸载(删除了所有与 go 相关的文件,即位于 /home/<my_user>/go 的文件和位于 /usr/local/go 的 go 可执行文件),然后重新安装适用于 arm64 的 go 和 vscode。

之后,我在使用 delve 在 vscode 中运行调试器时遇到了 could not launch process: EOF 错误,为了解决这个问题,我参考了这个 GitHub 线程 - https://github.com/go-delve/delve/issues/2794

并执行了以下步骤:

sudo rm -rf /Library/Developer/CommandLineTools

xcode-select --install

之后,我能够在 vscode 中使用 delve 调试代码。

英文:

Writing this so that anyone facing same problem while migrating to M1 mac can get all the required information at one place.

I faced this issue after migrating data from my intel mac to M1(using Apple Migration Assistant).
So basically, the go binaries and vscode for amd64 were copied to M1 mac(arm64)

Hence, i had to uninstall (removed all go related files i.e located at /home/&lt;my_user&gt;/go and go executables located at /usr/local/go) and reinstall go and vscode for arm64.

Post that, i was getting the error could not launch process: EOF while running the debugger in vscode using delve, to solve that i referred to this github thread - https://github.com/go-delve/delve/issues/2794

and performed below steps:

sudo rm -rf /Library/Developer/CommandLineTools

xcode-select --install

After that, i was able to use delve to debug code in vscode

答案8

得分: 0

主要的Go安装页面(https://go.dev/doc/install)提供了一个“下载”按钮,它是AMD64版本的。你需要前往这里:https://go.dev/dl/,选择正确的架构。一旦你完成了这个步骤,你需要按照@prakash-p提到的方法卸载Go。然后,你还需要按照@iamcmnut提到的方法重新在VS Code中安装Go工具。

英文:

The main Go install page (https://go.dev/doc/install) offers you a "download" button which is AMD64. You have to go here:
https://go.dev/dl/

and select the correct architecture. Once you've done that you need to uninstall as already mentioned by @prakash-p.

And then you'll also need to reinstall go tools in vs code as mentioned by @iamcmnut

huangapple
  • 本文由 发表于 2021年9月27日 16:58:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/69343900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定