我使用的是 Ubuntu 20.04.3 LTS,VSCode 1.62.3,go 1.17.3 linux/amd64
当我在 VSCode 中启动调试器时,它会向我显示此警告
“dlv-dap”命令不可用。请选择“安装”,或 请按照此处的安装说明进行操作。来源:Go(扩展)[安装][全部安装]
我单击“安装”并在输出中看到:
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
当我通过 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.
go.toolsEnvVars
值得推荐:
我们考虑完全弃用所以,我使用以下设置:
go.goroot
,并建议用户在需要修改go的环境变量时使用go.toolsEnvVars
。但存在可用性和安全性问题。因此,我们恢复了 go.goroot 的旧行为。大多数用户根本不应该设置 go.goroot 设置。更多:
"go.toolsEnvVars": {
"GOROOT": "path-to-go-root"
},
额外的设置,以防您可能需要设置 $GOROOT 并在终端中使用它:
"terminal.integrated.env.linux": {
"GOROOT": "path-to-go-root"
},