Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp: Do not notify all language servers on file save #17756

Merged
merged 4 commits into from
Sep 26, 2024

Conversation

osiewicz
Copy link
Contributor

This is not an ideal solution to https://github.com/fasterthanlime/zed-diags-readme, but current status quo is not great either; we were just going through all of the language servers and notifying them, whereas we should ideally do it based on a glob.
/cc @fasterthanlime

Release Notes:

  • N/A

This is not an ideal solution to https://github.com/fasterthanlime/zed-diags-readme, but current status quo is not great either; we were just going through all of the language servers and notifying them, whereas we should ideally do it based on a glob.
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Sep 12, 2024
@maxdeviant maxdeviant changed the title lsp: Do not notify all language servers on file save. lsp: Do not notify all language servers on file save Sep 12, 2024
@fasterthanlime
Copy link

I do not fully understand this change nor exactly how the integration works, but I want to emphasize that rust analyzer should absolutely know about the change in the markdown file in this instance.

The markdown file does contain rust code blocks that rust analyzer needs to know about I’m assuming.

@osiewicz
Copy link
Contributor Author

It's a bit more subtle; I don't think we spawn a separate language server instance in this case (for . md file) and the existing Rust instance does not care about Markdown files, as it does not register for watching of .md files.

This is a can of worms. ૮꒰꩜ ᯅ ꩜;꒱ა

@fasterthanlime
Copy link

Mhhh, I see.

for the record the original error I saw in the logs was something like “got diagnostics for path/to/README.md but this path isn’t in VFS” or something.

when making the repro I got a different error (the r-a panic)

@Veykril
Copy link

Veykril commented Sep 14, 2024

The panic in r-a happens because it receives an invalid path for the on save handler, it only registers rust and toml files for it but zed incorrectly gives it other files, violating the LSP (see #17376). We fixed panic behavior in r-a (it now logs an error) but this still needs fixing in zed.

@fasterthanlime
Copy link

but zed incorrectly gives it other files

Okay but there is Rust code in the markdown file.

The markdown file is included as the “doc” attribute of the .rs file, and there’s runnable doc tests in there, hence the diagnostics etc.

Shouldn’t r-a care about this markdown file?

@osiewicz
Copy link
Contributor Author

osiewicz commented Sep 14, 2024

I think there are 3 issues at play:

  1. Zed sends out file save notifications on paths RA didn't ask for.
  2. RA does not notice that a change in include_str'd file should be causing a rebuild of the project.
  3. Zed doesn't support completions in Rust snippets in .md files.

I think 1. is fully on Zed, no question asked; I'd say 2 is on RA side. Whether .md file contains Rust code or not seems orthogonal to the issue?
3. seems like a joint effort; RA shouldn't care about parsing markdown files and what-not. I imagine Zed should treat each code snippet in a .md file as a separate (or maybe not?) unnamed buffer and work with that. For it to happen though, RA needs to support unnamed buffers in the first place.

@Veykril
Copy link

Veykril commented Sep 14, 2024

but zed incorrectly gives it other files

Okay but there is Rust code in the markdown file.

The markdown file is included as the “doc” attribute of the .rs file, and there’s runnable doc tests in there, hence the diagnostics etc.

Shouldn’t r-a care about this markdown file?

Two things:

  1. r-a is not able to expand macro invocations in builtin attributes right now -> Support macro expansion inside attribute rust-lang/rust-analyzer#8092
  2. r-a's VFS does not currently handle files other than .rs and .toml files, so even if 1) worked it would still not include the readme -> include!ing files without .rs suffix does not work rust-lang/rust-analyzer#10178

Shouldn’t r-a care about this markdown file?

So to answer, yes, r-a should kind of care (in that it should track the content of the file changing), but it won't ever care about markdown files being saved. (there is a different notification for file updates that would be relevant).

@filipwiech
Copy link

filipwiech commented Sep 14, 2024

@osiewicz While the topic of Rust snippets in Markdown files seems more complex and would require further design and work, the changes in this PR still appear to make sense and be generally better than the current state of things. Would it make sense to go ahead and merge it, to avoid the most obvious issues and to improve the situation for all language servers (fix LSP violation)? 🙂

@osiewicz
Copy link
Contributor Author

@filipwiech yeah agree, we should not creep the scope; I think the solution in this PR might actually undernotify in certain cases (e.g. I'm pretty sure it won't notify RA about changes to .toml files), so I'll hack on it some more on the plane today.

@osiewicz osiewicz merged commit db92a31 into main Sep 26, 2024
9 checks passed
@osiewicz osiewicz deleted the lsp-do-not-notify-on-file-save branch September 26, 2024 11:18
schpet added a commit to schpet/zed that referenced this pull request Sep 27, 2024
…reee

* origin/main:
  git blame gutter: Use smallest possible space (zed-industries#18145)
  Fix minimum gutter line number spacing (zed-industries#18021)
  terraform: Bump to v0.1.1 (zed-industries#18382)
  lsp: Do not notify all language servers on file save (zed-industries#17756)
  Remove leftover println statements (zed-industries#18389)
  Fix `use_on_type_format` setting being unused per language (zed-industries#18387)
  Avoid panic by only restoring workspace if UI has launched (zed-industries#18386)
  Fix Typo in rust language guide (zed-industries#18383)
  editor: Fix cursor shape not restoring when setting removed (zed-industries#18379)
  Avoid unwrap in file finder (zed-industries#18374)
@filipwiech
Copy link

filipwiech commented Oct 1, 2024

@osiewicz Sorry to bother you, but it appears that this PR has broken support for Scala projects (with Scala extension, https://github.com/scalameta/metals-zed). After saving files with the Scala source code in the properly imported project LSP server doesn't get notified about the changes. AFAIK Metals (the LSP provider) does support the didChangeWatchedFiles client capability, so not sure what's going on here, maybe the watchers are not being correctly registered. 🙁

EDIT:
From the official documentation it seems that Metals registers only some special nice-to-have globs as part of the watchers, see here for more details: https://scalameta.org/metals/docs/integrations/new-editor#workspacedidchangewatchedfiles. So the "normal" source code files are still expected to be sent by the editor, even though they are not explicitly included in the registered paths. 🙁

Maybe the previous approach using the language_servers_for_buffer could still be combined with the paths watched by the server, as a fallback? 🤔

WeetHet added a commit to WHForks/zed that referenced this pull request Oct 14, 2024
osiewicz pushed a commit that referenced this pull request Oct 16, 2024
Reverts #17756. According to the existing
implementations of the LSP specification, namely
[Helix](https://github.com/helix-editor/helix/blob/a7651f5bf027ec98645d571ab05a685d97e1b772/helix-view/src/document.rs#L1038)
and, if I'm not wrong,
[VSCode](https://github.com/microsoft/vscode-languageserver-node/blob/main/client/src/common/textSynchronization.ts#L580),
`textDocument/didSave` has nothing to do with the watched files and
should be sent to the language servers connected to the buffers even if
the files are not watched by those. As the LSP spec doesn't say anything
about `didSave` being related to the watched files, and the reference
implementation in VSCode seemingly does not filter the notifications
according to those, it seems like this is an incorrect interpretation of
the specification

This also causes issues with language servers. See [Metals
issue](scalameta/metals-zed#28 (comment))
for example

Closes #18636

Release Notes:

- N/A
noaccOS pushed a commit to noaccOS/zed that referenced this pull request Oct 19, 2024
…17756)

This is not an ideal solution to
https://github.com/fasterthanlime/zed-diags-readme, but current status
quo is not great either; we were just going through all of the language
servers and notifying them, whereas we should ideally do it based on a
glob.
/cc @fasterthanlime

Release Notes:

- N/A
noaccOS pushed a commit to noaccOS/zed that referenced this pull request Oct 19, 2024
…dustries#19183)

Reverts zed-industries#17756. According to the existing
implementations of the LSP specification, namely
[Helix](https://github.com/helix-editor/helix/blob/a7651f5bf027ec98645d571ab05a685d97e1b772/helix-view/src/document.rs#L1038)
and, if I'm not wrong,
[VSCode](https://github.com/microsoft/vscode-languageserver-node/blob/main/client/src/common/textSynchronization.ts#L580),
`textDocument/didSave` has nothing to do with the watched files and
should be sent to the language servers connected to the buffers even if
the files are not watched by those. As the LSP spec doesn't say anything
about `didSave` being related to the watched files, and the reference
implementation in VSCode seemingly does not filter the notifications
according to those, it seems like this is an incorrect interpretation of
the specification

This also causes issues with language servers. See [Metals
issue](scalameta/metals-zed#28 (comment))
for example

Closes zed-industries#18636

Release Notes:

- N/A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants