JCWasmx86 / mesonlsp

An unofficial, unendorsed language server for meson written in C++
GNU General Public License v3.0
59 stars 8 forks source link

Writing to a meson build file sometimes undoes changes made to the file #133

Open Discusser opened 2 weeks ago

Discusser commented 2 weeks ago

Describe the bug Sometimes, when I make changes to my meson.build file, then I save it (with just :w, which in turn formats the buffer), or simply call vim.lsp.buf.format(), the changes I've just made get undid, as if I manually pressed the undo key.

To Reproduce Unfortunately this isn't very easy to reproduce. I've tried reproducing it myself but this only happens sometimes, and I can't really tell why it starts happening. I usually just fix this by restarting my editor.

Expected behavior I expect my changes to stay

Screenshots/Screencasts https://github.com/user-attachments/assets/9f574e3e-b7cf-45c1-94f7-560531a5425c

System info

Additional context End of :LspLog (I believe the issue here is with the cancelRequest notification, but I don't know what it means)

[ERROR][2024-08-28 20:54:51] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-08-28 20:54:51] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentSymbol \n"
[ERROR][2024-08-28 20:54:52] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-08-28 20:54:58] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/formatting \n"
[ERROR][2024-08-28 20:54:58] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:18: Received notification $/cancelRequest \n[ WARN ] AbstractLanguageServer - ../src/libls/ls.cpp:54: Unknown notification: '$/cancelRequest' \n[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
JCWasmx86 commented 2 weeks ago

Ok, that's extremely interesting. I would expect the language server not to be able to write to files (And I can't find an explicit part in the code that could do that)

You could try running this command as soon as the server starts:

strace -f -s320000 -p $(pidof mesonlsp) >/tmp/logs-$(pidof mesonlsp).txt 2>&1

This logs all syscalls, and you should be able to see if it's writing a file. Please remove all potential sensitive information and send it to me if you were able to reproduce this again, while strace is running

Discusser commented 2 weeks ago

I have managed to reproduce this issue with strace running after some time. The entire log file is very long (>40000 lines), so here are the last 2000 lines: mesonlsp_strace_end.txt I believe line 1851 is where the issue occurs when trying to save the file (I might be wrong but I noticed that this syscall always appears when the bug occurs): [pid 99909] writev(1, [{iov_base="Content-Length: 657\r\n\r\n{\"id\":426,\"jsonrpc\":\"2.0\",\"result\":[{\"kind\":18,\"location\":{\"range\":{\"end\":{\"character\":7,\"line\":2},\"start\":{\"character\":0,\"line\":2}},\"uri\":\"file:///home/discusser/Documents/Coding/C++/TerminalTextEditor/meson.build\"},\"name\":\"sources\"},{\"kind\":18,\"location\":{\"range\":{\"end\":{\"character\":12,\"line\":10},\"start\":{\"character\":0,\"line\":10}},\"uri\":\"file:///home/discusser/Documents/Coding/C++/TerminalTextEditor/meson.build\"},\"name\":\"dependencies\"},{\"kind\":19,\"location\":{\"range\":{\"end\":{\"character\":19,\"line\":17},\"start\":{\"character\":0,\"line\":17}},\"uri\":\"file:///home/discusser/Documents/Coding/C++/TerminalTextEditor/meson.build\"},\"name\":\"include_directories\"}]}", iov_len=680}, {iov_base=NULL, iov_len=0}], 2) = 680

JCWasmx86 commented 2 weeks ago

Sadly I cannot find anything relevant in there. The line you highlighted is just part of ths protocol, written to stdou

JCWasmx86 commented 2 weeks ago

Maybe it's some race condition or something like that with other parts of your editor configuration? Because I really cannot find a location where that could happen within mesonlsp. Something like this is plausible:

Maybe try asking in the support channels of neovim/mason/etc. whether somebody else had something similar happen to them

Discusser commented 2 weeks ago

I've looked online and I can't really find anyone with the same issue. I've only ever experienced this problem with MesonLSP. You mentioned that it could be a race condition, since vim.lsp.buf.format() is an async function. It turns out you can pass {async=false} as a parameter to make it synchronous. I'll try that next time I can reproduce the issue, maybe it'll make a difference.

Discusser commented 2 weeks ago

Passing {async=false} to vim.lsp.buf.format() does not change anything unfortunately. I noticed that this time, autocompletion also didn't work. I don't know if this was the case last time since I never bothered to check. Here's the end of :LspLog yet again:

[ERROR][2024-09-02 18:01:58] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:01:59] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:01:59] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:18: Received notification $/cancelRequest \n[ WARN ] AbstractLanguageServer - ../src/libls/ls.cpp:54: Unknown notification: '$/cancelRequest' \n[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:00] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/completion \n[ INFO ] ws-/home/discusser/Documents/Coding/C++/TerminalTextEditor - ../src/liblangserver/workspace.cpp:547: Created 0 completions \n"
[ERROR][2024-09-02 18:02:00] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:00] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/completion \n[ INFO ] ws-/home/discusser/Documents/Coding/C++/TerminalTextEditor - ../src/liblangserver/workspace.cpp:547: Created 0 completions \n"
[ERROR][2024-09-02 18:02:01] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:01] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:01] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:01] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:01] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/completion \n[ INFO ] ws-/home/discusser/Documents/Coding/C++/TerminalTextEditor - ../src/liblangserver/workspace.cpp:547: Created 0 completions \n"
[ERROR][2024-09-02 18:02:02] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/completion \n[ INFO ] ws-/home/discusser/Documents/Coding/C++/TerminalTextEditor - ../src/liblangserver/workspace.cpp:547: Created 0 completions \n"
[ERROR][2024-09-02 18:02:02] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:04] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:04] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:04] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/completion \n[ INFO ] ws-/home/discusser/Documents/Coding/C++/TerminalTextEditor - ../src/liblangserver/workspace.cpp:547: Created 0 completions \n"
[ERROR][2024-09-02 18:02:06] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:06] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:18: Received notification $/cancelRequest \n[ WARN ] AbstractLanguageServer - ../src/libls/ls.cpp:54: Unknown notification: '$/cancelRequest' \n[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:06] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/foldingRange \n"
[ERROR][2024-09-02 18:02:06] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentSymbol \n"
[ERROR][2024-09-02 18:02:06] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:09] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/documentHighlight \n"
[ERROR][2024-09-02 18:02:25] .../vim/lsp/rpc.lua:770    "rpc"   "/home/discusser/.local/share/nvim/mason/bin/mesonlsp"  "stderr"    "[ INFO ] AbstractLanguageServer - ../src/libls/ls.cpp:70: Received request: textDocument/foldingRange \n"

Notice how for the request textDocument/completion, the LSP returns 0 completions. Here's a video showing the behavior above: https://github.com/user-attachments/assets/cb9edef3-b0b1-4c6f-950c-f83583059c30

JCWasmx86 commented 2 weeks ago

The 0 completions can happen due to the code creating completions being whacky and not that sophisticated. But sadly I have no idea how to proceed further