/ troubleshooting

Setup for Auditing Go Projects with Go Modules in VSCode

Go Get

$ go get -u github.com/foo/boo
# the root dir will be:
$ cd `go env GOPATH`/src/github.com/foo/boo

Go Modules

If the project is managed by go modules, there should be a go.mod rest in the root dir.

# download all the dependencies defined in go.mod. Set up proxy if you need: export all_proxy=localhost:8888
$ go mod download
# copy dependencies to vendor folder.
$ go mod vendor

Build

depends on the project, you may need go build or make build_all

Solving VSCode Doesn't Recognize Vendor Packages

When viewing the project in vscode, you might notice that some imported packages are not recognized by default, and marked with red squiggly lines. And apperantly you also couldn't jump to the definition code related with those packages.

Reason

No doubt that this is caused by missing proper path in GOPATH
Surely you can add path for each workspace, but there is a more elegent solution supported by default.

Solution

  • Code -> Preferences -> Settings -> Extensions -> Go configuration
  • Enable 'Infer Gopath'
  • Restart VSCode
    WeChatWorkScreenshot_14769c38-fadb-404b-820d-71dc8e83725f

Solving VSCode Can't 'Go to defination'

When viewing a project managed by gomod, you may experience that you can't 'jump into' the defination of a variable/function.
WeChatWorkScreenshot_80997ae3-1c6a-4662-9f54-9387e6ef94be
Error: No definition found for '**'

Reason

  1. There was a bug in godef-mod.
    You may find a similar issue here: https://github.com/microsoft/vscode-go/issues/2145, it suggests you update godef-gomod by 'Go: Install/Update Tools'

  2. VSCode removed supports for godef-gomod. So you won't even find the 'godef-gomod' option in 'Go: Install/Update Tools' if you are using newer VSCode version (mine is v1.38.1)
    As stated here: https://github.com/Microsoft/vscode-go/issues/2194

  3. VSCode use Language Server to provide supports for go modules.
    As stated in the official repo wiki:

VS Code uses a host of Go tools to provide features like code navigation, code completion, build, lint etc. These tools do not provide a good support for Go modules yet.
Code navigation and code completion definitely works better when using the language server from Google. So, please give that a try.
If you are not using the language server, then this is mostly due to the limitation of the tools that power these features which are godef and gocode respectively. The Go tools team at Google are working on a language server which will be the long term solution for all language features.

Solution

Make sure you've enabled the Language Server.
WX20190919-112026
Or check your VSCode settings:

"go.useLanguageServer": true

Then update gopls through 'Go: Install/Update Tools'.
Relaunch vscode.

(BTW)error: secp256k1.h No such file

If you are working with ethereum-related(geth or other things based on it) projects, you may encounter this error during compilation:

vendor/github.com/ethereum/go-ethereum/crypto/secp256k1/curve.go:43:44: fatal error: libsecp256k1/include/secp256k1.h: No such file or directory
 #include "libsecp256k1/include/secp256k1.h"

Solution

Reference: https://github.com/ethereum/go-ethereum/issues/2738#issuecomment-365239248
Work around:

cp -r \
  "${GOPATH}/src/github.com/ethereum/go-ethereum/crypto/secp256k1/libsecp256k1" \
  "vendor/github.com/ethereum/go-ethereum/crypto/secp256k1/"
Setup for Auditing Go Projects with Go Modules in VSCode
Share this