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

x/tools/gopls: format feature doesn't follow `goimports` #33587

Open
inliquid opened this issue Aug 11, 2019 · 3 comments

Comments

@inliquid
Copy link

commented Aug 11, 2019

For example, here is what imports block looks like in VS Code after pressing Alt-Shift-F (formatting) with enabled gopls:
изображение

And here is what it looks like after pressing Ctrl-S (forcefully save file):
изображение

For p.2, these options are enabled for gopls:

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

I'd like to have an ability to configure gopls to follow goimports style when formatting the file or to have this behavior as default.

Related:
If I remove fmt dependency from code, but leave it in imports section, formatting will not remove fmt:
изображение

@gopherbot gopherbot added this to the Unreleased milestone Aug 11, 2019

@gopherbot gopherbot added the gopls label Aug 11, 2019

@stamblerre

This comment has been minimized.

Copy link
Contributor

commented Aug 12, 2019

This is intentional. In gopls, formatting is separate from organizing imports (hence the two different settings). Formatting a file just means that we will run gofmt on it, whereas organizing imports means that we will run the equivalent of goimports. Our intention is to decouple the two behaviors.

If you would like to manually organize imports, you can run the organize imports source action (Ctrl + Shift + P -> Source Action -> Organize Imports). It will only be available if your imports are incomplete. Furthermore, if you see a diagnostic indicating that you are using a package that has not been imported, we will offer you a quick fix suggestion so that you can automatically import it (just hover over the place with the error and select "quick fix").

@stamblerre stamblerre closed this Aug 12, 2019

@inliquid

This comment has been minimized.

Copy link
Author

commented Aug 12, 2019

Maybe let others say their word instead of just closing all my issues? I believe you are very wrong with that approach. At least it's non-professional.

There was a setting both in Sourcegraph's go-langserver and in bingo. There is setting in VS Code which allows to select gofmt or goimports for formatting. Most of the gophers I know use goimports-like for formatting in VS Code and Goland (last one is on a cosmic distance in all that, which makes use of gopls ridiculous).

@stamblerre

This comment has been minimized.

Copy link
Contributor

commented Aug 12, 2019

I apologize for closing your issue prematurely, but you are welcome to continue commenting if you feel I have made a mistake. Triaging a large number of issues is a difficult task, and I typically prefer to close an issue if I feel I have answered a question.

As I explained in my comment, gopls uses both gofmt and goimports. The LSP specification separates out the behaviors of formatting a file and organizing the imports in a file, so we have chosen to split these behaviors along those lines. This explains the differences in the behavior of formatting and saving a file.

I have re-opened this issue so that we can continue the discussion. gopls is significantly different from previous Go tooling, so our goals here are not necessarily to make decisions that are in line with past behaviors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.