Efron Licht
Efron Licht

Reputation: 498

Gopath environment errors

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.

Upvotes: 4

Views: 1786

Answers (2)

payneio
payneio

Reputation: 453

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.

Upvotes: 0

VonC
VonC

Reputation: 1323593

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
    "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
        },

You can replace "${workspaceRoot}" by C:/work/go in your case.
That way, a Ctrl+Shift+B 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

{
    "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
        }
    ]
}

Again you can replace "${workspaceRoot}" 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 F5 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 "go.goroot": "D:/prgs/go/latest")

Upvotes: 2

Related Questions