asdf-golang icon indicating copy to clipboard operation
asdf-golang copied to clipboard

Both `$GOPATH` or `$GOROOT` are not set & request for a custom place to use my installed go version

Open MuhmdRaouf opened this issue 5 years ago • 15 comments

Describe the bug Both $GOPATH or $GOROOT are not.

snowflake in ~
➜ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/snowflake/Library/Caches/go-build"
GOENV="/Users/snowflake/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/snowflake/.asdf/installs/golang/1.13.8/packages"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/snowflake/.asdf/installs/golang/1.13.8/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/snowflake/.asdf/installs/golang/1.13.8/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/35/t0nqj9vd2zd735xf5d05ybzr0000gn/T/go-build082200721=/tmp/go-build -gno-record-gcc-switches -fno-common"

snowflake in ~
➜ echo $GOROOT


snowflake in ~
➜ echo $GOPATH


snowflake in ~
➜ asdf version
v0.7.6

To Reproduce Steps to reproduce the behavior:

  1. Install go using asdf-golang
  2. switch to any version of the currently installed go
  3. echo $GOPATH or $GOROOT
  4. return emptiness :D

Expected behavior To Have $GOPATH & $GOROOT set.

Desktop (please complete the following information):

  • OS: macOS Catalina Version 10.15.3
  • SHELL: zsh 5.7.1 (x86_64-apple-darwin19.0)

Additional context Can I add a sort of Place to put my $GOPATH & $GOROOT as I want them to be inside of go folder inside of my home directory and the library append on it. eg: installing go 1.13.8 with the following environment variable ASDF_GOLANG_CUSTOM_PATH=$HOME/go so my $GOPATH & $GOROOT set as the following $GOPATH=ASDF_GOLANG_PATH/1.13.8/...... $GOROOT=ASDF_GOLANG_PATH/1.13.8/......

MuhmdRaouf avatar Feb 24 '20 10:02 MuhmdRaouf

I've found this alias is useful

alias go-reshim='asdf reshim golang && export GOV=$(asdf current golang | sed  '\''s/ *(set by .*)//g'\'') && export GOROOT="$ASDFINSTALLS/golang/$GOV/go/"'

I have this alias called in my .zshrc or .bashrc files, but I find that I need to call this everytime I switch to a new project.

ghost avatar Mar 14 '20 16:03 ghost

+1 for this.

Also, it'd be great if the GOBIN was set. Currently I have to manually set this.

jpmcb avatar Apr 17 '20 18:04 jpmcb

I generally set my defaults in ~/.bashrc and set project local versions in ~/.envrc and use direnv to set/unset. With the latest versions of asdf there is the ability to add a hook for environment variables, but it seems like it could get hairy to determine when I should override what the user already has in their env.

kennyp avatar Jun 16 '20 22:06 kennyp

Any advantages you see to trying to handle that here? I mean I could see setting them in the env hook if they’re not already set. Thoughts?

kennyp avatar Jun 16 '20 22:06 kennyp

Any solution?

jhoonb avatar Jul 03 '20 15:07 jhoonb

it'd be good to document this in readme - what user needs to do for these to be set properly with asdf-golang

RafalSkolasinski avatar Aug 05 '20 11:08 RafalSkolasinski

@kennyp

I recently had this issue while using asdf-golang in my Github Actions pipeline. My goreleaser build step was failing when I was using just asdf-golang: https://github.com/goreleaser/goreleaser-action/issues/241, but goreleaser worked when I switched from asdf-golang to https://github.com/actions/setup-go.

After some debugging, I realized one of the differences between the two was that https://github.com/actions/setup-go sets GOROOT while asdf-golang does not. I was able to rip out actions/setup-go and use asdf-golang again once I added another build step that sets GOROOT for every job.: https://github.com/sourcegraph/ds-to-dhall/commit/a89565d1057a3ff1f9708a2241a291aacf1f42b7#diff-6495850214b8b7be403455ec207980c9

It would be great if asdf-golang could handle setting GOROOT automatically since it can lead to esoteric errors like https://github.com/goreleaser/goreleaser-action/issues/241

ggilmore avatar Aug 31 '20 18:08 ggilmore

I've found this alias is useful

alias go-reshim='asdf reshim golang && export GOV=$(asdf current golang | sed  '\''s/ *(set by .*)//g'\'') && export GOROOT="$ASDFINSTALLS/golang/$GOV/go/"'

I have this alias called in my .zshrc or .bashrc files, but I find that I need to call this everytime I switch to a new project.

Thanks, this has indeed been helpful. I made a slight modification since the sed parsing didn't quite work for my version of asdf.

alias go-reshim='asdf reshim golang && export GOROOT="$(asdf where golang)/go/"'

AnthonyWhitaker avatar Nov 12 '20 16:11 AnthonyWhitaker

@ggilmore So looking at your case it seems like other tools need to know where the GOROOT is and try to get it from the env instead of asking go. The bin/exec-env script properly sets both the GOROOT and the GOPATH and other scripts could get that info from go env GOROOT for example.

I'm still game to do something if we can figure out a clean way to update the env, but I don't know anything off hand. :thinking:

kennyp avatar Jan 01 '21 23:01 kennyp

@RafalSkolasinski What do you have in mind when you say?

what user needs to do for these to be set properly with asdf-golang

Are you asking what you should add to an envrc file or something like that?

kennyp avatar Jan 01 '21 23:01 kennyp

Could something like what the Java plugin does work here as well, with precmd hooks to update the env? https://github.com/halcyon/asdf-java/blob/5b8533eb2b2c8215165bdd9568c5cb9441a94e13/set-java-home.zsh

austince avatar Jun 17 '21 15:06 austince

Using https://github.com/asdf-community/asdf-direnv with global .tool-versions and .envrc for overriding solves all the problems for me.

Matts966 avatar Jun 17 '21 17:06 Matts966

@Matts966 can you give us an example or snippet from your configs please to help us how we can do it.

MuhmdRaouf avatar Jun 17 '21 17:06 MuhmdRaouf

@MuhmdRaouf Here is my dotfiles https://github.com/matts966/dotfiles-open.

  • .zshrc
    • https://github.com/Matts966/dotfiles-open/blob/a43f90bcdee2c0a0e9efb72f4ed7524e444948f8/.zshrc#L73-L75
  • .config/direnv/direnvrc
    • https://github.com/Matts966/dotfiles-open/blob/main/direnvrc
  • .tool-versions
    • https://github.com/Matts966/dotfiles-open/blob/main/.tool-versions
  • .envrc to override defaults
    export ASDF_GOLANG_VERSION=1.15
    use asdf
    

You can refer official doc for other shells. https://github.com/direnv/direnv/blob/master/docs/hook.md

Edit: Maybe I misunderstood the problem. We should set additional export for GOPATH and GOROOT even with asdf-direnv for example by export GOROOT="$(asdf where golang)/go/"' for your global installation.

Edit2: You can add root .envrc with use asdf to set GOPATH and GOROOT for the global installation

Matts966 avatar Jun 17 '21 17:06 Matts966

Why do we need GOPATH?

Since Go 1.11, you don't have to use GOPATH anymore. Simply go to your project directory and do this once:

go mod init github.com/youruser/yourrepo
  • With this, Go creates a module root at that directory.
  • You can create as many modules as you want.
  • For step by step instructions, also see this answer.

https://stackoverflow.com/a/53026674/7391331

trallnag avatar Sep 07 '21 16:09 trallnag

Why do we need GOPATH?

That's where things that you go install go to, including the binaries. Though, you can set GOBIN somewhere other than GOPATH/bin.

asdf puts ~/.asdf/shims at the front of your path, so no matter what version you select, your shell will find the binary. I fell into using asdf-direnv, but for the increased performance when opening a shell (evaluating takes a long time for some reason).

Key thing is to not set any of the Go env vars as shell env vars - let ASDF manage them.

bitwombat avatar Mar 15 '23 00:03 bitwombat

After I thought I had it fixed with some of the previous suggestions, what ended up working for both GOPATH and GOROOT with repo-specific .tool-versions was this in my ~/.bashrc:

# go
export GOPATH=$(asdf where golang)/packages
export GOROOT=$(asdf where golang)/go
export PATH="${PATH}:$(go env GOPATH)/bin"

pythoninthegrass avatar Jul 03 '23 23:07 pythoninthegrass

@pythoninthegrass 's solution worked beautifully for me!

Drew-Daniels avatar Sep 14 '23 17:09 Drew-Daniels

🤙


From: Drew Daniels @.> Sent: Thursday, September 14, 2023 12:17:58 PM To: asdf-community/asdf-golang @.> Cc: pythoninthegrass @.>; Mention @.> Subject: Re: [asdf-community/asdf-golang] Both $GOPATH or $GOROOT are not set & request for a custom place to use my installed go version (#28)

@pythoninthegrasshttps://github.com/pythoninthegrass 's solution worked beautifully for me!

— Reply to this email directly, view it on GitHubhttps://github.com/asdf-community/asdf-golang/issues/28#issuecomment-1719849485, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AA7ILP2LBSOZ4HWKPBSYMWLX2M34NANCNFSM4K2E4IYQ. You are receiving this because you were mentioned.Message ID: @.***>

pythoninthegrass avatar Sep 14 '23 21:09 pythoninthegrass