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

Langium documents don't get removed after the containing directory is renamed #1230

Open
lars-reimann opened this issue Oct 18, 2023 · 11 comments · May be fixed by #1786
Open

Langium documents don't get removed after the containing directory is renamed #1230

lars-reimann opened this issue Oct 18, 2023 · 11 comments · May be fixed by #1786
Labels
bug Something isn't working LSP Language Server Protocol integration

Comments

@lars-reimann
Copy link
Member

lars-reimann commented Oct 18, 2023

After renaming a directory, all DSL files contained inside are listed twice in services.shared.workspace.LangiumDocuments. This can affect various downstream features, like linking, type computation, or validation.

Langium version: 2.0.2
Package name: langium

Steps To Reproduce

  1. In a Langium project, observe the contents of services.shared.workspace.LangiumDocuments. In the repo linked below, it gets logged in the output panel.
  2. Create a folder.
  3. Create a DSL file inside (extension .hello for the repo linked below).
  4. Rename the folder.

Link to code example: https://github.com/lars-reimann/langium-cst-range

The current behavior

Before renaming the directory, the document is included (correct):
image

After renaming (third array), the new document is shown (correct), but the old one is not deleted (wrong):
image

The expected behavior

Only the new document should be included.

@lars-reimann lars-reimann added the bug Something isn't working label Oct 18, 2023
@msujew
Copy link
Member

msujew commented Oct 18, 2023

@lars-reimann Thanks for the report. I believe this is part of a larger issue where we don't actually listen to a few of the workspace related LSP notifications, mainly:

  • workspace/didChangeWorkspaceFolders
  • workspace/willRenameFiles
  • workspace/didRenameFiles
  • workspace/willCreateFiles
  • workspace/didCreateFiles
  • workspace/willDeleteFiles
  • workspace/didDeleteFiles

Some of these are not interesting for the default behavior of Langium, but we should definitely start handling the didChangeWorkspaceFolders, didRenameFiles, didCreateFiles and didDeleteFiles notifications.

@spoenemann spoenemann added the LSP Language Server Protocol integration label Oct 26, 2023
@spoenemann
Copy link
Contributor

I had a closer look at this. We already get notifications of most changes through workspace.didChangeWatchedFiles notifications. So the only case where we need additional notifications are when folders are renamed, as those are not covered by the glob pattern **/*.hello. We could do this by explicitly watching all folders in the language server, but what I don't like about it is that we have a distribution of responsibilities between language client and language server code. It would be better to find a solution where watched file system events are specified fully on the client or server side.

@cdietrich
Copy link
Contributor

see also didClose in #1281

@spoenemann
Copy link
Contributor

I experimented with adding / removing workspace folders with Langium documents and did not see any issues. So it seems we don't need to handle didChangeWorkspaceFolders for now.

A service for the file operations is introduced in #1286, but as mentioned in that PR, those notifications should not be used to trigger DocumentBuilder updates. They are rather meant to enhance the user experience when working with the IDE, e.g. by automatically changing some text content when you rename a file (we could make use of that for langium file renaming).

The remaining question is how to fix the issue reported here. My current understanding is that we should use the file system watcher for that by adding a suitable glob pattern and handling it.

@sailingKieler
Copy link
Contributor

sailingKieler commented Dec 7, 2023

While reviewing and testing your changes of #1286 I could produce a corrupt state of langiumDocuments by renaming a document's parent folder and then opening the document, see screenshots.

Thus listening to didChangeWorkspaceFolders is necessary I guess.

Bildschirmfoto 2023-12-07 um 13 50 19

Bildschirmfoto 2023-12-07 um 13 50 01

@msujew
Copy link
Member

msujew commented Dec 7, 2023

Thus listening to didChangeWorkspaceFolders is necessary I guess.

workspace/didChangeWorkspaceFolders is for the case when you actually change the workspace by adding a completely new workspace folder or remove one from the workspace. Note that workspace folders are not folders inside of a workspace but the directories that constitute a multi-root workspace.

But I agree. The language server should listen to it.

@sailingKieler
Copy link
Contributor

workspace/didChangeWorkspaceFolders is for the case when you actually change the workspace by adding a completely new workspace folder or remove one from the workspace. Note that workspace folders are not folders inside of a workspace but the directories that constitute a multi-root workspace.

@msujew you're right, I didn't check the spec, which is saying "about workspace folder configuration changes"

@sailingKieler
Copy link
Contributor

sailingKieler commented Dec 7, 2023

The remaining question is how to fix the issue reported here. My current understanding is that we should use the file system watcher for that by adding a suitable glob pattern and handling it.

This seems not to be possible in an adequate way, see microsoft/vscode#60813

An explicit remark on that was deleted with https://github.com/microsoft/vscode/pull/139881/files#diff-0a75aed19c118603eb96332bc0b9c2d7867f4182346d16d18b7fc31b6ceeb321L10951-L10952 beginning of last year.

I could make notifications on folder name changes work with a glob pattern like ('**/{*,*.<extension>}, which however kills the file extension based filtering, and in case of a folder name change we would have to look for documents with the corresponding folder path prefix on our own... 😕

@ballcoach12
Copy link

Has anyone developed a workaround for this issue?

@msujew
Copy link
Member

msujew commented Jan 8, 2025

FYI, there are seemingly workarounds for this, but they cannot be implemented on the language server side (i.e. we cannot implement them in Langium). Instead, they require changes to the language client. See stinb/UnderstandForVSCode@62eef64#diff-cae88b2ea25f713b20b6aa35c12ae6cf520b11d806be403dfb04cb28f612275aL124.

Langium listens correctly to all language client events. It's just that the vscode-languageclient (or vscode in general) doesn't correctly send the events when the folder is renamed.

@msujew msujew linked a pull request Jan 9, 2025 that will close this issue
@msujew
Copy link
Member

msujew commented Jan 9, 2025

I've provided a PR for this over at #1786. Note that this issue isn't specific to renaming but also applies to any directory deleting operation. To the language server, a rename is just a delete+update notification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working LSP Language Server Protocol integration
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants