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

Terminals don't set configured GOPATH environment variable #3522

Open
JohnstonJ opened this issue Sep 4, 2024 · 4 comments
Open

Terminals don't set configured GOPATH environment variable #3522

JohnstonJ opened this issue Sep 4, 2024 · 4 comments

Comments

@JohnstonJ
Copy link

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.23.0 windows/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    • golang.org/x/tools/gopls v0.16.1
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    1.92.2
    fee1edb8d6d72a0ddff41e5f71a671c23ed924b9
    x64
    
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.42.0
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
# Tools Configuration


## Environment

GOBIN: undefined
toolsGopath: 
gopath: C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\gopath
GOROOT: C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\goroot
PATH: C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\goroot\bin;C:\Program Files\PowerShell\7;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Program Files\PowerShell\7\;C:\Program Files\Docker\Docker\resources\bin;C:\Users\JohnstonJ\AppData\Local\Programs\Python\Launcher\;C:\Users\JohnstonJ\AppData\Local\Microsoft\WindowsApps
PATH (vscode launched with): C:\Program Files\PowerShell\7;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Program Files\PowerShell\7\;C:\Program Files\Docker\Docker\resources\bin;C:\Users\JohnstonJ\AppData\Local\Programs\Python\Launcher\;C:\Users\JohnstonJ\AppData\Local\Microsoft\WindowsApps

## Tools

	go:	C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\goroot\bin\go.exe: go version go1.23.0 windows/amd64

	gopls:	C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\gopath\bin\gopls.exe	(version: v0.16.1 built with go: go1.23.0)
	gotests:	not installed
	gomodifytags:	not installed
	impl:	not installed
	goplay:	not installed
	dlv:	not installed
	staticcheck:	C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\gopath\bin\staticcheck.exe	(version: v0.5.1 built with go: go1.23.0)

## Go env

Workspace Folder (dv-toolbox): c:\Users\JohnstonJ\Documents\Projects\dv-toolbox

	set GO111MODULE=
	set GOARCH=amd64
	set GOBIN=
	set GOCACHE=C:\Users\JohnstonJ\AppData\Local\go-build
	set GOENV=C:\Users\JohnstonJ\AppData\Roaming\go\env
	set GOEXE=.exe
	set GOEXPERIMENT=
	set GOFLAGS=
	set GOHOSTARCH=amd64
	set GOHOSTOS=windows
	set GOINSECURE=
	set GOMODCACHE=C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\gopath\pkg\mod
	set GONOPROXY=
	set GONOSUMDB=
	set GOOS=windows
	set GOPATH=C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\gopath
	set GOPRIVATE=
	set GOPROXY=https://proxy.golang.org,direct
	set GOROOT=C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\goroot
	set GOSUMDB=sum.golang.org
	set GOTMPDIR=
	set GOTOOLCHAIN=auto
	set GOTOOLDIR=C:\Users\JohnstonJ\Documents\Projects\dv-toolbox\.tools\goroot\pkg\tool\windows_amd64
	set GOVCS=
	set GOVERSION=go1.23.0
	set GODEBUG=
	set GOTELEMETRY=local
	set GOTELEMETRYDIR=C:\Users\JohnstonJ\AppData\Roaming\go\telemetry
	set GCCGO=gccgo
	set GOAMD64=v1
	set AR=ar
	set CC=gcc
	set CXX=g++
	set CGO_ENABLED=0
	set GOMOD=c:\Users\JohnstonJ\Documents\Projects\dv-toolbox\go.mod
	set GOWORK=
	set CGO_CFLAGS=-O2 -g
	set CGO_CPPFLAGS=
	set CGO_CXXFLAGS=-O2 -g
	set CGO_FFLAGS=-O2 -g
	set CGO_LDFLAGS=-O2 -g
	set PKG_CONFIG=pkg-config
	set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\JOHNST~1\AppData\Local\Temp\go-build2632435785=/tmp/go-build -gno-record-gcc-switches
	

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file.
Share all the settings with the go. or ["go"] or gopls prefixes.

{
    "go.gopath": "C:\\Users\\JohnstonJ\\Documents\\Projects\\dv-toolbox\\.tools\\gopath",
    "go.alternateTools": {
        "go": "C:\\Users\\JohnstonJ\\Documents\\Projects\\dv-toolbox\\.tools\\goroot\\bin\\go.exe"
    }
}

Describe the bug

I am using Go in a portable fashion - I've downloaded Go and put it into a subdirectory of my repo, along with creating a special GOPATH.

Unfortunately, terminals created in VS Code don't have the GOPATH environment variable properly set. They do have the PATH variable updated with the new $GOROOT\bin. But not the GOPATH.

My expectation is that the terminals should have the custom GOPATH as well.

Steps to reproduce the behavior:

  1. Set up a custom GOPATH by changing the go.gopath setting.
  2. Open a new terminal in VS Code: I am testing with both Command Prompt and PowerShell.
  3. Print the value of GOPATH, and observe the environment variable has not been set.
  4. Also observe that go env when run from inside the terminal will print the wrong GOPATH value.

Likely root cause

There is a function called updateIntegratedTerminal at

export async function updateIntegratedTerminal(terminal: vscode.Terminal): Promise<void> {
if (
!terminal ||
// don't interfere if this terminal was created to run a Go task (goTaskProvider.ts).
// Go task uses ProcessExecution which results in the terminal having `go` or `go.exe`
// as its shellPath.
(isTerminalOptions(terminal.creationOptions) &&
path.basename(terminal.creationOptions.shellPath || '') === correctBinname('go'))
) {
return;
}
const gorootBin = path.join(getCurrentGoRoot(), 'bin');
const defaultGoRuntime = getBinPathFromEnvVar('go', defaultPathEnv, false);
if (defaultGoRuntime && gorootBin === path.dirname(defaultGoRuntime)) {
return;
}
// append the goroot to the beginning of the PATH so it takes precedence
// TODO: add support for more terminal names
if (vscode.env.shell.search(/(powershell|pwsh)$/i) !== -1) {
terminal.sendText(`$env:Path="${gorootBin};$env:Path"`, true);
terminal.sendText('clear');
} else if (vscode.env.shell.search(/fish$/i) !== -1) {
terminal.sendText(`set -gx PATH ${gorootBin} $PATH`);
terminal.sendText('clear');
} else if (vscode.env.shell.search(/\/(bash|sh|zsh|ksh)$/i) !== -1) {
terminal.sendText(`export PATH=${gorootBin}:$PATH`, true);
terminal.sendText('clear');
}
}

It updates the PATH for the current GOROOT. But there doesn't appear to be any code for updating GOPATH. Perhaps it was simply never implemented?

Screenshots or recordings

image

@gopherbot gopherbot added this to the Untriaged milestone Sep 4, 2024
@hyangah
Copy link
Contributor

hyangah commented Sep 10, 2024

I think it's a reasonable request. On the other hand, there are many other GO* environment variables.
(for example, GOMODCACHE, GOPRIVATE, .. are some of the commonly modified Go environment variables.)
Should these be also applied to the terminals?

@TLCFEM
Copy link

TLCFEM commented Sep 10, 2024

I think it is a bug on Windows. I vaguely remember that I used to directly invoke go in ps.

Here on linux, PATH is correctly set in terminals.

Code_n9WZhYtvQm

But on Windows, PATH is not updated.

Code_twleohXIMi

@hyangah
Copy link
Contributor

hyangah commented Sep 11, 2024

@TLCFEM I think that is a separate issue. This issue is about GOPATH, not the Path/PATH handling. For your issue, can you please open a separate issue to follow up?

@JohnstonJ
Copy link
Author

I think it's a reasonable request. On the other hand, there are many other GO* environment variables. (for example, GOMODCACHE, GOPRIVATE, .. are some of the commonly modified Go environment variables.) Should these be also applied to the terminals?

It seems to me like any environment variable that is controlled by the extension's configuration might be worthy of consideration to set in the terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants