Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching directories doesn't change go version #294

Open
Integralist opened this issue Mar 29, 2023 · 11 comments
Open

Switching directories doesn't change go version #294

Integralist opened this issue Mar 29, 2023 · 11 comments

Comments

@Integralist
Copy link

👋🏻

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.

@johnliu55-msft
Copy link

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

@Integralist
Copy link
Author

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
Copy link
Author

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
}

@johnliu55-msft
Copy link

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.

@Integralist
Copy link
Author

@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

@ChronosMasterOfAllTime
Copy link
Collaborator

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

@Integralist
Copy link
Author

Ah, thanks for clarifying.

@chriscarpenter12
Copy link

Is this the same issue as #186 ?

@nphilbrook
Copy link

Is this the same issue as #186 ?

Yes, and I believe the same issue as #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.

@dicebattle
Copy link

dicebattle commented Jan 18, 2024

@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.

@Integralist
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants