1

I am using M2, and use brew installed go 1.20. Now I want to switch Go version in VSCode:

Choose Go Environment in VSCode

But it failed with this warning message:

Switching Go version when process.env["GOROOT"] is set is unsupported.

warning message

After removing GOROOT from .zshrc, GOROOT has been disapeared from env. But it still exist in go env. How can I remove GOROOT from go env? I also tried to remove it from ${HOME}/Library/Application Support/go/env, but I still can not switch Go version.

➜  ~ env |grep GOROOT
➜  ~ go env GOROOT
/usr/local/go
Zeke Lu
  • 6,349
  • 1
  • 17
  • 23
lowang
  • 29
  • 4
  • 1
    How do you switch go version? Where does this `GOROOT is set` error come from? Does `go env -w GOROOT=""` work for you? – Zeke Lu May 22 '23 at 14:17
  • Do not install Go via brew and do not change versions. – Volker May 22 '23 at 16:05
  • @ZekeLu go env -w GOROOT="" doesn't work, after do that, I check it in "${HOME}/Library/Application Support/go/env" and shows GOROOT="". but go env still has GOROOT set. I don't know where GOROOT is set, maybe brew install set it. Now I have to set "go.goroot" in settings.json to change the go version. – lowang May 23 '23 at 15:15
  • @ZekeLu switch go version steps: Use the button bar(which shows go version) at the bottom, then select "Choose Go Environment" in the top windows. – lowang May 23 '23 at 15:19
  • @Volker It works fine in my working macbook, I usally switch go version in vscode for different go projects . – lowang May 23 '23 at 15:22
  • @lowang I have uploaded images captured on my computer. But it's not from a macOS. You can edit the answer to replace the images. – Zeke Lu May 24 '23 at 10:01

1 Answers1

1

Choose Go Environment is a feature of the vscode-go extension (see Managing Your Go Version).

If after removing GOROOT from .zshrc, the output from env | grep GOROOT is empty, then you have done the right thing to make the feature work.

Bear in mind that the change won't be observed by VSCode immediately. It's required to restart VSCode. The tricky thing is, if VSCode is started by a process that has GOROOT set, VSCode will still see it. So maybe it's better to restart the system to be safe.


Now I have to set "go.goroot" in settings.json to change the go version.

You should remove this configuration after restart VSCode or reboot the system. Otherwise, you will get this warning:

Switching Go version when "go.goroot" is set is unsupported.

But it still exist in go env. How can I remove GOROOT from go env.

This is expected. You don't need to, and can't, remove it from the output of go env.

go env GOROOT shows the effective setting of GOROOT. It shows the value from one of the following sources (higher priority first):

  1. the path inferred from the current go command. For example, if you have go1.19 installed with go install golang.org/dl/go1.19@latest, running go1.19 env GOROOT will give you the path like $HOME/sdk/go1.19.
  2. the GOROOT environment variable;
  3. the value set in the Go environment configuration file (go env GOENV shows the path to the configuration file).
  4. the path inferred from the go command found in the PATH parts (this is a little confusing compared to point 1 above).
Zeke Lu
  • 6,349
  • 1
  • 17
  • 23
  • 1
    Yes, it works now after removing `go.root` from settings and restart vscode. – lowang May 24 '23 at 14:46
  • Hi, @Zeke Lu, I also met the issue that "Choose go environmen" does not take effect. It still the go version which is at /usr/local/bin/go. /usr/local/bin/go is the link to go installed via brew. Although I have changed the link /usr/local/bin/go to point to anothe go bin, it still does not take effect use "choose go environment" in vscode after reload vscode. – lowang Jul 11 '23 at 10:42
  • Hi @lowang, I think your use case is to select a special go version for a special project. In this case, I think it's better to add `"go.goroot": "/path/to/the/special/go/root"` to the `.vscode/settings.json` file. You can find the `GOROOT` for the go version you want with this command: `go1.17.11 env GOROOT` (assumes that you want to find `GOROOT` for `go1.17.11`). – Zeke Lu Jul 11 '23 at 11:37
  • If it does not work, please provide the steps guide about how to reproduce the issue. – Zeke Lu Jul 11 '23 at 11:40