Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Add go tool directory to PATH when invoking tools #2514

Closed
FiloSottile opened this issue May 14, 2019 · 6 comments
Closed

Add go tool directory to PATH when invoking tools #2514

FiloSottile opened this issue May 14, 2019 · 6 comments

Comments

@FiloSottile
Copy link
Contributor

With go/packages, tools need to shell out to the go tool to do their job. It's important that this go tool is the same go tool used by the rest of the editor (which might be based on alternateTools, GOROOT, or PATH, see #1760).

When invoking any tool, the extension should prepend the directory containing the go binary to the PATH environment variable, so the tool will use that go binary.

(I initially reported this as a staticcheck issue when it broke in the standard library: dominikh/go-tools#460.)

@ramya-rao-a
Copy link
Contributor

Is this an issue just with staticcheck, or tools in general?
Isn't it a standard that the go binary lives in whatever the go env GOROOT is pointing to?
If so, then shouldn't tools respect GOROOT first and then look at the path?

@FiloSottile
Copy link
Contributor Author

This is going to be an issue with any tool based on go/packages, which should be all of them eventually.

Using go env GOROOT is a bit circular, you have to run go to get to go env GOROOT, so it's a bit weird to then change the go binary afterwards. (Like, do you rerun go env GOROOT with the new binary? What if it changes, do you keep going?)

In practice, one should prefer setting PATH so that the right go is first, rather than setting GOROOT, but there is no way to do that in settings.json, is there?

@ramya-rao-a
Copy link
Contributor

Agreed on the circular reference issue if the suggestion was the tools use go env to get the GOROOT, I just assumed that there was a way to get that information without running go first

How about the tools using the value set in the environment variable GOROOT? Shouldn't that work? If yes, then you can use the go.goroot setting in VS Code. The value passed to this setting is used to set the environment variable GOROOT when shelling out any of the Go tools used by this extension.

In practice, one should prefer setting PATH so that the right go is first, rather than setting GOROOT, but there is no way to do that in settings.json, is there?

There are a couple of ways one could do this

  • set the right PATH outside of VS Code, so that when you run VS Code, it inherits the right values
  • use go.toolsEnvVars setting. Values set here are set as env vars every time the Go plugin shells out to call one of the tools. Need to verify if something like my-path-to-go:${env:PATH} would work. If it doesn't, then that is an easy fix to make

@FiloSottile
Copy link
Contributor Author

FiloSottile commented May 17, 2019 via email

@ramya-rao-a
Copy link
Contributor

I was digging around the code and found that we were already appending the value of GOROOT (as defined by env variable or the go.goroot setting) + /bin into the PATH variable when shelling out Go tools. We did this because cgo expected the go binary to be in the PATH, exactly the same issue as you are seeing.

I have updated this to instead use whatever is the path the extension uses to run the Go binary. This way, we respect the alternate tools value.

Can you try the latest beta version (0.10.3-beta.3) and see if it works as expected for you?

@ramya-rao-a
Copy link
Contributor

This bug fix is now available in the latest update (0.11.0) to the Go extension.

@vscodebot vscodebot bot locked and limited conversation to collaborators Aug 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants