GOPATH environment variable not getting set in the Fish shell?
Asked Answered
T

2

5

I have a ~/.config/fish/config.fish which contains the following lines:

set PATH $PATH (go env GOPATH)/bin
set -x GOPATH (go env GOPATH)

If I simply run go env GOPATH at the command line, I get the default location (~/go):

> go env GOPATH
/Users/kurt/go

However, the $GOPATH environment variable is not defined:

> echo $GOPATH

Why is the $GOPATH environment variable not getting set?

Tews answered 23/10, 2019 at 2:13 Comment(2)
What about your path? It includes the $GOPATH/bin directory? Maybe you can't execute (go env GOPATH) in the config.fish file... In my config I have the full path: set -x GOPATH /home/myuser/goBroider
Works for me (I use both fish and go) and have the same set -x GOPATH (go env GOPATH) in my ~/.config/fish/config.fish. I suspect your actual situation is different from what you have reported here. One possibility is you haven't set $PATH appropriately.Labradorite
R
4

set -x will only affect your current session. Once you logout of that session, the export will go away.

set --export --global will export your environment across all running fish sessions, however is ephemeral and will not persist after you log out.

set --export --universal will export your environment across all running fish sessions, and is persistent.

-g or --global
       Causes the specified shell variable to be given a global scope. 
Global variables don't disappear and are available to all functions 
running in the same shell.  They can even be modified.

-U or --universal
       Causes  the  specified shell variable to be given a universal scope. 
If this option is supplied, the variable will be shared between all the current 
user's fish instances on the current computer, and will be preserved across 
restarts of the shell

If you are running a current version of fish, you can use the built in fish_add_path (go env GOPATH)/bin once and it's done, you don't have to have it in a conf file.

Recognizee answered 8/9, 2022 at 18:50 Comment(2)
https://mcmap.net/q/143742/-modifying-path-with-fish-shell also see this solution.Recognizee
That one liner fish_add_path (go env GOPATH)/bin worked perfectly for me. I appreciate the long form of parameters being written out. That was nice & helpful.Cerebritis
B
2

To add Go paths to your Fish shell, add the following lines to your config.fish:

set -x GOPATH $HOME/go
set -x PATH $PATH $GOPATH/bin
Beading answered 11/9, 2022 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.