Gopath环境错误

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

Gopath environment errors

问题

我正在使用Visual Studio Code编写一些Go代码。昨天一切正常,但是现在我无法在VS Code中运行调试器或构建代码。

我使用的是Windows 10,并且我选择使用PowerShell作为终端。

我收到以下错误信息:

go: GOPATH条目是相对路径;必须是绝对路径:"/Users/efronlicht/go"。
有关更多详细信息,请参阅:'go help gopath'
退出状态 2
进程退出,代码:1

这是一个VS Code特定的错误,因为我可以像往常一样通过终端使用go build来构建Go源文件。

以下是go env的结果:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\work\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

如你所见,我的GOPATH是绝对路径,而不是相对路径。

英文:

I'm using Visual Studio code to write some go code. Everything was working fine yesterday, but now I can't run the debugger or build in VS-Code.

I am on Windows 10, and I use Powershell as my terminal of choice.

I get the following error:

go: GOPATH entry is relative; must be absolute path: "/Users/efronlicht/go".
For more details see: 'go help gopath'
exit status 2
Process exiting with code: 1

This is a VS-CODE specific error, because I can build go source files with go build through the terminal as usual.

Here are the results of go env:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\work\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

As you can see, my GOPATH is an absolute path, not a relative one.

答案1

得分: 2

我在Windows 10上使用VSCode 1.13.1,并且在没有任何问题的情况下启动或调试。

启动涉及在工作区中有一个${workspaceroot}/.vscode/tasks.json文件。
为了确保GOPATH值正确,我的GOPATH包括:

{
// 有关tasks.json格式的文档,请参见https://go.microsoft.com/fwlink/?LinkId=733558
"version": "0.1.0",
"command": "build",
"isShellCommand": true,
"showOutput": "always",
"tasks": [
{
"options": {
"env": {
"GOROOT": "D:/prgs/go/latest",
"GOPATH": "${workspaceRoot}"
}
},
"echoCommand": false,
"taskName": "install",
"isBuildCommand": true
},
]
}

你可以在你的情况下将"${workspaceRoot}"替换为C:/work/go
这样,按下<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd>会触发编译和安装(go install)。

调试器涉及以下内容:

  • 在%PATH%中有一个dlv.exe文件
  • 一个${workspaceroot}/.vscode/launch.json文件

这是我的配置:

{
"version": "0.2.0",
"configurations": [
{
"stopOnEntry": false,
"cwd": "${workspaceRoot}",
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {
"GOPATH": "${workspaceRoot}"
},
"args": [],
"showLog": true
}
]
}

同样,你可以在你的情况下将"${workspaceRoot}"替换为C:/work/go(在GOPATHcwd中都替换)。

请注意,我同时指定了GOPATH和cwd(当前工作目录)。
我从工作区根目录相对打开我的文件(这样,断点会被识别)。从main.go中简单地按下<kbd>F5</kbd>可以成功运行delve(在Windows上!)。

有了这些配置,我可以从没有设置GOROOTGOPATHcmd Windows shell中启动VSCode,它仍然可以正常工作(因为我的本地用户设置包括"go.goroot": "D:/prgs/go/latest")。

英文:

I use VSCode 1.13.1 on Windows 10, and I launch or debug without any issue.

Launching involves in your workspace a ${workspaceroot}/.vscode/tasks.json file.
To be sure of the GOPATH value, mine includes:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    &quot;version&quot;: &quot;0.1.0&quot;,
    &quot;command&quot;: &quot;build&quot;,
    &quot;isShellCommand&quot;: true,
    &quot;showOutput&quot;: &quot;always&quot;,
    &quot;tasks&quot;: [
        {
            &quot;options&quot;: {
                &quot;env&quot;: {
                    &quot;GOROOT&quot;: &quot;D:/prgs/go/latest&quot;,
                    &quot;GOPATH&quot;: &quot;${workspaceRoot}&quot;
                }
            },
            &quot;echoCommand&quot;: false,
            &quot;taskName&quot;: &quot;install&quot;,
            &quot;isBuildCommand&quot;: true
        },

You can replace &quot;${workspaceRoot}&quot; by C:/work/go in your case.
That way, a <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> triggers a compilation+installation (go install)

And the debugger involves:

  • having a dlv.exe in the %PATH%
  • a ${workspaceroot}/.vscode/launch.json file

Here is mine

{
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;stopOnEntry&quot;: false,
            &quot;cwd&quot;: &quot;${workspaceRoot}&quot;,
            &quot;name&quot;: &quot;Launch&quot;,
            &quot;type&quot;: &quot;go&quot;,
            &quot;request&quot;: &quot;launch&quot;,
            &quot;mode&quot;: &quot;debug&quot;,
            &quot;remotePath&quot;: &quot;&quot;,
            &quot;port&quot;: 2345,
            &quot;host&quot;: &quot;127.0.0.1&quot;,
            &quot;program&quot;: &quot;${fileDirname}&quot;,
            &quot;env&quot;: {
                &quot;GOPATH&quot;: &quot;${workspaceRoot}&quot;
            },
            &quot;args&quot;: [],
            &quot;showLog&quot;: true
        }
    ]
}

Again you can replace &quot;${workspaceRoot}&quot; by C:/work/go in your case (both in GOPATH and cwd).

Note that I specificy GOPATH as well as cwd (current working directory)
I open my file relative from the workspace root (that way, the breakpoints are recognized). A simple <kbd>F5</kbd> from main.go does run delve successfully (on Windows!)

With that, I can launch VSCode from a cmd Windows shell which has no GOROOT or GOPATH set, and it still works. (because my local user settings do include &quot;go.goroot&quot;: &quot;D:/prgs/go/latest&quot;)

答案2

得分: 0

如果你正在命令行中的 tmux shell 中启动 VSCode,请尝试在 tmux 之外启动。还可以尝试从启动器中启动。最近在 MacOS 上,tmux 和 VSCode 的更改在环境设置方面给我带来了很多麻烦,导致了类似的问题。

英文:

If you are launching VSCode from the command line in a tmux shell, try launching outside of tmux. Also try launching from the launcher. Changes in tmux and VSCode on MacOS have been giving me headaches with environment settings lately resulting in similar issues.

huangapple
  • 本文由 发表于 2017年6月14日 05:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/44531822.html
匿名

发表评论

匿名网友

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

确定