lukas-reineke / lsp-format.nvim

A wrapper around Neovims native LSP formatting.
559 stars 27 forks source link

Completely stopped working on latest master (v2.3.2) #39

Closed Mange closed 2 years ago

Mange commented 2 years ago

Hi,

I'm not even sure how to debug this properly or where to start.

I pulled the latest version and now I can't format anymore, on any LSP.

Symptoms:

Here's my config:

-- packer
  use "lukas-reineke/lsp-format.nvim"
local lspformat = require("lsp-format")

-- …

  lspformat.setup()
  -- lspformat.setup {
  --   typescript = {
  --     exclude = { "tsserver" },
  --   },
  --   lua = {
  --     exclude = { "sumneko_lua" },
  --   },
  --   ruby = {
  --     -- Use standardrb via null-ls instead.
  --     exclude = { "solargraph" },
  --   },
  -- }
  -- lspformat.disable { args = "markdown" }
  -- lspformat.disable { args = "vimwiki" }
  -- lspformat.disable { args = "eruby" } -- completely breaks in most formatters

-- LSP servers configured like this:
-- { on_attach = on_attach }

local function on_attach(...)
  lspformat.on_attach(...)
  on_attach_without_formatting(...)
end

I use null-ls for most formatting stuff.

Running nvim nightly NVIM v0.8.0-dev+27-g5c4ec25478

Mange commented 2 years ago

I went to my checked out package and jumped between different versions.

It broke completely on v2.3.0, same symptoms as listed in description.

The version before it, v2.2.3 still works, but only when formatting a saved changed buffer. Running :Format on an unchanged buffer does nothing. Seems this is a separate problem.

lukas-reineke commented 2 years ago

can you make a minimal init.lua file that reproduces this?

bmulholland commented 2 years ago

I just tried installing for the first time and formatting using this plugin is not working for me either. Using nvim 0.7 stable, though.

bmulholland commented 2 years ago

Also confirm that this PR broke :Format: https://github.com/lukas-reineke/lsp-format.nvim/pull/33/files. If you've got any debug statements I can try adding to help, happy to tell you the result. I don't have time to make a minimal repro, sorry.

lukas-reineke commented 2 years ago

Alright, let's try to add some logging to debug then.

The first obvious ones are here https://github.com/lukas-reineke/lsp-format.nvim/blob/2fcb2c99d8a833190d7bb577ed4ec4a304926300/lua/lsp-format/init.lua#L166 log what is send to the server

And here https://github.com/lukas-reineke/lsp-format.nvim/blob/2fcb2c99d8a833190d7bb577ed4ec4a304926300/lua/lsp-format/init.lua#L124 the result that comes back

bmulholland commented 2 years ago

I figured it out, at least for me. What has changed is the behaviour when LSP on_attach is not properly configured. The fix is to properly attach lsp-format to your LSPs. (FWIW, the requirement to do careful configuration didn't jump out at me, and I just blindly copy/pasted the default config, go and all 🙈)

Specifically, if an LSP is missing the require("lsp-format").on_attach(client) requirement, as mine was, then on commit db45b37 autoformat will not work (and perhaps never did). :Format will, though.

The commit that "broke" :Format is actually 4ba2f95f78af8b2d4d99c8ebe32074c529101b32 (I bisected). And this makes sense, considering that commit says "Don't format with client if on_attach was not called for that client"

Thanks for your time @lukas-reineke, appreciate the debug help (and the plugin!)

lukas-reineke commented 2 years ago

👍 Yes the behavior changed, but I consider it a bug that this worked before. To be used for formatting, each LSP now needs to be attached individually. Before it used all servers once it was called for one server on the buffer.

bmulholland commented 2 years ago

I consider it a bug that this worked before

Me too :). Thanks again.

Mange commented 2 years ago

I created a badly formatted shell script and tried to :Format it with some logging added on the previously mentioned lines. Here's what I got:

-- client.request call
{
  bufnr = 1,
  handler = <function 1>,
  method = "textDocument/formatting",
  params = {
    options = {
      insertSpaces = true,
      tabSize = 2
    },
    textDocument = {
      uri = "file:///home/mange/Projects/dotfiles/foo.sh"
    }
  }
}

-- _handler being called with
{
  ctx = {
    bufnr = 1,
    client_id = 1,
    method = "textDocument/formatting",
    params = {
      options = {
        insertSpaces = true,
        tabSize = 2
      },
      textDocument = {
        uri = "file:///home/mange/Projects/dotfiles/foo.sh"
      }
    }
  },
  err = {
    code = -32601,
    message = "Unhandled method textDocument/formatting",
    <metatable> = {
      __tostring = <function 1>
    }
  }
}

I have two LSPs for shells: bashls and null-ls. bashls does not handle formatting, so the error makes sense. However, I would expect the handler and formatting to be called again for null-ls, but there's only this one call.

I then tried disabling bashls and restarted so only null-ls was attached. If I run :Format now nothing is logged at all.

By stepping through all the conditionals I figured out that the problem was that M.buffers were empty.

…aaaand, here we are. Turns out I set up on_attach on all LSP servers but null-ls, since it was placed in a different section much further down and I forgot to check it.

So another one one the pile; misconfiguration in the PEBKAC module. :sweat_smile: