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

goimports is not used as the default format tool in Go module-enabled environment #2416

Closed
nezorflame opened this issue Apr 5, 2019 · 8 comments

Comments

@nezorflame
Copy link
Contributor

nezorflame commented Apr 5, 2019

This issue is created due to the discussion at #2410 (comment)

It seems like the goreturns is still being preferred instead of goimports for me in spite of change from #2309.
Removing "go.formatTool": "goimports" instantly makes the editor ask to install goreturns. This doesn't go away after the IDE restart or after I open a new project.

My setup:

  • macOS 10.14.4
  • Go 1.12.1
  • VSCode 1.33.0 (same happens in 1.32.3)
  • vscode-go 0.9.3-beta.7
  • GO111MODULE=on is globally set in my ENV in .zprofile, so modules are enabled

Warning:

warning

@nezorflame
Copy link
Contributor Author

nezorflame commented Apr 6, 2019

Also, when you open a Go file, the editor asks for the default formatter, but the options are non-descriptive:
1
2
Whatever you choose, it always writes this to the settings:

"[go]": {
    "editor.defaultFormatter": "ms-vscode.Go"
},

@nezorflame
Copy link
Contributor Author

@ramya-rao-a any info I'm missing which could help?

@ramya-rao-a
Copy link
Contributor

@nezorflame Apologies for the delay

Removing "go.formatTool": "goimports" instantly makes the editor ask to install goreturns. This doesn't go away after the IDE restart or after I open a new project.

When you remove the "go.formatTool": "goimports", you are telling the extension that you don't want to use goimports for formatting. Since the default is goreturns which the extension can't find on your machine, you get the prompt. This is the expected behaviour.

From this point, if I reload the VS Code window, then I get the prompt on using the gopls language server for better performance. If I don't agree to update, the extension adds the setting "go.formatTool": "goimports" back.

Below are the cases where this may not happen and instead one would get prompted about goreturns not being available

  • Not using Go modules
  • Using language server, but not the formatting feature from the language feature
  • Said "No" to the update to use gopls for modules

Did you land in any of the above cases?

@ramya-rao-a
Copy link
Contributor

Regarding the prompt on multiple formatters, can you share what your Go related settings were when you got that prompt?

Based on your screenshots it looks like 2 formatting providers were registered by the Go extension. I'd like to know the settings that lead to that

@nezorflame
Copy link
Contributor Author

nezorflame commented Apr 12, 2019

Thanks for clearing this out.

Did you land in any of the above cases?

Indeed, case 2 as in

Using language server, but not the formatting feature from the language feature

As for the prompt:

"go.autocompleteUnimportedPackages": true,
"go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
"go.docsTool": "godoc",
"go.formatTool": "goimports",
"go.formatFlags": [
    "--local=github.com/nezorflame"
],
"go.lintTool": "golangci-lint",
"go.lintFlags": [
    "--config=/home/nezorflame/.config/.golangci.yml"
],
"go.buildOnSave": "off",
"go.vetOnSave": "off",
"go.lintOnSave": "off",
"go.coverageDecorator": {
    "type": "gutter",
},
"go.coverageOptions": "showUncoveredCodeOnly",
"go.coverOnTestPackage": true,
"go.coverOnSingleTest": false,
"go.testFlags": [
    "-v",
    "-cover"
],
"go.addTags": {
    "tags": "json",
    "promptForTags": false,
    "transform": "snakecase"
},
"go.enableCodeLens": {
    "references": false,
    "runtest": true
},
"go.useLanguageServer": true,
"go.languageServerExperimentalFeatures": {
    "autoComplete": true,
    "diagnostics": true,
    "documentSymbols": true,
    "findReferences": true,
    "format": false,
    "goToDefinition": true,
    "goToTypeDefinition": true,
    "hover": true,
    "rename": true,
    "signatureHelp": true,
    "workspaceSymbols": true,
},
"[go]": {
    "editor.snippetSuggestions": "top",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    },
}

Setting the "format" to true doesn't show me any messages (I assume since gopls is automatically used as the default formatter), so I guess this happens due to the "format": false setting.

@ramya-rao-a
Copy link
Contributor

I've pushed a fix so that goimports is again chosen as the tool to use in the below cases

  • Using language server, but not the formatting feature from the language feature
  • Said "No" to the update to use gopls for modules

Setting "format" to false in go.languageServerExperimentalFeatures tells the extension to register the old formatting provider. But this doesn't tell the language server not to register its formatting provider. I believe this is the case where we end up with 2 formatters being registered and thus the prompt.

@ramya-rao-a
Copy link
Contributor

Logged microsoft/vscode#72315 to track the second issue

@ramya-rao-a
Copy link
Contributor

#2446 is tracking the issue of multiple formatters being registered.
Closing this issue as goimports is getting chosen as the formatting tool when using Modules

@vscodebot vscodebot bot locked and limited conversation to collaborators Aug 13, 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

2 participants