expert icon indicating copy to clipboard operation
expert copied to clipboard

Formatting destroys the buffer

Open mhanberg opened this issue 4 months ago • 11 comments

I noticed that sometimes, when you format (in this case on save), it completely clobbers the text in the buffer.

Prior to this, i noticed I was reporting a diagnostic error that was not accurate (i'm pretty sure).

In this video, I save (nothing happens), then reload the buffer by doing :e, then save again and it destroys it.

https://github.com/user-attachments/assets/7e7ba459-2c55-4390-a3e6-3263a9e182a8

mhanberg avatar Aug 29 '25 16:08 mhanberg

This is very annoying bug... I also have it with current elixir-ls... Sometimes I need to totally restart neovim or it will keep removing random chunks from the code forever whenever it saves and tries to format.

Maybe it's more related to the formatter or treesitter than the lsp itself... nevertheless, I'm sad to see that it is still present.

halian-vilela avatar Aug 29 '25 16:08 halian-vilela

Are you seeing it be completely destroyed or just the parentheses problem that ElixirLS has had?

mhanberg avatar Aug 29 '25 16:08 mhanberg

Completely destroys... I'll try to get some screenshot the next time it happens...

To illustrate: it's just like it adds a lot of indentation and random spaces between random characters...

Sometimes, to get back to normal I need to restart LSP 2 or 3 times in a row...

I don't know exactly what triggers it, but I observed that it probably has to do with some comments in locations that the formatter may get confused like between case statements; or just over some fn definitions. Stuff like that...

halian-vilela avatar Sep 02 '25 01:09 halian-vilela

I am wondering if this is related to the introduction of GenLSP and the code Lexical used to translate the utf8 and utf16 code positions.

mhanberg avatar Sep 04 '25 15:09 mhanberg

Possibly the same issue: In NeoVim, I just wrote alias GridPoint.Schedules.WeeklyScheduleFactory and saved, and it was changed to alias GridPoint.Schedules.WeeklySchedu()leFactory. I undid and saved again and it happened again. I quit out of NeoVim and reopened the same file and it stopped doing that.

I've seen other such mangled text.

nathanl avatar Sep 04 '25 15:09 nathanl

If it's helpful I've seen similar with ElixirLS in Emacs. But it goes away when run with (setq lsp-document-sync-method lsp--sync-full)

axelson avatar Sep 04 '25 19:09 axelson

Possibly the same issue: In NeoVim, I just wrote alias GridPoint.Schedules.WeeklyScheduleFactory and saved, and it was changed to alias GridPoint.Schedules.WeeklySchedu()leFactory. I undid and saved again and it happened again. I quit out of NeoVim and reopened the same file and it stopped doing that.

I've seen other such mangled text.

Experience report: I had 2 days of Zed usage, probably ran into this every hour to hour-and-a-half -- 'I save the file, its attempt to auto-format introduces broken syntax that it doesn't "see" as broken; restarting the language servers fixes it'.

Just ran into it now, so I tracked this down, since I see that this comment is what I've run into as well. I'm not sure if 'destroys the buffer' is the right description, from the point of view of a user it feels more like 'code occasionally gets mangled on save / format'

mrluc avatar Sep 06 '25 22:09 mrluc

Yeah, happens to me quite often. I've turned off format-on-save and am only using mix format for now. Its happened to me in both zed and neovim.

zachdaniel avatar Sep 06 '25 23:09 zachdaniel

I think the issue might be related to this https://github.com/elixir-lang/expert/issues/110#issuecomment-3260167953

mhanberg avatar Sep 07 '25 00:09 mhanberg

Not sure if that issue is vim specific, but this particular thing has happened in vim and zed for me (maybe they do the same thing)

zachdaniel avatar Sep 07 '25 09:09 zachdaniel

... Its happened to me in both zed and neovim.

Oh hey, that seems like a great find.

I'm kinda out of my depth here, but if that's true that makes it feel like 'wrong LSP messages, or wrong order' -- when/if we have time we could try to check in the LSP logs in Zed.

Edit: interesting, just read that issue, it does seem like it might be LSP-related, and someone has taken the next step on tracking it down, logging etc!

mrluc avatar Sep 07 '25 16:09 mrluc

Just want to report that the same issue is present in Helix editor as well. Appreciate you taking a look!

YurkoHoshko avatar Oct 07 '25 04:10 YurkoHoshko

Just want to report that the same issue is present in Helix editor as well. Appreciate you taking a look!

Can you confirm you're using the latest?

mhanberg avatar Oct 07 '25 12:10 mhanberg