Search code examples
govisual-studio-codegopath

How to update the GOPATH for VS Code Go extension tools?


My $GOPATH is /home/shambhav/code_mis/golang(current gopath), but long ago when I was setting up GO, I messed it up and GOPATH was /home/shambhav/home/shambhav/code_mis/golang(previous gopath). I noticed this and deleted the contents of /home/shambhav/home/shambhav/code_mis/golang and the directory itself and set GOPATH to the correct place.

Visual Studio Code seems to have stored /home/shambhav/home/shambhav/code_mis/golang as my GOPATH. And it isn't changing even after I changed my GOPATH. VSC obviously can't find my imports and things like gocode, goimports, golps etc in /home/shambhav/home/shambhav/code_mis/golang, VSC is kinda useless now. And also when I let VSC to download gocode, golps, etc to see what would happen, it remade /home/shambhav/home/shambhav/code_mis/golang` and put the files there.

I'm not going to switch my GOPATH because the directory is so weird, I will be made fun of if someone sees it, the only option is to update the GOPATH.

Some clarification, the Go compiler knows my actual GOPATH, it's just VSC not updating it.

So,

Long ago: GOPATH = home/shambhav/home/shambhav/code_mis/golang

VSC stored the Long ago GOPATH.

Now: GOPATH = home/shambhav/code_mis/golang

But VSC still thinks that the long ago GOPATH is still the real GOPAH and it is causing problems.


Solution

  • You can change the GOPATH for only the VS Code Go extension tools by specifying the go.toolsGopath property in settings.json

    So, in settings.json that would be,

    "go.toolsGopath": "home/shambhav/code_mis/golang"
    

    After you made this change reinstall the tools and you should be good to go.

    You can refer the following link for more details,
    https://github.com/golang/vscode-go/blob/master/docs/settings.md