goenv icon indicating copy to clipboard operation
goenv copied to clipboard

How to combine Goenv to use VSCode?

Open tabris233 opened this issue 1 year ago • 7 comments

Does anyone use both vscode and goenv?

Because of different projects use different Golang versions. So I look for Golang Version Management tool. I found goenv, it's cool. Goenv can install different Goalng versions and switch easily like pyenv. Very thanks for this tool.

I use VSCode to write the golang code. Although I have set goenv local 1.18.10, VSCode still recognizes the Golang version of goenv global. Like this.

image

So how to change to 1.18.10?

(Very sorry for my poor english)

tabris233 avatar Mar 22 '23 16:03 tabris233

Ran into the same issue as well. Any guidance on how to fix this since it prepends to the path causing goenv to be useless in this case. Thanks in advance.

saheljalal avatar Apr 13 '23 01:04 saheljalal

me too ... looking forward to someone to help solve it.

waset avatar May 16 '23 11:05 waset

One possible way is to add the following line(replace the path with real $GOROOT) to tell vscode the path of go root, to either the ~/Library/Application Support/Code/User/settings.json (default for current user) or /Users/yangfei/work/code/vitess/.vscode/settings.json (will override user configuration and project only)

  "go.goroot": "/Users/yangfei/work/tools/goenv/versions/1.19.9"
➜  echo $GOROOT
/Users/yangfei/work/tools/goenv/versions/1.19.9

For example

Screen Shot 2023-06-04 at 13 52 38

amyangfei avatar Jun 04 '23 05:06 amyangfei

The above solution is not ideal since settings.json will have to be updated whenever the go version is updated.

brianstrauch avatar Aug 08 '23 20:08 brianstrauch

When I'm in a directory that defines a .go-version, I open vscode or vscodium as follows from this directory:

# For vscode
$> code .
# For vscodium
$> codium .

This allows the IDE to inherit the environment values from this directory. Make sure that you already closed any vscode instances before opening the IDE from this directory.

I think changing the goroot values manually within vscode is not a maintainable solution when you're working with multiple projects with different go env values.

serdardalgic avatar Sep 24 '23 13:09 serdardalgic

@tabris233 @waset https://github.com/owenthereal/goup, use this bro, image the config is this, image image when you use goup change go verison,reload vscode, the goversion in the status bar will automatically switch image

clearyup avatar Mar 31 '24 07:03 clearyup

I sorted it out and it's working as expected adding this to vscode settings:

"go.alternateTools": {
        "go": "/path/to/goenv/.goenv/shims"
    },

Usually, the path to goenv is $HOME/.goenv. This seems to tell gopls to use the go version set by goenv instead of injecting the first version it finds on the system. Important to note that I couldn't change go versions on the fly, meaning I need to set the goenv version I want before starting vscode. Most likely a reset of the gopls would do the trick, but I haven't tried that.

Another important thing, the issue https://github.com/go-nv/goenv/issues/367 seems to be a duplicate of this one. I have not tried all the solutions proposed there as what I described already fixed the issue for me (tested in Mac and Ubuntu on WSL2).

If you try all the above steps and you're still having issues, close all vscode instances and run goenv rehash, then try again

brunogbv avatar Jul 24 '24 15:07 brunogbv