Can a VSCode extension monitor file system changes?
Asked Answered
B

1

7

We need a Visual Studio Code extension that monitors a specific folder on the file system, and when the contents of the folder change, it should trigger some action.

Can an extension do that?

I suppose we need a Task. However, all the examples on tasks I have found deal with triggers from inside VSCode (mostly edits by the user).

I have not found any examples on background tasks that are triggered by an external event source like a network or a file system.

Bornite answered 2/12, 2021 at 7:56 Comment(7)
have you looked at the file watch API of VSCBullnose
FileSystemWatcher and FileSystemProvider.Donela
@Donela isn't FileSystemWatcher only for files within the currently opened solution? And FileSystemProvider seems to be an interface that I need to implement if I want my own file system.Bornite
@Bullnose the only thing I can find is vscode.workspace.createFileSystemWatcher(...), but that seems to be about files within the currently opened workspace or folder. I need to watch the file system outside of the workspace.Bornite
does Node have a file watch module?Bullnose
@Bullnose yes, fs.watch. Can I use NodeJS directly in VSCode background tasks?Bornite
Yes, VSC is a NodeJS applicationBullnose
A
7

Since VSCode 1.64.2 (Jan. 2022), vscode.workspace.createFileSystemWatcher is able to monitor files outside the current VSCode workspace!

See issue 140693 "Test: out-of-workspace file watching from extensions".

credits to Windows team though for implementing the best OS file watcher across all operating systems.

ReadDirectoryChangesW is awesome and is used by the file watcher library we use.

Ananias answered 25/2, 2022 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.