goenv icon indicating copy to clipboard operation
goenv copied to clipboard

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

Open envintus opened this issue 2 years ago • 14 comments

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.

envintus avatar Jul 18 '21 00:07 envintus

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 avatar Sep 13 '21 20:09 derekcrovo

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

derekcrovo avatar Sep 13 '21 20:09 derekcrovo

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

jochman avatar Nov 11 '21 08:11 jochman

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 avatar May 12 '22 01:05 neersighted

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.

neersighted avatar May 12 '22 01:05 neersighted

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

Nalum avatar Aug 17 '22 11:08 Nalum

Looks like this has been solved.

ChronosMasterOfAllTime avatar Feb 19 '23 05:02 ChronosMasterOfAllTime

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.

ianjukes avatar Oct 10 '23 07:10 ianjukes

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

ChronosMasterOfAllTime avatar Oct 10 '23 11:10 ChronosMasterOfAllTime

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

ianjukes avatar Oct 10 '23 11:10 ianjukes

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

rahul-r-modi avatar Nov 09 '23 18:11 rahul-r-modi

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.

chriscarpenter12 avatar Nov 09 '23 18:11 chriscarpenter12

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

Unfortunately not -- I still have the same issue.

ianjukes avatar Nov 15 '23 08:11 ianjukes

@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

ChronosMasterOfAllTime avatar Dec 13 '23 17:12 ChronosMasterOfAllTime