set $GOPATH for a vscode project / terminals - go

I want to set $GOPATH for each vscode project/workspace. Right now, in .vscode/settings.json, I have:
{
"go.gopath": "$HOME/codes/huru"
}
I close vscode and reopened, and at the command line terminal, I echo $GOPATH, and it's empty. I was hoping that vscode would read the env variable from "go.gopath", but it seems not have to done so.
Does anyone know how to do this?

The go.gopath on user settings or workspace settings will replace the GOPATH value on the VSCode. This particular GOPATH value is the one that shows up whenever Go: Current GOPATH command is executed on VSCode, so it is not the $GOPATH environment variable.
The go.gopath value will not replace the $GOPATH environment variable.
Explanation from GOPATH in the VS Code Go extension:
Out of the box, the extension uses the value of the environment variable GOPATH. From Go 1.8 onwards, if no such environment variable is set, then the default GOPATH as deciphered from the command go env is used.
Setting go.gopath in User settings overrides the GOPATH that was derived from the above logic. Setting go.gopath in Workspace settings overrides the one from User settings. You can set multiple folders as GOPATH in this setting. Note that they should be ; separated in Windows and : separated otherwise.
Below is an example I've made up that might be useful on trying to understand the differences.
For example, I already set the $GOPATH env on my local with a certain value. Then I set the go.gopath on the user settings (with different value compared to the $GOPATH). When I execute the command Go: Current GOPATH, a popup on the bottom right appears, showing the very same value as on my go.gopath settings. I put red line on all of this.
But, whenever I execute shell command echo $GOPATH on the terminal, the output is still the $GOPATH value from my env variable (blue line). This is because go.gopath setting will not replace the $GOPATH env variable.
In your case, the echo $GOPATH return empty output because you haven't set the $GOPATH environment variable.

Related

System variables aren't evaluated in Windows command prompt, user variables are

I would like to be able to cd into a folder that is added to a system as a system variable in Windows 11. Here is a visual representation of the problem:
As you can see, windows command prompt is able to "resolve" variables from the "User variables" section of the Environment variables configuration in Windows, however variables from the "System variables" aren't.
I need to cd into %VS140COMNTOOLS% folder but as you can see it won't be resolved by the command prompt. Is this by design?
VS140COMNTOOLS also doesn't appear in set output.
How can I navigate into %VS140COMNTOOLS%?
System variables and user vars end up in a single environment block. When expanding env vars, there's no concept of user/system. User vars override system vars.
Also: each process gets its own copy of env vars which is resolved when the process launches. Therefore, if a process appears to be "missing" an env var, one of the following happened:
The variable was set after the process launched. For example maybe this command prompt was open during the installation of something that created an env var. You'll need to re-launch the command prompt to get the var.
OR, the process modified it / removed it.

How do I change the name conda displays in parantheses?

I have a conda environment that I created with a path (conda create -p /full/path/to/env). When I activate the environment it displays the full path to the environment in parentheses at the beginning of the command prompt line. like
(full/path/to/env) [username#server]
I want to change it so it just shows env instead of full/path/to/env. How do I do that?
I tried adding the directory leading up to my conda environment (i.e. full/path/to) to my env_dirs configuration using conda config but that didn't work.

GOPATH environment variable not getting set in the Fish shell?

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

Using environment variables when adding paths on MacOS

I've found myself with the need to add a new path permanently in all terminal sessions on my Mac. Specifically I want to add the contents of my $GOPATH/bin to my $PATH.
So far I think my options are to either:
Add it to my $HOME/.bash_profile file using export syntax.
Create a file containing the path to add in the /etc/paths.d directory.
I've settled on option 2, because I like the idea of just adding files with one line in whenever I want a new path added permanently.
I have tried adding in a file /etc/profile.d/gopath containing ~/code/go/bin. This works. However, what I'd like to do is evaluate the environment variable, $GOPATH/bin such that if I decide to change my $GOPATH I only have to change the variable. However, that just adds the literal words "$GOPATH/bin" to my path, it doesn't actually add the directory to my path. The $GOPATH bash environment variable is currently set in my ~/.bashrc file.
Some questions:
Why doesn't the $ syntax evaluate in the $PATH or setting of $PATH? Is that not bash?
What comes first, the inclusion of ~/.bashrc, ~/.bash_profile or /etc/profile.d? It is reasonable of me to think that the environment variable would be there when setting the $PATH?
How can I have this environment variable be evaluated and substituted if it is feasible?
Thanks for your help. All my searches don't seem to come up with the above answers.

-bash: set: -g: invalid option when I setting GOPATH

When I set my GOPATH use:
set -gx GOPATH /usr/local/Cellar/go/1.8.1
I get this issue:
-bash: set: -g: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
The bash command set doesn't support the g option. Also this command is not used for setting environment variables all together - your snippet is probably intended for a different shell (fishshell?).
In bash, use export as suggested:
export GOPATH /usr/local/Cellar/go/1.8.1
However, you should understand what you are doing and how to configure your environment on MacOS (guessing from 'Cellar' in your path).
This might be a good starting point.
You should set GOROOT environment variable instead of GOPATH.
GOROOT should reference a folder (where go is installed), not the go executable itself
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
GOPATH should reference a folder under which you will find src, pkg and bin. (it should not reference directly the src folder):
See "How to Write Go Code - Workspace"
Regarding the GOPATH:
try and set it in your ~/.bashrc (using export).
check that your current shell is a bash (and not another one like fish)
check the output of go env.

Resources