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

Autoimport not working #2473

Closed
asdine opened this issue Apr 25, 2019 · 21 comments
Closed

Autoimport not working #2473

asdine opened this issue Apr 25, 2019 · 21 comments

Comments

@asdine
Copy link

asdine commented Apr 25, 2019

Since version 0.10.0 (0.10.1 included), autoimport doesn't work anymore.
This happens in a regular GOPATH as well as using go modules.

I am using Visual Studio Code Version: 1.33.1 on Ubuntu 18.04.2 LTS.
I have selected goimports in the settings, but it doesn't work with goreturn either

@tj
Copy link

tj commented Apr 25, 2019

ditto, auto-import and auto-complete for unimported packages fail for me

@huguesalary
Copy link

Both of those work for me when the gopls language server is disabled

@csprl
Copy link

csprl commented Apr 25, 2019

Yeah, it looks like it's related to gopls. Disabling it (Settings -> Use Language Server) solved it for me too.

@dkqkxx
Copy link

dkqkxx commented Apr 25, 2019

try del "go.useLanguageServer" and reload goimports

@huguesalary
Copy link

Looking into the vs-code source, and at the settings sections here's what I gathered:

1- When using the language server (gopls), the format tools are disabled:

image

So, it makes sense that goimports won't work.

2- Now, enabling the language server isn't sufficient. It needs to be configured, and that part is not easily found in the documentation.

You need to make sure to use this configuration(from https://github.com/golang/go/wiki/gopls#editors-instructions) to get the auto-import feature back:

"go.useLanguageServer": true,
"go.languageServerExperimentalFeatures": {
        "diagnostics": true // for diagnostics as you type
},
"[go]": {
    "editor.snippetSuggestions": "none",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    },
},
"gopls": {
    "usePlaceholders": true, // add parameter placeholders when completing a function
    "enhancedHover": true,   // experimental to improve quality of hover (will be on by default soon)
}

Now, unfortunately, on my end, using this configuration restores the import feature, but it breaks the autocompletetion, as seen here:

image

@stamblerre
Copy link
Contributor

For the goimports/gofmt behavior, you need to configure this setting:

"[go]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
}

@stamblerre
Copy link
Contributor

@huguesalary: imports should not be related to autocompletion. Does autocompletion work when you turn off the setting?

@huguesalary
Copy link

@stamblerre The auto completion broke when I enabled the language server, but, disabling the language server did not bring back the auto-completion. Meanwhile, the reformatting also broke.

I'm totally unclear about what is going on.

@stamblerre
Copy link
Contributor

Can you confirm that the language server is running? Go to "View: Debug Console" -> "Output" tab -> "Tasks" drop down, and check that there is an item titled gopls. You should be able to see more debug information there. Remember that you have to reload VSCode every time you change language server settings.

@huguesalary
Copy link

I figured it out.

The language server was correctly running.

The issue was that the autoimport feature imported github.com/Sirupsen/logrus (note the capital S). This import in turn was breaking, all the go tools with an error go: github.com/Sirupsen/[email protected]: parsing go.mod: unexpected module path "github.com/sirupsen/logrus"

This error was completely invisible as far as vscode is concerned. It simply was breaking, but not showing any information, not even in the debug console.

Fixing this import fixed my issue.

@stamblerre
Copy link
Contributor

Glad you were able to resolve your issue, and I'm sorry the errors weren't showing up. We're going to try and do a better job of showing errors to the user - you can track this on golang/go#31668.

@huguesalary
Copy link

Oh, I don't think the error reporting issue is a gopls one. I did get gopls errors in the console:

cacheAndDiagnose: publishing diagnostics
[Error - 11:01:26 AM] Request textDocument/documentSymbol failed.
  Message: no file information for file:///Users/hugues/Documents/bbctl/pkg/cmd/helpers.go
  Code: 0 

The issue, I think, was vscode not showing me at any moment the other error (and cause of everything else):

go: github.com/Sirupsen/[email protected]: parsing go.mod: unexpected module path "github.com/sirupsen/logrus"

@Hawken94
Copy link

#2469 this issue has an solution.

jellevandenhooff said,

This happens to me both go.useLanguageServer and without. It started happening recently. It make autocompleteUnimportedPackages impossible to use. Reverting to 0.9.2 makes the problem disappear.

@ramya-rao-a
Copy link
Contributor

So to summarize,

@huguesalary
Copy link

@ramya-rao-a the only visible error was in the output tab of vscode, under the gopls selector.

image

And the error was:

cacheAndDiagnose: publishing diagnostics
[Error - 11:01:26 AM] Request textDocument/documentSymbol failed.
  Message: no file information for file:///Users/hugues/Documents/bbctl/pkg/cmd/helpers.go
  Code: 0 

@zephinzer
Copy link

Same problem, switched back to 0.9.2 and it started working again

@stamblerre
Copy link
Contributor

@huguesalary and @zephinzer: do you mind each filing issues in https://github.com/golang/go/issues? I can help you debug it there.

@ramya-rao-a
Copy link
Contributor

In the latest beta version of this extension,

  • The source.organizeImports code action is enabled by default for Go files. So, no setting needs to be added to get missing imports or remove unused imports on file save when using gopls
  • Auto-import during auto-completion should be fixed as well.

If anyone is seeing issues after enabling gopls, please log a new issue at https://github.com/golang/go/issues, share any Go related settings you have and the logs from View -> Output -> gopls

Closing this issue as most of the concerns have been addressed. Please log new issues for the rest

@ramya-rao-a
Copy link
Contributor

The fix to make the setting in #2473 (comment) a default so that users don't have to set them has been released in the latest update (0.10.2)

@fr-les
Copy link

fr-les commented May 8, 2019

I have seen this issue repeatedly, since the functionality was first introduced. It comes and goes over time (on both Mac and Windows). Sometimes it works with goimports but not goreturns, and sometimes it doesn't. I used to try debugging it, but after the Nth time, I just gave up and accepted that it's faster and less stressful not to worry about it and type the imports I need. Feeling renewed curiosity today, I tried the fix in #2473. It worked for me this time, but only after switching to goimports AND disabling the language server.

@ramya-rao-a
Copy link
Contributor

@fastreles The latest goimports definitely works well with modules. goreturns does not. This is a known issue and golang/go#24661 is tracking the work to get module support for various tools.

The language server gopls also works well in adding missing imports, removing unused imports on file save. If you don't see that happening, please log an issue at https://github.com/golang/go/issues. We can use all the feedback we can get to keep improving the language server

@microsoft microsoft locked as resolved and limited conversation to collaborators May 9, 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

10 participants