go-nv / goenv

:blue_car: Like pyenv and rbenv, but for Go.
https://github.com/go-nv/goenv
MIT License
2.05k stars 245 forks source link

goenv not setting $GOPATH and $GOROOT on Apple M1 using ZSH #186

Open envintus opened 3 years ago

envintus commented 3 years ago

I've followed the installation instructions in an attempt to setup goenv and have it manage $GOPATH and $GOROOT on my Apple M1 MacBook Pro.

I'm using ZSH as my shell and this is the contents of my .zshrc file.

export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"

After configuring .zshrc and restarting my shell this is what I see for Golang environment variables:

$ env | grep GO | sort
GOENV_ROOT=/Users/hsatterwhite/.goenv
GOENV_SHELL=zsh

For what it's worth I'm able to successfully use goenv to install Golang. I also have a non-M1 MacBook Pro that I did the same setup on using ZSH and had no issues with installation and having goenv manage $GOPATH and $GOROOT.

derekcrovo commented 2 years ago

I had the same problem on traditional, non-M1 hardware. Turns out that earlier in my giant .zshrc I was source'ing GVM. It was setting GOROOT to be an empty string. Once I removed that it worked and my $PATH is back to normal.

derekcrovo commented 2 years ago

Scratch that, I must have had a bad test. I still have the issue.

jochman commented 2 years ago

Same on non-m1 mac. goenv rehash does setting the GOROOT and GOPATH vars after changing them.

neersighted commented 2 years ago

The issue here is export PATH="$GOROOT/bin:$PATH". $GOROOT is evaluated when your .zshrc is read and not when the PATH is searched. As such you'll directly call the go binaries instead of using goenv's shims. Remove that from your config and things should work as expected.

neersighted commented 2 years ago

It looks like the offending lines came from the README -- it looks like this feature is not that well thought-out and potentially breaks stuff badly. I personally don't use goenv init -/configure according to the README so I never ran into this.

Nalum commented 2 years ago

I've hit this issue, but if I don't run goenv init - then goenv doesn't work for me.

My zshrc config is as follows:

# Check for goenv existance and enable it if it's there
[[ -d "${HOME}/.goenv" ]] && export GOENV_ROOT="${HOME}/.goenv"

# Enable goenv things
if [ ! -z "${GOENV_ROOT}" ]; then
  export PATH="${GOENV_ROOT}/bin:${PATH}"
  eval "$(goenv init -)"
  ## Commenting these as it breaks the use of `.go-version` files.
  #export PATH="${GOROOT}/bin:${PATH}"
  #export PATH="${PATH}:${GOPATH}/bin"
fi
ChronosMasterOfAllTime commented 1 year ago

Looks like this has been solved.

ianjukes commented 11 months ago

I just started using the latest version goenv on my MacBook Pro with an Apple M1 processor, installed via Homebrew, and am having this exact same issue. The shell environment variables $GOPATH and $GOROOT are not set when I use goenv shell <version> or goenv local <version> commands. Note -- the version of Go is switched (with the one caveat below) but not the environment variables.

As per @jochman's post above, goenv rehash will set the variables after switching the version, but I think this should be done automatically?

And like @Nalum, when I follow the install instructions, adding these two lines after eval "$(goenv init -)" in my .zshrc breaks the use of .go-version for local version switching.

export PATH="${GOROOT}/bin:${PATH}"
export PATH="${PATH}:${GOPATH}/bin"

If I remove those two lines local version switching works (but setting the environment variables still doesn't work).

@ChronosMasterOfAllTime -- you marked this issue as resolved back in Feb 19? It does not appear to be resolved, unless I misunderstood the discussion in this thread.

Any help really appreciated.

ChronosMasterOfAllTime commented 11 months ago

@ianjukes I will look into this. I also have a a Mac with zsh installed. Let me try to reproduce what you're running into.

ianjukes commented 11 months ago

Thank you @ChronosMasterOfAllTime. Just let me know if you need me to do any additional troubleshooting.

rahul-r-modi commented 10 months ago

Did this ever work for you @ianjukes? I am facing the same issue as you

chriscarpenter12 commented 10 months ago

As @ianjukes described I have the same behavior.

As per @jochman's post above, goenv rehash will set the variables after switching the version, but I think this should be done automatically?

And like @Nalum, when I follow the install instructions, adding these two lines after eval "$(goenv init -)" in my .zshrc breaks the use of .go-version for local version switching.

export PATH="${GOROOT}/bin:${PATH}"
export PATH="${PATH}:${GOPATH}/bin"

Using ubuntu with zsh and this is my experience also. The two exports added after the init described in the readme break the .go-version file switching, and it requires manually running goenv rehash to update the env vars.

ianjukes commented 9 months ago

Did this ever work for you @ianjukes? I am facing the same issue as you

Unfortunately not -- I still have the same issue.

ChronosMasterOfAllTime commented 8 months ago

@ianjukes please find me on Discord so we can better figure out what's going on

my username is the same as here but all lowercase