2

My vscode does not detect errors in golang.

Example:

package somepackage

import "fmt"

func f(name string) string {
    name = 1
    return name
}

This should throw a type error, but it does not. I never get any errors.

My settings.json contains

"go.gopath": "some/path", 
"go.vetOnSave": "package",
"go.lintOnSave": "package",
"go.testOnSave": true,
"go.buildOnSave": "package",
"go.coverOnSave": true 

I was able to run go: install/update Tools. All tools are installed successfully.

I am also able to run debug a .go file in vscode.

User12547645
  • 6,955
  • 3
  • 38
  • 69
  • 1
    Is your GOROOT path set? Also are there any other go files in the same project where there might an error? – pwaterz Apr 09 '19 at 00:52
  • Yes, I inserted `go.goroot` in my application. Apparently, I have two conflicting go installations on my computer. After recompiling the tools, vscode was able to detect errors. Thanks! Please submit your suggestion as an answer so I can close this issue. – User12547645 Apr 09 '19 at 06:49
  • I keep seeing "go: install/update tools" in documentation. Has this option been removed recently? I'm assuming it was an old menu setting Go > Install/update tools? – Duncan Jones May 29 '20 at 17:00

1 Answers1

6

As @pwaterz pointed out, the solution to my problem was to add "go.goroot: /some/other/path".

The reason that vscode was not able to detect errors was, that there are different go versions on my computer. Adding the goroot and running go: install/update Tools solved the problem.

---- Edit: Multiple go versions ----

Had multiple conflicting go versions on my Mac, pulled in via brew. Fixed the problem with a reinstall.

  • Uninstall go and also run brew uninstall go
  • Reinstall go
  • Set environment variables in your .bash_profile or similar. Compare here.
  • Apply the changes to your profile by running e.g. source .bash_profile
  • Restart VSCode
  • In settings.json set "go.goroot": "/usr/local/go"
  • Run go: Toggle workspace trust space to make sure changes to settings.json are applied (you have to trust your workspace for that)
  • go: Install/update tools and select all

---- Edit: Incorrect root folder ----

Make sure that you open the root folder of your project and not a sub-folder of your project. That may cause in invalid import paths otherwise

---- Edit: Broken language server ----

User12547645
  • 6,955
  • 3
  • 38
  • 69
  • I had this problem again. Be aware that you may need to `Go: Toggle Workspace Trust Flag` before running `go: install/update Tools`. Otherwise the change to `go.goroot` will be ignored. – User12547645 Jun 14 '21 at 09:44
  • Nice, running `Go: Install/Update tools` from the command palette worked for me. – insanely_sin Nov 26 '22 at 19:14