How to tell when FileSystemWatcher actually starts listening for events #1242
-
In our tests it seems when we create a file watcher const w = vscode.workspace.createFileSystemWatcher(...) it actually does not start seeing file events for some time after creation. While this is not a huge deal when a real user interacts with the extension (since people are inherently slow and are able to retry any action) it makes it very hard to test any code that depends on file watching in automated tests. For example we need to do this const unwrapper = new ProjectUnwrapper() // some code creating file watcher in constructor
await sleep(5000) // give the backing file watchers enought time to actually start watching
await vscode.workspace.fs.rename(j('foo.fizz'), j('bar.fizz'))
expect(unwrapper).reactedToFileEventSomehow() Which works most of the time but breaks down when the CI is under load because the 5sec sleep is no longer enough. We also tried watching the system log (by tailing the log file on disk and using this to unblock barriers in the test) with trace logging enabled as described here https://github.com/microsoft/vscode/wiki/File-Watcher-Issues but sometimes that did not work either + some ways of creating the watcher do not seem to produce any message into that log at all (e.g. when given just single string arg to the Is there some better way to go about this? I think it'd be very useful to have some means provided by vscode API to know when one can reliably expect file events to be emitted e.g. export interface FileSystemWatcher extends Disposable {
onWatcherStateChanged: Event<FileWatcherState>;
}
export enum FileWatcherState {
Preparing, // no events will arrive yet, watch is being set up
Watching , // fully operational
Degraded, // e.g. when the dreaded "No more file descriptors" problem appears,
// some events might still appear but can't be relied on to be complete
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Here's what Copilot suggest which seems reasonable to me but let me know your thoughts. Your analysis of the delay in the FileSystemWatcher becoming fully operational due to the underlying asynchronous setup process is accurate. This indeed poses challenges for automated testing, especially in CI environments where resources can be constrained and unpredictable. Currently, the Visual Studio Code API does not provide a direct way to determine when a FileSystemWatcher is fully operational. Your approach of introducing a delay (await sleep(5000)) is a common workaround but, as you've discovered, it's not reliable under all conditions. Proposed Solution Workaround Strategies
Example Polling Workaround async function waitForFileChange(expectedFilePath, timeout = 10000) {
let elapsedTime = 0;
const pollInterval = 500;
while (elapsedTime < timeout) {
if (await checkFileExists(expectedFilePath)) {
return true; // File change detected
}
await sleep(pollInterval);
elapsedTime += pollInterval;
}
throw new Error('Timeout waiting for file change');
}
async function checkFileExists(filePath) {
try {
await vscode.workspace.fs.stat(vscode.Uri.file(filePath));
return true;
} catch (error) {
return false;
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
} This example checks for the existence of a file, but you could adapt it to check for specific file changes or the presence of sentinel files that indicate readiness. While these workarounds can help, the ideal solution would be for the VS Code API to provide a more reliable way to determine the operational state of FileSystemWatcher instances. |
Beta Was this translation helpful? Give feedback.
-
Hi @vaclavHala, are you still having this issue? |
Beta Was this translation helpful? Give feedback.
-
I also ran into the problem of changes being delayed when opening files using vscode.workspace.openTextDocument via FileSystemWatcher's onDidChange. I will share the repository for those who are interested in verification. |
Beta Was this translation helpful? Give feedback.
Hello @olguzzar , yes we are still facing this problem. I created a feature request here but since this is a feature only really useful for extension authors who try to test the way their extensions integrate with file watching there seems to be little interest.
At the moment we are considering mocking the whole file watcher in tests and emitting expected file events manually, since the vscode provided facility can't be relied on in automated testing for reasons outlined above. While this approach makes the tests reliable, it lowers confidence in the features in the extension being tested working as expected "in the real world" when combined with the real file watching. Also it makes writ…