LSP formatting doesn't always work

As I shared previously on reddit, I was using GitHub - mhartington/formatter.nvim (works great) & then decided to give LSP formatting a try, instead of having too many configs LSP, LSP for linters (efm, null-ls, etc…) & formatting.

After setting it up autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync(), it doesn’t always run & I tried it with both efm & null-ls so I don’t think it’s a plugin issue.

I tried all of these or variants of them

vim.lsp.buf.formatting()
vim.lsp.buf.formatting_sync()
vim.lsp.buf.formatting_seq_sync()

Also tried with timeouts like vim.lsp.buf.formatting_sync(nil, 2000) etc…

The only way was to always run LspRestart which is a bit annoying to do every time you want to save a file. So I wonder how to solve this issue? because it’s a deal-breaker for me.

When you see the issue, what’s the output of :LspInfo? Perhaps it’s the bug where clients are randomly getting detached from buffers? (See Prevent detaching LSP Clients)

What do you see in the logs? Can you try setting the log level to trace and see if you see the requests going out to the server?