Lsp-mode: `lsp-go` will not import or remove unused package when formatting

Created on 10 Jan 2019  路  15Comments  路  Source: emacs-lsp/lsp-mode

Describe the bug
Previously in go-mode, when formatting the buffer, goimports will be called then auto import and remove unused packages.

but lsp-go will not.

I found that there is

(defcustom lsp-go-format-tool "goimports"
  "The tool to be used for formatting documents.  Defaults to `goimports' if nil."
  :type '(choice (const :tag "goimports" "goimports")
                 (const :tag "gofmt" "gofmt"))
  :group 'lsp-go)

but not working, and asked in bingo, it already supported the feature, but maybe a flag --format-style="goimports" is needed
https://github.com/saibing/bingo/issues/53

To Reproduce
main.go

package main
func main() {
    fmt.Println("Hello, world")
}

then M-x lsp-format-buffer and fmt is not imported.

Expected behavior
fmt is imported.

Which Language Server did you use
lsp-go, bingo

OS
macOS 10.13.6

Error callstack

>>> go-bingo:67166(sync)
Content-Length: 274

{
  "jsonrpc": "2.0",
  "method": "textDocument/formatting",
  "params": {
    "textDocument": {
      "uri": "file:///Users/user/code/golang/src/test/test.go"
    },
    "options": {
      "tabSize": 4,
      "insertSpaces": false
    }
  },
  "id": 492
}
<<<< go-bingo:67166
{
  "id": 492,
  "result": null,
  "jsonrpc": "2.0"
}

Most helpful comment

Based in comments in https://github.com/saibing/bingo/issues/53 you should add --format-style="goimports" flag in lsp-clients-go-language-server-flags. If you think this should be the default setting feel free to provide a PR.

All 15 comments

Based in comments in https://github.com/saibing/bingo/issues/53 you should add --format-style="goimports" flag in lsp-clients-go-language-server-flags. If you think this should be the default setting feel free to provide a PR.

Sorry to zombie this issue, but I'm experiencing the same problem. If something isn't imported I see the Add import: <package name> prompt on the right hand side but if I run lsp-format-buffer it doesn't import the missing package.

I tried adding (setq lsp-client-go-server-args '("--format-style=\"goimports\"") to my .emacs but nothing changed.

What language server are you using? gopls recently changed format buffer to no longer add imports. You need to also do lsp-organize-imports.

Oh cool! Ty for the super quick response. That set me down the right direction. I just added the following to my ~/.emacs

(add-hook 'before-save-hook #'lsp-organize-imports)
(add-hook 'before-save-hook #'lsp-format-buffer)

And now it imports and formats the buffer.

Edit: Forgot to mention I'm using gopls

Hmmm unforunately, this approach doesn't work 100% of the time. I have to manually run the commands. I wonder if its because I'm using lsp-deferred

Try changing the order of the two hooks.

I'll try that out today and let you report back how it goes.

(add-hook 'before-save-hook #'lsp-format-buffer)
(add-hook 'before-save-hook #'lsp-organize-imports)

works for me, but every time I save a file, it takes seconds.
looks like the cost is on counsel-M-x

Is this the expected time for lsp-organize-imports, or there is something wrong with my config?

BTW, I have tried emacs -q, it also takes seconds

It should be instantaneous (usually). I would make sure you are running the newest release of gopls and that you chose your project root correctly in Emacs.

weird 馃え

I have tried the 2 versioned gopls, no luck... I will dig into it when free, thanks for letting me know it should be instantaneous...

$ gopls version                                                                                            [11:38:28]
golang.org/x/tools/gopls v0.1.7
    golang.org/x/tools/[email protected] h1:YwKf8t9h69++qCtVmc2q6fVuetFXmmu9LKoPMYLZid4=

and

$ gopls version                                                                                            [13:47:47]
golang.org/x/tools/gopls 0.2.0
    golang.org/x/tools/[email protected] h1:ddCHfScTYOG6auAcEKXCFN5iSeKSAnYcPv+7zVJBd+U=

If you are using go-mode and goimports, you could try this snippet.

(setq gofmt-command "goimports")
(add-hook 'before-save-hook #'gofmt-before-save)

That would tell which one caused the issue, lsp-organize-imports or goimports itself.

@seagle0128 I changed the hook from goimports to lsp yesterday founding this issue commented.

both of it takes seconds, that's why I was considering the cost may be counsel-M-x

@zwpaper I don't think counsel-M-x is relevant. Perhaps the go project is huge? You should try goimport command directly. I guess it also will take a few seconds.

You are right, goimports takes seconds checking the file, I will deep dive into goimports, thanks

BTW, my project is not that huge, maybe it is because of the client-go and goswagger generated dependency.

@zwpaper The root cause should be in goimports. But you may be interested in this #1177 . If the saving actions are async, all issues are addressed.

Was this page helpful?
0 / 5 - 0 ratings