Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 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

This comment has been minimized.

Copy link

@tj tj commented Apr 25, 2019

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

@huguesalary

This comment has been minimized.

Copy link

@huguesalary huguesalary commented Apr 25, 2019

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

@csprl

This comment has been minimized.

Copy link

@csprl 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.

@arbent

This comment has been minimized.

Copy link

@arbent arbent commented Apr 25, 2019

try del "go.useLanguageServer" and reload goimports

@huguesalary

This comment has been minimized.

Copy link

@huguesalary huguesalary commented Apr 25, 2019

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

This comment has been minimized.

Copy link
Collaborator

@stamblerre stamblerre commented Apr 25, 2019

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

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

This comment has been minimized.

Copy link
Collaborator

@stamblerre stamblerre commented Apr 25, 2019

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

@huguesalary

This comment has been minimized.

Copy link

@huguesalary huguesalary commented Apr 25, 2019

@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

This comment has been minimized.

Copy link
Collaborator

@stamblerre stamblerre commented Apr 25, 2019

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

This comment has been minimized.

Copy link

@huguesalary huguesalary commented Apr 25, 2019

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/logrus@v1.4.1: 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

This comment has been minimized.

Copy link
Collaborator

@stamblerre stamblerre commented Apr 25, 2019

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

This comment has been minimized.

Copy link

@huguesalary huguesalary commented Apr 25, 2019

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/logrus@v1.4.1: parsing go.mod: unexpected module path "github.com/sirupsen/logrus"

@Hawken94

This comment has been minimized.

Copy link

@Hawken94 Hawken94 commented Apr 26, 2019

#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

This comment has been minimized.

Copy link
Member

@ramya-rao-a ramya-rao-a commented Apr 30, 2019

So to summarize,

  • When using gopls, adding of missing imports or removal of unused imports don't work on file save until the setting regarding source.organizeImports is added.
    • I have updated our README regarding this
  • Auto import of unimported packages do not work anymore on auto-completion.
  • Importing of a wrong package broke all features of gopls
    • @huguesalary At this point, did VS Code show any error at all?
    • @stamblerre This is a situation where the language server fails to provide any feature I guess. We will need to think of the best way to notify users about this. I believe the middleware is where we can add something to check for rejected promise from the language service and then do something appropriate. Thoughts?
@huguesalary

This comment has been minimized.

Copy link

@huguesalary huguesalary commented Apr 30, 2019

@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

This comment has been minimized.

Copy link

@zephinzer zephinzer commented Apr 30, 2019

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

@stamblerre

This comment has been minimized.

Copy link
Collaborator

@stamblerre stamblerre commented Apr 30, 2019

@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

This comment has been minimized.

Copy link
Member

@ramya-rao-a ramya-rao-a commented Apr 30, 2019

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

This comment has been minimized.

Copy link
Member

@ramya-rao-a ramya-rao-a commented Apr 30, 2019

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)

@fastreles

This comment has been minimized.

Copy link

@fastreles fastreles 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

This comment has been minimized.

Copy link
Member

@ramya-rao-a ramya-rao-a commented May 9, 2019

@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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
10 participants
You can’t perform that action at this time.