Cannot run debug Go using VSCode on Mac M1

Viewed 10217

I found a topic that encounter the same problem (Can't 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.

5 Answers
  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.

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.

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": [],
        }
    ]
}
Related