asdf-community / asdf-golang

Go plugin for the asdf version manager
https://github.com/asdf-vm/asdf
MIT License
534 stars 88 forks source link

Switching versions doesn't update `GOPATH` #106

Open gskll opened 1 year ago

gskll commented 1 year ago

Describe the bug When I switch between versions the go env variables are out of sync. This causes issues when running commands such as go install or go test

❯ go install github.com/vektra/mockery/v2@v2.20.0
# internal/goarch
compile: version "go1.19.9" does not match go tool version "go1.20.5"
❯ go test ./...
# internal/cpu
compile: version "go1.19.9" does not match go tool version "go1.20.5"

To Reproduce Steps to reproduce the behavior:

$ asdf plugin update --all
$ asdf install golang 1.20.5
$ asdf global golang 1.20.5
$ asdf install golang 1.19.9
$ asdf local golang 1.19.9

$ asdf reshim golang
$ asdf list golang
 *1.19.9
  1.20.5
$ go version
go version go1.19.9 darwin/arm64

$ asdf list golang
 *1.19.9
  1.20.5

$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="$HOME/Library/Caches/go-build"
GOENV="$HOME/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="$HOME/.asdf/installs/golang/1.20.5/packages/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="$HOME/.asdf/installs/golang/1.20.5/packages"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="$HOME/.asdf/installs/golang/1.19.9/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="$HOME/.asdf/installs/golang/1.19.9/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19.9"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="$HOME/.../go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/py/yt2zkc3j2qv9cwc50ydn2_sw0000gp/T/go-build2400661110=/tmp/go-build -gno-record-gcc-switches -fno-common"

Expected behavior For the go env variables to all be updated pointing to the current asdf version In the go env output above the problematic variables are:

Additional context

❯ asdf version
v0.11.3
❯ fish --version
fish, version 3.6.1

In my config.fish I have

source ~/.asdf/plugins/golang/set-env.fish
set -gx GOROOT (go env GOROOT)
set -gx GOPATH (go env GOPATH)

I get the expected behaviour by adding a line to this to manually update the go env

source ~/.asdf/plugins/golang/set-env.fish
go env -w GOPATH=(asdf where golang)/packages
set -gx GOROOT (go env GOROOT)
set -gx GOPATH (go env GOPATH)

Am I doing something wrong? If not could we extend the set-env functions to update the GOPATH as well as GOROOT?

dobleme commented 1 year ago

I'm a newbie on asdf and go but forcing to export the GOPATH always solved my problem, because I have a specific GOPATH in my .zshrc was not being rewriting it and I couldn't install another gopls version. I know there is the option file $HOME/.default-golang-pkgs, but I wanted to be able to install things at demand.

I don't know if it's a good idea, I understand there will be people that doesn't want that behavior and always want to install into his original GOPATH. For me, is useful, since I want to be able to use the specific gopls. The only drawback is when I set my system version again, I need to remove the shims in order to use the original GOPATH bin.

Maybe, you know another ways? I don't know if I'm breaking something else.

diff --git a/bin/exec-env b/bin/exec-env
index a363908..2691fd5 100755
--- a/bin/exec-env
+++ b/bin/exec-env
@@ -5,7 +5,5 @@ if [ "${ASDF_INSTALL_VERSION}" != 'system' ] ; then
            export GOROOT=$ASDF_INSTALL_PATH/go
        fi

-       if [[ "unset" == "${GOPATH:-unset}" ]] ; then
-           export GOPATH=$ASDF_INSTALL_PATH/packages
-       fi
+       export GOPATH=$ASDF_INSTALL_PATH/packages
 fi
ankitcharolia commented 1 year ago

@gskll @dobleme Kindly try out this goenv: https://github.com/ankitcharolia/goenv