Skip to content

A moved or renamed document is connected again to a language server that no longer matches it #1914

Closed

Description

@lagergren

When a file whose document is connected is renamed or moved, the language server gets the document again at the new place, even if the file no longer matches it there. A connected test.foo, of a language server for *.foo files, that is renamed to test.txt stays open on that language server, although a request for test.txt then matches no language server.

How it happens

LSPFileListener.onFileRenameAfter disconnects the document of the earlier place and calls reconnectMovedDocument, which connects the file to a language server of the same definition (for a definition with instances, the instance chosen for the new place). Nothing checks that the definition still matches the file: its mappings (file type, language, file name patterns and document matchers) and the client features' isEnabled(file).

A test on main renames a connected test.foo to test.txt: the language server still synchronizes the file afterwards, while getLanguageServers finds no language server for it.

#1911 reuses that reconnect for a document that the move of its file missed, and for an open file whose connection dropped because the file moved. Its third fix asks again through a request instead, which matches the file again.

Possible fix

Match the file again before connecting it at its new place, for example by connecting it through a request for the definition, as the third fix of #1911 does with getLanguageServers(psiFile, null, null, serverDefinition). That also checks isEnabled() and the client features. For a definition that matches asynchronously, that request needs the fix of #1913 first.

Found in the review of #1911.

Activity

  1. d-biehl commented on Nov 9, 2026

    @d-biehl
    Contributor

    I've reproduced it on main (18f9d5e): a test renames a connected test.foo to test.txt, and the language server for *.foo files opens it again. I'll connect a moved document through a request for its definition, as you suggest, once #1915 and #1911 are in, as it needs the asynchronous matching of #1915 and changes the same places as #1911.

    If I don't get to it today, I can only go on with this and with #1911 on Friday, as I'm at a conference from tomorrow.

  2. lagergren commented on Nov 9, 2026

    @lagergren
    CollaboratorAuthor

    @d-biehl, #1911 is merged (2253ca0), so the switch you planned there can come with this one: once #1915 is in, the second request for a moved file in askAgainForTheInstanceOf can ask for its definition only, as getLanguageServers(psiFile, null, null, serverDefinition), which then takes a definition that matches the file asynchronously too, in place of the ordinary request that its before-starting filter keeps to the definition. That also ends the retries for other definitions that CodeRabbit asked about in #1911. The race with a scoped connection in its cleanup stays as we left it in its thread, and can be looked at again when the reconnection of a moved document goes through a request here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions