goenv icon indicating copy to clipboard operation
goenv copied to clipboard

Switching directories doesn't change go version

Open Integralist opened this issue 2 years ago • 11 comments

👋🏻

I use https://www.warp.dev/ with a Zsh shell and a https://starship.rs/ prompt (I also use Zoxide for switching directories but the reported issue happens even with standard cd).

I'm finding that when I switch between project directories (where each project has its own .go-version file and are set with different Go versions), that changing directory doesn't trigger the go binary to be the expected version.

Here is my ~/.zshrc configuration:

if [ ! -d "$HOME/.goenv" ]; then
  git clone https://github.com/syndbg/goenv.git ~/.goenv
fi
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"

At the time of reporting this issue, I'm using the latest release:

$ goenv --version
goenv 2.0.6

If I open up a new terminal instance at my home directory ~/ and run which go I see:

/Users/integralist/.goenv/versions/1.20.0/bin/go

This aligns with goenv global.

Now if I cd into a project directory with a .go-version file containing 1.20.2 and re-run go version I'll still see the global Go 1.20.0 being used. If I reload my shell scripts (e.g. source ~/.zshrc) then running both which go and go version will now report the expected 1.20.2 version.

Now if I cd into another project with a .go-version file containing 1.18.5 and re-run go version I'll still see the previous Go version 1.20.2 being used. If I reload my shell scripts (e.g. source ~/.zshrc) then running both which go and go version will now report the expected 1.18.5 version.

Integralist avatar Mar 29 '23 09:03 Integralist

I encountered the same issue. I had to remove the last two PATH manipulations at the end:

# export PATH="$GOROOT/bin:$PATH"
# export PATH="$PATH:$GOPATH/bin"

for version switch to work, where go version shows the correct version corresponding to the .go-version file. However, both $GOROOT and $GOPATH environment variables are not updated.

image

johnliu55-msft avatar Apr 19 '23 13:04 johnliu55-msft

However, both $GOROOT and $GOPATH environment variables are not updated.

Does that break anything for you? e.g. do any of your Go projects not compile?

I don't understand the consequence of having the go command pointing at the correct binary and yet the environment variables being incorrect.

Have you checked go env GOPATH GOROOT? I would expect it to show a version that corresponds to the same binary version as the go command is showing.

So here's what I've just tried. I comment out the PATH modifications you mention...

if [ ! -d "$HOME/.goenv" ]; then
  git clone https://github.com/syndbg/goenv.git ~/.goenv
fi
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
# export PATH="$GOROOT/bin:$PATH"
# export PATH="$PATH:$GOPATH/bin"

I then open up a new terminal instance so my ~/.zshrc gets loaded and I run go version, which returns...

go version go1.18 darwin/arm64

I check goenv local and goenv shell (neither are set) but goenv global correctly shows version 1.20.3 is set (as I previously set that to be my global version).

So I can see that goenv hasn't affected the go command to point at the 1.20.3 binary.

Now, running go version doesn't change the output from before, it's still...

go version go1.18 darwin/arm64

A which go still reveals it's set to /usr/local/go/bin/go.

I then run which go because I'm interested what binary is being executed...

/usr/local/go/bin/go

Now that's interesting because I don't recall installing go manually?

I then run goenv which go and see...

/Users/integralist/.goenv/versions/1.20.3/bin/go

Now what's really interesting is the following...

$ go env GOPATH GOROOT
/Users/integralist/go/1.20.3
/Users/integralist/.goenv/versions/1.20.3

...although go version reports 1.18 the go env thinks that 1.20.3 should be used (as if goenv had an affect).

So yeah goenv is having no actual affect (since commenting out the two PATH modifications) although it internally has configured the right global to be used and otherwise thinks that is what's being used.

OK so now, I cd to /tmp where I have a .go-version file set to 1.18.5 and yup no change, it doesn't automatically switch still. Even if I re-source my ~/.zshrc so commenting out those two PATH modifications completely break things for me.

Not sure why they worked for you 🤔 (at least partially worked for you).

Integralist avatar Apr 20 '23 08:04 Integralist

Here's my workaround that seems to work for me (e.g. re-sourcing the .zshrc every time I change directory)...

# Standard configuration...

if [ ! -d "$HOME/.goenv" ]; then
  git clone https://github.com/syndbg/goenv.git ~/.goenv
fi
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"

# I already have `cd` and `__zoxide_cd` functions defined 
# for running `ls` every time I change directory, 
# so now I just add `source ~/.zshrc` to it as well...

function cd {
  builtin cd "$@"
  RET=$?
  ls
  source ~/.zshrc
  return $RET
}

function __zoxide_cd {
  builtin cd "$@"
  RET=$?
  ls
  source ~/.zshrc
  return $RET
}

Integralist avatar Apr 20 '23 08:04 Integralist

However, both $GOROOT and $GOPATH environment variables are not updated.

Does that break anything for you? e.g. do any of your Go projects not compile?

Nothing breaks, I mentioned this because the two lines I commented out are leveraging these two environment variables which don't seem to be updated by goenv accordingly.

I then run which go because I'm interested what binary is being executed...

/usr/local/go/bin/go

Now that's interesting because I don't recall installing go manually?

Maybe check whether you configured the $PATH environment variable somewhere so that /usr/local/go/bin/go has higher precedence then goenv's shim? In my case no matter what the current working directory is, running which go always shows:

/home/johnliu/.goenv/shims/go

which I think is mandatory for goenv to work correctly.

johnliu55-msft avatar Apr 21 '23 02:04 johnliu55-msft

@ankitcharolia thanks, I'll try it out.

What's the rationale for this fork? Are there significant issues with the current version, and what has been changed in the fork?

Thanks

Integralist avatar Aug 24 '23 11:08 Integralist

@Integralist that isn't a fork, rather a whole new implementation relying on a golang-compiled CLI tool using goreleaser.

ChronosMasterOfAllTime avatar Aug 24 '23 18:08 ChronosMasterOfAllTime

Ah, thanks for clarifying.

Integralist avatar Aug 24 '23 18:08 Integralist

Is this the same issue as #186 ?

chriscarpenter12 avatar Nov 10 '23 14:11 chriscarpenter12

Is this the same issue as #186 ?

Yes, and I believe the same issue as https://github.com/go-nv/goenv/issues/209 as well

The key point being the docs recommend

export PATH="$GOROOT/bin:$PATH" 

in your shell RC file, but this means local .go-versions won't work unless you re-source that file every time you change directories. Since the global default GOROOT will be ahead of the shims in your path.

nphilbrook avatar Nov 14 '23 10:11 nphilbrook

@Integralist I don't think it's a complete solution, but at least you've given me a way to work around it. Thank you.

And since it's been almost a year since this issue was first reported, I'm wondering why it still hasn't been fixed.

dicebattle avatar Jan 18 '24 06:01 dicebattle

@dicebattle this is my approach now

https://github.com/Integralist/dotfiles/blob/main/.zshrc#L574-L596

https://github.com/Integralist/dotfiles/blob/main/.zshrc#L517-L527

Again, not perfect but does the job better than before

Integralist avatar Jan 18 '24 09:01 Integralist