Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Golang Intellisense slow in VS Code when changing imports?

I have the ms-vscode.go Go extension installed in my VS Code setup and am using the gopls language server. The language server seems to perform well with Intellisense operations except when I am editing imports, at which point there is considerable lag as every edit to the import takes several seconds to update.

For example, the following is a replay of typing in manually import "net/http" letter-by-letter (rather than copy/paste). The clip runs at 20x speed, so it takes about 1.8 minutes from when I stop typing the import statement to when the language server gets to the correct error of "net/http" imported but not used:

enter image description here

Am I doing something wrong?

My go-related settings:

  "go.useLanguageServer": true,
  "go.alternateTools": {
      "go-langserver": "gopls"
  },

Output from gopls reveals that much time is spent in go list:

4.037297s for ...go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" ...

for every change.

like image 805
tbhartman Avatar asked Sep 12 '19 13:09

tbhartman


People also ask

Why is my VSCode so laggy?

You might have extensions installed that slow Visual Studio down. For help on managing extensions to improve performance, see Change extension settings to improve performance. Similarly, you might have tool windows that slow Visual Studio down.

Is VS code good for go?

In the Go Developer Survey 2020 Results, 41% of respondents chose Visual Studio Code as their most preferred editor for Go. This makes Visual Studio Code the most popular editor for Go developers. Visual Studio Code and the Go extension provide IntelliSense, code navigation, and advanced debugging.


1 Answers

That might be because:

  • go tools does not yet support autocompletion of unimported packages #31906: issue 31906: resolved in Jan. 2020.
  • saibing/tools does.

Before Jan. 2020 and gopls 0.3.0, You could try and see if the issue persist with saibing/tools, using Go 1.13 in module mode.

git clone https://github.com/saibing/tools
cd tools/gopls
go install

Make sure your ~/go/bin (using the default GOPATH) does show a new gopls executable with a recent timestamp.

Relaunch VSCode then.

Note: microsoft/vscode-go issue 2484 ("Go: Autocomplete Unimported Packages" feature when using Go modules") just got resolved (Jan. 2020) too:

Enabling the setting go.autocompleteUnimportedPackages used to provide completions for unimported packages.
When such a completion item was selected by the user, not only was the current word completed, but an import statement is added in the file for that package.

This now works with Go modules.

Again, gopls 0.3.0 should help.


My gopls settings:

"gopls": {
    "usePlaceholders": true, // add parameter placeholders when completing a function
    "enhancedHover": true, // experimental to improve quality of hover (will be on by default soon)
},

But also:

"[go]": {
    "editor.snippetSuggestions": "none",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    },
},
"go.lintTool": "golangci-lint",
"go.useLanguageServer": true,
"go.languageServerExperimentalFeatures": {
    "format": true,
    "autoComplete": true
},
like image 179
VonC Avatar answered Nov 15 '22 03:11

VonC