无法在VSCode中调试Go代码 – 找不到GOROOT目录:/snap/bin/go

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

Can't debug Go in VSCode - cannot find GOROOT directory: /snap/bin/go

问题

我正在使用Ubuntu 20.04.3 LTS,VSCode 1.62.3和go 1.17.3 linux/amd64。

当我在VSCode中启动调试器时,它显示以下警告:

"dlv-dap"命令不可用。请选择"install",或按照此处的安装说明进行安装。来源:Go(扩展)[安装] [全部安装]

我点击安装并在输出中看到:

Tools environment: GOPATH=/home/raspberry/GolandProjects
在模块模式下在/home/raspberry/GolandProjects/bin中安装1个工具。
  dlv-dap

安装github.com/go-delve/delve/cmd/dlv@master失败
{
 "killed": false,
 "code": 2,
 "signal": null,
 "cmd": "/usr/local/go/bin/go get -x -d github.com/go-delve/delve/cmd/dlv@master",
 "stdout": "",
 "stderr": "go: cannot find GOROOT directory: /snap/bin/go\n"
}

1个工具安装失败。

dlv-dap: 无法安装dlv-dap(github.com/go-delve/delve/cmd/dlv@master): 错误: 命令失败: /usr/local/go/bin/go get -x -d github.com/go-delve/delve/cmd/dlv@master
go: cannot find GOROOT directory: /snap/bin/go

当我通过go工具检查GOROOT时,我看到:

$ go env GOROOT
/usr/local/go

当我检查/snap/bin/时,我看到:

~$ cd /snap/bin/
:/snap/bin$ ls
canonical-livepatch  pre-commit.validate-config
go                   pre-commit.validate-manifest
gofmt                snap-store
go.gofmt             snap-store.ubuntu-software
pre-commit           snap-store.ubuntu-software-local-file

我感到困惑,/snap/bin/go存在,但是VSCode告诉我不存在,并且无法运行调试器。如何修复它?

更新1

当我将GOROOT设置为/snap/bin/go时,通过在控制台中输入以下内容:

$ export GOROOT=/snap/bin/go

当我尝试运行调试器时,VSCode开始打印以下内容:

Couldn't find dlv-dap at the Go tools path, /home/raspberry/GolandProjects/, /home/raspberry/GolandProjects or /usr/local/go/bin:/home/raspberry/.local/bin:/home/raspberry/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Follow the setup instruction in https://github.com/golang/vscode-go/blob/master/docs/debugging.md#getting-started.

英文:

I am using Ubuntu 20.04.3 LTS, VSCode 1.62.3, go 1.17.3 linux/amd64

When I start the debugger in VSCode it shows me this warning

> The "dlv-dap" command is not available. Please select "install", or
> follow the installation instructions here. Source: Go (Extension) [Install] [Install All]

I click Install and see that in the output:

Tools environment: GOPATH=/home/raspberry/GolandProjects
Installing 1 tool at /home/raspberry/GolandProjects/bin in module mode.
  dlv-dap

Installing github.com/go-delve/delve/cmd/dlv@master FAILED
{
 "killed": false,
 "code": 2,
 "signal": null,
 "cmd": "/usr/local/go/bin/go get -x -d github.com/go-delve/delve/cmd/dlv@master",
 "stdout": "",
 "stderr": "go: cannot find GOROOT directory: /snap/bin/go\n"
}

1 tools failed to install.

dlv-dap: failed to install dlv-dap(github.com/go-delve/delve/cmd/dlv@master): Error: Command failed: /usr/local/go/bin/go get -x -d github.com/go-delve/delve/cmd/dlv@master
go: cannot find GOROOT directory: /snap/bin/go

When I check GOROOT by the go tool I see that:

$ go env GOROOT
/usr/local/go

When I check /snap/bin/ I see that:

~$ cd /snap/bin/
:/snap/bin$ ls
canonical-livepatch  pre-commit.validate-config
go                   pre-commit.validate-manifest
gofmt                snap-store
go.gofmt             snap-store.ubuntu-software
pre-commit           snap-store.ubuntu-software-local-file

I'm confused, there is /snap/bin/go but VSCode tells me that there is not and fails to run the debugger. How to fix it?

UPDATE 1

When I set GOROOT to /snap/bin/go by typing this into the console:

$ export GOROOT=/snap/bin/go

VSCode starts to print that when I try to run the debugger:

Couldn't find dlv-dap at the Go tools path, /home/raspberry/GolandProjects/, /home/raspberry/GolandProjects or /usr/local/go/bin:/home/raspberry/.local/bin:/home/raspberry/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Follow the setup instruction in https://github.com/golang/vscode-go/blob/master/docs/debugging.md#getting-started.

答案1

得分: 12

所以,这是修复方法-在VSCode中添加“首选项:打开设置(JSON)”这一行

"go.goroot": "/snap/go/current"

这是解释-https://github.com/golang/vscode-go/issues/166

英文:

So, here is the fix - add to VSCode Preferences: Open Settings (JSON) this line

"go.goroot": "/snap/go/current"

Here is the explanation - https://github.com/golang/vscode-go/issues/166

答案2

得分: 0

以下是导航和添加的确切路径:
在VSCode中,转到
文件->首选项->设置->扩展->Go
您将找到"编辑为JSON"按钮。
在那个位置添加以下代码:
无法在VSCode中调试Go代码 – 找不到GOROOT目录:/snap/bin/go

英文:

Below is exact path to navigate and add
in VSCode go to
File-> Preferences-> Settings-> Extension-> Go
you will find edit as json button.
Add this code in that place
无法在VSCode中调试Go代码 – 找不到GOROOT目录:/snap/bin/go

huangapple
  • 本文由 发表于 2021年12月2日 13:26:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/70194570.html
匿名

发表评论

匿名网友

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

确定