🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@OleksandraKordonets
Copy link

@OleksandraKordonets OleksandraKordonets commented Nov 21, 2025

Fixes #956
This PR adds optional support for a new TypeScript server feature that allows the editor to provide file watch events instead of having tsserver watch all files directly. When enabled and supported, this can significantly reduce the number of filesystem watchers in large workspaces.

Changes:

  • Added a new optional CLI option: --canUseWatchEvents
  • Passed --canUseWatchEvents to tsserver when supported
  • Implemented a WatchEventManager to track tsserver watcher requests and manage dynamic workspace watchers
  • Forwarded relevant client file change notifications to tsserver using the watchChange request
  • Added support for new tsserver events: createFileWatcher, createDirectoryWatcher, closeFileWatcher
  • Added fallback behavior with warnings when prerequisites are not met

When this feature is enabled and all requirements are satisfied the server will:

  • Launch tsserver with --canUseWatchEvents
  • Register minimal LSP file watchers instead of tsserver watching everything
  • Forward DidChangeWatchedFiles events to tsserver as watchChange notifications
  • If any prerequisite is missing, the server logs an explanation and falls back to the existing behavior, ensuring there is no change or risk for existing users.

Notes:
Requires TypeScript >= 5.4 and client support for dynamic watched-files registration and relative patterns. And if any prerequisite is missing, a warning is logged and the server continues with normal tsserver file watching, so no behavior change for existing users.

@OleksandraKordonets OleksandraKordonets changed the title Add support for --canUseWatchEvents and watch event forwarding feat: Add support for --canUseWatchEvents and watch event forwarding Nov 21, 2025
@github-actions github-actions bot added the feat label Nov 21, 2025
@rchl
Copy link
Member

rchl commented Nov 21, 2025

I have actually been working on it last week and came quite far. One thing that was a bit problematic was to adapt the watcher ID concept into LSP where there are no IDs. Curious to check how you've handled that.

After I'll check the changes out we'll have to decide which changes to go with based on what would be more efficient.

@rchl
Copy link
Member

rchl commented Nov 21, 2025

After cursory look I have some comments but I think it would be better to just go with my version of the code which is based on vscode's implementation more closely as that makes maintenance easier.

Some notes (but not sure you should spend time on those given what I said above):

  • The setting whether to enable events should be passed through initializationOptions, not cli.
  • Server explicitly doesn't support workspace folders (has no corresponding server capability) so there is no need to try to read those.
  • Event manager doesn't do debouncing when notifying tsserver about events. The original vscode code does.
  • The ts events related to watching are not currently handled so basically all the relevant code doesn't run
  • There are issues with un-registering capabilities. It unregisters same capability multiple times rather than always the last one. Think there is a race condition in setting this.registration.
  • The events are not matched properly against registered watchers. I had to do some quick hack fixes to get things running in the first place but looks like simple file change events are not reported to tsserver.
  • There is a quite a lot of custom code for handling paths (normalizing, getting dirname, replacing (back)slashes). I wouldn't trust such code personally since there is a lot of corner cases that it might not handle. There are already established ways for doing that (URI class, os.path functions) that should generally work better. Assuming that it's even necessary to do all that.
  • The most tricky part for getting this to work is matching file events from the client to tsserver watcher IDs and I'm not sure how well it works given the problems above.

@rchl
Copy link
Member

rchl commented Nov 21, 2025

I appreciate your efforts on getting registrations aggregated. Since tsserver is very granular in its events, I think your approach makes sense where you mainly just watch the root and match against tsserver watchers once the events are received. That said, I would still prefer to match vscode more closely in some parts.

Perhaps we can work together on this branch to get it working and finalized? That would be more efficient than going through PR comments. Alternatively I could work on my branch and potentially pick some stuff from you.

@OleksandraKordonets
Copy link
Author

I appreciate your efforts on getting registrations aggregated. Since tsserver is very granular in its events, I think your approach makes sense where you mainly just watch the root and match against tsserver watchers once the events are received. That said, I would still prefer to match vscode more closely in some parts.

Perhaps we can work together on this branch to get it working and finalized? That would be more efficient than going through PR comments. Alternatively I could work on my branch and potentially pick some stuff from you.

Thanks so much for the detailed review and for taking the time to run and investigate my implementation. I really appreciate the explanations, a lot of these areas were things I didn’t fully understand when I started, and I learned a lot while working on this.
For me, this was mainly a learning experience and a way to understand how everything fits together, so feedback like this is really valuable.

I’m glad some of the ideas were useful. Since you already have a more complete implementation underway, I think it makes the most sense for you to continue with your version. Please feel free to take anything from my branch that helps.

@rchl
Copy link
Member

rchl commented Nov 21, 2025

No problem.

Lets work on this together then. I can push to the branch so it should be easy and we can discuss things as we go.

@OleksandraKordonets
Copy link
Author

@rchl
Hi, thanks a lot for pushing those changes and continuing the work on this. I was a bit busy over the past couple of weeks, but I’m catching up now and will review them shortly.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pass --canUseWatchEvents to tsserver

2 participants