FileWatchers
in VS Code... watch files.
The VS Code API docs for the FileSystemWatcher
interface states:
A file system watcher notifies about changes to files and folders on disk or from other FileSystemProviders
.
Which should answer your question for what file watchers do.
The VS Code API docs for the createFileSystemWatcher
workspace function are much elaborated:
Creates a file system watcher that is notified on file events (create, change, delete) depending on the parameters provided.
By default, all opened workspace folders will be watched for file changes recursively.
That's to be expected. How else would VS Code implement things like its Explorer view? (which updates itself when files and directories are added, removed, renamed, moved, etc.)
As for extensions that use this API to do things like providing Intellisense, how else would they know when you've created, edited, or deleted files in the programming/markup language they support, in order to hook that info into the rest of the software they use to provide their IntelliSense functionality?
You stated:
I tried to add the "*
" pattern in the files.watcherExclude to stop filewatcher, but I have no clue whether it's actually working or not.
See this section of the createFileSystemWatcher
docs:
Additional paths can be added for file watching by providing a RelativePattern with a base
path to watch. If the pattern
is complex (e.g. contains **
or path segments), the path will be watched recursively and otherwise will be watched non-recursively (i.e. only changes to the first level of the path will be reported).
VS Code and extensions that use the VS Code API can override your files.watcherExclude
settings where they deem fit. From the createFileSystemWatcher
docs:
Note that requests for recursive file watchers for a base
path that is inside the opened workspace are ignored given all opened workspace folders are watched for file changes recursively by default. Non-recursive file watchers however are always supported, even inside the opened workspace because they allow to bypass the configured settings for excludes (files.watcherExclude
). If you need to watch in a location that is typically excluded (for example node_modules
or .git
folder), then you can use a non-recursive watcher in the workspace for this purpose.
But the createFileSystemWatcher
docs do warn its users to try to minimize usage:
If possible, keep the use of recursive watchers to a minimum because recursive file watching is quite resource intense.
[...]
Note that file events from recursive file watchers may be excluded based on user configuration. The setting files.watcherExclude
helps to reduce the overhead of file events from folders that are known to produce many file changes at once (such as node_modules
folders). As such, it is highly recommended to watch with simple patterns that do not require recursive watchers where the exclude settings are ignored and you have full control over the events.
So use the files.watcherExclude
setting where you deem appropriate for your project, and trust VS Code and your extensions to do the right thing. If you really notice them being wasteful in some areas that generalize to all VS Code users and not just to you and your project, then you can file an issue ticket to VS Code or the extension presenting the case and evidence.
Now, if you're not just a user of an extension or VS Code, but you're developing VS Code or those extensions, the way to dispose of a FileSystemWatcher
once it is no longer needed is to use its dispose
method.
Extra tidbit: If you want to learn a bit about how file watchers are implemented, see this page in the VS Code wiki: https://github.com/microsoft/vscode/wiki/File-Watcher-Issues
**
but you can also find multiple issues on their repository that note various things to use for excludes based on the projects you deal with. An example – Dineric*
and**
do not prevent file tree hints icons (ie yellow "M" for "modified"), when I focus the window again after editing the file externally. Also, the file's error count (ie red "9+") is only visible if I have the file open, regardless of this setting. I don't know what else this setting might affect. – Illegaltouch foo.txt
ornpm install
. See github.com/microsoft/vscode/issues/125886 for when they made VS Code watch node_modules by default, which you can disable with"**/node_modules/**": true,
– Illegal