How to set Go Proxy in VS Code?
If you have a private repository for Go modules, this is how to configure VS Code with GOPROXY env variables.
Updated: 14th May 2021 with GUI option in VS Code
A pretext to this article, I’m not going to talk about how to set up a proxy for your Go Modules. If that’s what you are after please go here.
Not all code you write will be Open Source meaning we need private packages. When using Go Modules, Go will look for the URL location, if this cannot be found you may be familiar with this error
Error loading workspace: err: exit status 1: stderr: go: townsy.private-github.com/me/sdk-go@v0.0.17: module lookup disabled by GOPROXY=off : packages.Load error
This means Go wants to look in an alternative location for the package, but there is no proxy enabled.
If you have a Proxy already running you need to tell Go to use that Proxy. This is how to configure VS Code to use your proxy
Option 1 — Least preferred
Simplest way to do this is to globally enable your proxy, just add the following to your shell settings file e.g. `.zshrc` Please note your values will be different depending on your configuration.
export GOPRIVATE=townsy.private-github.com
export GOPROXY=https://athens-proxy.townsy.io
export GONOPROXY=none;
export GONOSUMDB=townsy.private-github.com
This method works, but if you are like me and use the same IDE/machine for various workflows, you may need to use different proxy’s on other projects, or not a proxy at all.
Option 2 — Preferred method
In VS Code it is possible to set your Workspace settings on a project by project basis. In your settings file set the following
{
"go.toolsEnvVars": {
"GOPRIVATE": "townsy.private-github.com",
"GOPROXY": "https://athens-proxy.townsy.io",
"GONOPROXY": "none;",
"GONOSUMDB": "townsy.private-github.com"
},
"terminal.integrated.env.linux": {
"GOPRIVATE": "townsy.private-github.com",
"GOPROXY": "https://athens-proxy.townsy.io",
"GONOPROXY": "none;",
"GONOSUMDB": "townsy.private-github.com"
}}
This will ensure VS Code uses these environment variables when preforming `go get` requests etc. I also added configuration to use the same environment variables in local terminals to VS Code. For Windows use "terminal.integrated.env.windows"
Option 3 — Same as above using GUI
It is possible to access the above env variables by using the GUI options in VS Code.
Start by opening up the IDE Preferences/settings and in the search bar type go
Scroll down until you get to Go: Tools Env Vars
and click Edit
You’ll then be taken to the env vars where you can save your proxy settings
If this helped you out please give a clap or a comment. Took me a while digging for this info.
Over and Out
Chris