Skip to content

Watched files: changes in a directory outside the project aren't reported, and a stopped server's watchers stay active #1741

Closed

Description

@lagergren

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.addRootToWatch or replaceWatchedRoots. 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:

  1. The server registers RelativePattern(<a temporary directory>, "**/*.txt"). The directory already holds present.txt.
  2. A test writes created.txt in that directory with java.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:

  1. A server registers a watcher.
  2. The wrapper restarts.
  3. A file matching the watcher is created.

The restarted server receives didChangeWatchedFiles for that file.

Proposal

  1. 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.
  2. 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.

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.

Activity

  1. added
    bug
    on Oct 20, 2026
  2. added this to the 0.21.1 milestone on Oct 20, 2026
  3. moved this to In Progress in Java Toolingon Oct 20, 2026
  4. added a commit that references this issue on Oct 20, 2026
    e5d3c42
  5. moved this from In Progress to Done in Java Toolingon Oct 20, 2026
  6. added a commit that references this issue on Oct 20, 2026
    8f550f0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugdidChangeWatchedFiles

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions