langium icon indicating copy to clipboard operation
langium copied to clipboard

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

Open lars-reimann opened this issue 1 year ago • 9 comments

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 avatar Oct 18 '23 09:10 lars-reimann

@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.

msujew avatar Oct 18 '23 09:10 msujew

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.

spoenemann avatar Oct 30 '23 14:10 spoenemann

see also didClose in https://github.com/eclipse-langium/langium/discussions/1281

cdietrich avatar Nov 09 '23 13:11 cdietrich

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.

spoenemann avatar Nov 10 '23 09:11 spoenemann

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

sailingKieler avatar Dec 07 '23 13:12 sailingKieler

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.

msujew avatar Dec 07 '23 13:12 msujew

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 avatar Dec 07 '23 14:12 sailingKieler

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 https://github.com/microsoft/vscode/issues/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... 😕

sailingKieler avatar Dec 07 '23 15:12 sailingKieler