Lsp: gopls is not formatting or importing

Created on 18 Nov 2020  路  8Comments  路  Source: sublimelsp/LSP

Describe the bug
I am working on a file in a go path.

To Reproduce

I made a go file:

package main 

import (

)

func main() {
    fmt.Println("hello")
}

I save this file and I get an error from gopls:

compiler error undeclared name:fmt

Expected behavior
I thought that my go file would be autoformatted.

Environment (please complete the following information):

  • OS: Ubuntu 20.04
  • Sublime Text version: 3.2.2 Build 3211
  • LSP version: v0.14.2

Additional context
My LSP settings:

{
    "lsp_format_on_save": true,
    "lsp_code_actions_on_save": {
        "source.fixAll": true,
        "source.organizeImports": true
    },
    "show_symbol_action_links": true,
    "only_show_lsp_completions": true,
    "clients": {
        "gopls": {
            "enabled": true,
            "settings": {
                "gopls.usePlaceholders": true,
                "gopls.analyses": {
                  "fillreturns": true,
                  "nonewvars": true,
                  "undeclaredname": true,
                  "unusedparams": true
                },
                "gopls.completeUnimported": true,
                "gopls.staticcheck": true
            }
        }
    }
}
ST3 questioheldebug

Most helpful comment

I've tried in ST3 and I can see that formatting is triggering when the code has no errors otherwise.

The "organize imports" feature is apparently not part of the formatting functionality which means that in ST3 you have to fix issues manually through code actions.

All 8 comments

It's possible that your code has to be valid before it can be formatted. Does it work once you fix the errors?

It does not format once I fix that error.

Also, I'm surprised it doesn't fix that error itself because its something goimports will do..

For ST3, the "lsp_format_on_save": true, setting must be set in global settings and not in LSP settings.

(also lsp_code_actions_on_save is not supported in ST3 but that's not necessarily the problem here)

The error would actually be fixed by the source.organizeImports code action but running code actions on saving is only supported in ST4.

If you have a license for ST3 you can jump on Discord (https://discord.gg/TZ5WN8t) and grab a build from #announcements channel. The LSP experience is much better with it.

@rchl Okay I've upgraded to ST4 and no longer have the issue! It works beautifully too.

I have an old computer that I can try your fix on ST3 for future users

Okay, in ST3 I have this for my user preferences:

{
    "font_face": "Go Mono",
    "font_size": 10,
    "ignored_packages":
    [
        "Vintage"
    ],
    "lsp_format_on_save": true
}

and removed lsp_format_on_save from LSP settings, but it still doesnt work.

I've tried in ST3 and I can see that formatting is triggering when the code has no errors otherwise.

The "organize imports" feature is apparently not part of the formatting functionality which means that in ST3 you have to fix issues manually through code actions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arsham picture arsham  路  4Comments

ayoub-benali picture ayoub-benali  路  3Comments

JonasPf picture JonasPf  路  6Comments

lsmenicucci picture lsmenicucci  路  3Comments

toxinu picture toxinu  路  5Comments