microsoft / vscode-go

An extension for VS Code which provides support for the Go language. We have moved to https://github.com/golang/vscode-go
Other
5.93k stars 648 forks source link

No longer adding imports automatically #2363

Closed Divni closed 5 years ago

Divni commented 5 years ago

As of a few weeks ago vscode-go is no longer auto adding imports. My formatter is set to goreturns, and when I manually run goreturns it works fine. Also it does format my go files, just doesn't add the imports.

It seems like there's some sort of environment issue.

To work around this I've installed https://github.com/emeraldwalk/vscode-runonsave, with the following config:

        "commands": [
            {
                "match": ".go",
                "cmd": "goreturns -w \"${file}\""
            }
        ]
    }

But this is less than idea. I only mention it because I would imagine this command runs with the same environment that vscode-go does its formatting with, and if it doesn't then maybe there's your bug?

ramya-rao-a commented 5 years ago
Divni commented 5 years ago
ramya-rao-a commented 5 years ago

If there are errors during formatting, then they will be logged as well.

From the View menu, select Output. Select Log (Extension Host) from the drop down on the top right corner of the panel. This will have logs from the extension host and errors from extensions. Clear it (there should be a button on the top right corner), try formatting again and see if anything is printed to the logs

Divni commented 5 years ago

I could swear I've been through these steps, but this morning while trying your suggestion I went in and toggled "format on save" on and on and now it appears to be working again. No errors were logged at any time. I'm not sure what went wrong, but it's working again now. Will report back if that changes, but until then it appears my problem is resolved. Thank you!

smallnest commented 5 years ago

I have met the same issue and I have configured "editor.formatOnSave to true.

image

If I click the "Format Document" on the context menu, the go file can be fotmatted and imports added.

go version go version go1.12 darwin/amd64

vscode-go configuration

   "go.buildTags": "\"reuseport quic kcp zookeeper etcd consul ping rudp utp\"",
    "go.buildFlags": [],
    "go.lintFlags": [],
    "go.vetFlags": [],
    "go.coverOnSave": false,
    "go.useCodeSnippetsOnFunctionSuggest": false,
    "go.autocompleteUnimportedPackages": true,
    "go.gotoSymbol.includeGoroot": true,
    "go.gotoSymbol.includeImports": true,
    "go.gocodeAutoBuild": false,
    "editor.formatOnSave": true,
    "go.formatTool": "goreturns",
    "go.vetOnSave": "package",
smallnest commented 5 years ago

The OUTPUT:

image
smallnest commented 5 years ago

It looks the error is caused by formatOnSaveTimeout #2336 , I have set it to 3000 and imported can be added automatically.

Please ignore my comments, thanks.