Repository navigation
Description
There are two problems with the watchers a server registers for workspace/didChangeWatchedFiles, both on main (9d1e5cd).
1. Changes in a watched directory outside the project are not reported
LSP4IJ reports a watched-file change when the VFS reports an event for a file that matches a registered watcher. For a directory outside the project, there is no such event when another process changes a file there:
- LSP4IJ never asks IntelliJ to watch the directory: nothing calls
LocalFileSystem.addRootToWatchorreplaceWatchedRoots. IntelliJ's native watcher watches the project's content roots, and the roots requested that way. - The VFS reports changes only for files it has loaded, and nothing loads the directory.
So when a server watches a directory outside the project, such as a build output directory or an installed library, the server isn't told when another process writes there. The watcher can be a RelativePattern whose baseUri is that directory, or an absolute glob. LSP says: "The watched files notification is sent from the client to the server when the client detects changes to files and folders watched by the language client."
To reproduce it with LSP4IJ's mock server:
- The server registers
RelativePattern(<a temporary directory>, "**/*.txt"). The directory already holdspresent.txt. - A test writes
created.txtin that directory withjava.nio.
The directory is never loaded into the VFS (present.txt never appears among its cached children), and no didChangeWatchedFiles arrives.
We met this in our own IntelliJ client, for Ecstasy. The server watches external resource directories, and in IntelliJ the changes there reached it only after the directory had been loaded and the VFS refreshed. We found that native watching marks the changed files dirty, but the VFS isn't refreshed while the IDE stays focused. We bridge it in the plugin: we request native watches for those roots, observe a root that doesn't exist yet through its nearest existing parent, and refresh just those roots every 2 seconds. In VS Code, the same server receives these changes without any workaround.
2. A stopped server's watchers stay active
LanguageServerWrapper.stop() clears dynamicRegistrations without running their unregistration handlers. For workspace/didChangeWatchedFiles, that handler is what removes the watchers from the file listener, so they stay registered after their server has stopped. After a restart, the new server is told of changes that match watchers it never registered.
To reproduce it:
- A server registers a watcher.
- The wrapper restarts.
- A file matching the watcher is created.
The restarted server receives didChangeWatchedFiles for that file.
Proposal
- Drop a stopped server's watchers. When the current server stops, drop the watchers it registered, and ignore a registration that a stopped connection's callback still delivers.
- Watch explicit roots outside the project. For each watcher with an explicit root outside the project (the base of a
RelativePattern, or the fixed prefix of an absolute glob):- Request native watching of the root with
LocalFileSystem.replaceWatchedRoots: recursive when the pattern descends into subdirectories, flat otherwise. - Load the root's directories into the VFS, so that creations are reported. Observe a root that doesn't exist yet through its nearest existing parent, flat, until it appears.
- Refresh those directories every 2 seconds while the watchers are registered. Read one directory per cancellable read action, so that a large tree never holds off write actions, and don't follow symbolic links.
- Release the watches and stop refreshing when the watchers are unregistered, their server stops, or the wrapper is disposed.
- Request native watching of the root with
Pure globs such as **/*.java, and roots inside the project, stay as they are: the project's VFS events already cover them.
The cost is a refresh of the registered external roots every 2 seconds. Are you OK with that, or would you prefer something else, such as a longer interval or a setting? I'll open a pull request with this proposal, which you can review against it.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
fix: watch the roots outside the project that file watchers name, and…
fix: watch the roots outside the project that file watchers name, and…