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

How can i use my own GOPATH default? #277

Open
carryxyh opened this issue Sep 14, 2017 · 24 comments
Open

How can i use my own GOPATH default? #277

carryxyh opened this issue Sep 14, 2017 · 24 comments

Comments

@carryxyh
Copy link

carryxyh commented Sep 14, 2017

everytime i login my zsh. my GOPATH is /.../.gvm/pkgsets/go1.9/global.

@carryxyh
Copy link
Author

carryxyh commented Sep 14, 2017

Why the GVM PKGSET modify my system GOPATH ?
And how can i modify it ?
Now , all my go path is /.gvm/pkgsets/system/global , I am regretting use the gvm pkgset.
It is make my go env very chaotic

@carryxyh
Copy link
Author

carryxyh commented Sep 14, 2017

export GVM_ROOT; GVM_ROOT="/Users/xiuyuhang/.gvm" export gvm_go_name; gvm_go_name="go1.9" export gvm_pkgset_name; gvm_pkgset_name="global" export GOROOT; GOROOT="$GVM_ROOT/gos/go1.9" export GOPATH; GOPATH="/Users/xiuyuhang/go-project" export GVM_OVERLAY_PREFIX; GVM_OVERLAY_PREFIX="${GVM_ROOT}/pkgsets/go1.9/global/overlay" export PATH; PATH="${GVM_ROOT}/pkgsets/go1.9/global/bin:${GVM_ROOT}/gos/go1.9/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}" export LD_LIBRARY_PATH; LD_LIBRARY_PATH="${GVM_OVERLAY_PREFIX}/lib:${LD_LIBRARY_PATH}" export DYLD_LIBRARY_PATH; DYLD_LIBRARY_PATH="${GVM_OVERLAY_PREFIX}/lib:${DYLD_LIBRARY_PATH}" export PKG_CONFIG_PATH; PKG_CONFIG_PATH="${GVM_OVERLAY_PREFIX}/lib/pkgconfig:${PKG_CONFIG_PATH}" export gvm_pkgset_name="go-pj" export PATH; PATH="/Users/xiuyuhang/.gvm/pkgsets/go1.9/go-pj/bin:$PATH" export GVM_OVERLAY_PREFIX; GVM_OVERLAY_PREFIX="${GVM_ROOT}/pkgsets/go1.9/go-pj/overlay" export PATH; PATH="/Users/xiuyuhang/.gvm/pkgsets/go1.9/go-pj/bin:${GVM_OVERLAY_PREFIX}/bin:${PATH}" export LD_LIBRARY_PATH; LD_LIBRARY_PATH="${GVM_OVERLAY_PREFIX}/lib:${LD_LIBRARY_PATH}" export DYLD_LIBRARY_PATH; DYLD_LIBRARY_PATH="${GVM_OVERLAY_PREFIX}/lib:${DYLD_LIBRARY_PATH}" export PKG_CONFIG_PATH; PKG_CONFIG_PATH="${GVM_OVERLAY_PREFIX}/lib/pkgconfig:${PKG_CONFIG_PATH}"

why export so many same var like :

export PATH; PATH="/Users/xiuyuhang/.gvm/pkgsets/go1.9/go-pj/bin:${GVM_OVERLAY_PREFIX}/bin:${PATH}"
....
export PATH; PATH="${GVM_ROOT}/pkgsets/go1.9/global/bin:${GVM_ROOT}/gos/go1.9/bin:${GVM_OVERLAY_PREFIX}/bin:${GVM_ROOT}/bin:${PATH}"

so chaotic isnt it??

@mgm702
Copy link

mgm702 commented Dec 2, 2017

I'm having this same issue and I was wondering if there was any solution to fixing this problem?

@carryxyh
Copy link
Author

carryxyh commented Dec 2, 2017

I really dont know ... This issue does not have anybody to attention

@anitgandhi
Copy link

In my .bashrc or .bash_profile, AFTER the gvm related source lines, I have export GOPATH=$HOME/go and everything goes back to normal.

You can do something similar to set it to your expected GOPATH, just make sure put the line after so your setting will override GVM's.

@kaedys
Copy link

kaedys commented Dec 12, 2017

You can fix this on a version-by-version basis using gvm pkgenv, and then changing the export GOPATH; GOPATH="<blah>" line to point at your normal GOPATH. However, you have to do this for each version of Go you have installed through GVM (because each version has a different pkgenv set), and you have to re-set your default Go version after you do this, because gvm use <version> --default copies that version's pkgenv, instead of simply piggybacking off it.

I really wish there was an option in GVM to not use version-specific GOPATHs.

@antagon
Copy link

antagon commented Feb 11, 2018

Look at gvm linkthis --help.

@adam-hanna
Copy link

adam-hanna commented Jul 6, 2018

I tried adding export GOPATH=$HOME/go to my ~/.bashrc after the sourced gvm scripts, but then my go stopped working. I have no idea why.

EDIT - scratch that. I just figured out the error but don't exactly know why it was happening. My ~/.bashrc had the following:

[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
# gvm pkgset use global
export GOPATH=$HOME/go
export GOBIN=$HOME/go/bin
export PATH=$GOBIN:$PATH

I changed the last line to: export PATH=$PATH:$GOBIN and everything's working now. Don't know why the order of $GOBIN matters???

TLDR; My updated, working ~/.bashrc is:

[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
# gvm pkgset use global
export GOPATH=$HOME/go
export GOBIN=$HOME/go/bin
export PATH=$PATH:$GOBIN

@pecigonzalo
Copy link

Edit ❯ vim ~/.gvm/environments/default and set your defaults

@kaedys
Copy link

kaedys commented Aug 27, 2018

@adam-hanna you don't need to set $GOBIN. If unset, it is automatically inferred as $GOPATH/bin, or if $GOPATH is not set, ~/go/bin (since $GOPATH is inferred to be ~/go if not set). GVM does not alter or set $GOBIN, so you don't need to either.

And as previously stated, the proper way to define a consistent singular GOPATH is to us gvm pkgenv. With your method, if you use gvm use <version> at any point, you'll have to reload your .bashrc to reset your GOPATH again. If you update pkgenv, the gvm will natively set your GOPATH properly.

@pecigonzalo
Copy link

@kaedys I edited the file because I could not get pkgenv to work with system go.

EG:

> gvm use system --default
> gvm pkgenv 
(edit as required)

after reloading the shell, gvm was not setting my default pkgenv.

The use case, just so you have context, installing some user tool, like awless or so.

@kaedys
Copy link

kaedys commented Aug 28, 2018

You have to edit the pkgenv before you set default, because the --default flag copies the specified pkgenv to a separate default pkgenv file that you can't directly edit, at least via the pkgenv command. Do this:

gvm use system
gvm pkgenv
<edit as required>
gvm use system --default

Now it will use your pkgenv changes on reload.

@pecigonzalo
Copy link

@kaedys thanks for the tip, that worked but what if you have to change it again later on?

@kaedys
Copy link

kaedys commented Sep 12, 2018

@pecigonzalo Simply edit the pkgenv for the relevant version (in this case, system) again, and then call gvm use <version> --default again. Exact same sequence of commands as above. Basically, think of gvm use <version> --default as taking a read-only copy of the specified version pkgenv. To alter the values, you need to edit them in the original pkgenv and tell gvm to take another copy.

While it's intuitive to think of --default as simply pointing gvm at which version to enable by default (and that would be a solid way of implementing it), that's not how gvm currently implements --default. It's a copy, not a pointer.

@pecigonzalo
Copy link

pecigonzalo commented Sep 12, 2018

Makes sense, thanks a lot for the explanation. Maybe it would be good to add this to the Wiki

@ohenepee
Copy link

ohenepee commented Oct 2, 2018

How do you use the pkgenv... nothing I type shows. I already regret going the gvm way. Couldn't things be made a little easier?

@kaedys
Copy link

kaedys commented Oct 28, 2018

How do you use the pkgenv... nothing I type shows. I already regret going the gvm way. Couldn't things be made a little easier?

Literally just type gvm pkg. Call gvm use <version> first to pick which version you're editing. gvm pkgenv opens your terminal editor, which defaults to vi. If you're not sure how to use vi (it's an acquired taste/skill), use this command instead to use a more user-friendly editor instead:

EDITOR=nano gvm pkgenv

@ahmad380360
Copy link

شكرا

@yidane
Copy link

yidane commented Apr 10, 2019

I change all GOPATH in gvm environment files and It fixed.

@tkshnwesper
Copy link

I was able to achieve a desirable result using sym-linking

ln -s $GOPATH "$HOME/go"

@mrzou
Copy link

mrzou commented Dec 23, 2019

https://github.com/golang/go/wiki/SettingGOPATH

@kaedys
Copy link

kaedys commented Jan 6, 2020

https://github.com/golang/go/wiki/SettingGOPATH

Not even slightly helpful. Read the thread maybe?

The issue is that GVM automatically configures itself with version-specific GOPATHs, despite a single global GOPATH being a common (I suspect majority) use case. The question was how to configure GVM to use one's already-configured global GOPATH (or $HOME/go, if empty) rather than the version-specific one, and ideally how to make it default to that (which it cannot do).

That all said, with the advent of modules, GOPATH is really nothing more than a base location for determining the bin (target of go install) and the module cache location. The former can be set using the environment variable $GOBIN (and GVM does not mutate or overwrite this env var), and the latter, while not configurable, doesn't really need to be configured or be a global location.

So the TL;DR for this is, use module mode. If you absolutely must use non-module mode, you'll have to update the GOPATH definition using gvm pkgenv (and remember to run gvm use <version> against afterwards, to make the change take effect.)

@xumu1
Copy link

xumu1 commented Mar 17, 2022

Edit GOPATH of the file (/.gvm/environments/default and same level config) , thengvm use go1.x(anyway, let it changed).
Now the GOPATH is your specify.

@kaedys
Copy link

kaedys commented Mar 17, 2022

Duh, that's in the documentation. The problem is that you have to do that again for every new version of Go you install using GVM. The ask is for some way to tell newly installed versions, newly created pkgenvs, to use a specific fixed GOPATH instead of GVM always trying to segregate GOPATHs between versions. That hasn't been needed for years, and definitely isn't needed in the world of Go modules, since literally the only purpose of the GOPATH now is for your GOBIN (which is independent of your installed Go version) and your module cache, which the Go tooling already handles natively because its version-aware when it comes to dependency models. Like, GVM could entirely omit the GOPATH set, and just leave it as whatever the user had previously set, or the default ${HOME}/go, and Go would work perfectly fine and in fact in a more predictable manner.

As it stands, if you install a new Go version using GVM and forget to update the pkgenv, it will force you to redownload your entire module cache, and then also reinstall any tools to your (new) GOBIN that you've downloaded (or locally installed) using go install. The Go tooling is already version-aware, so there's literally zero reason to still be defaulting to a complete version separate of the GOPATH.

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