vanowm
vanowm

Reputation: 10201

file system watcher doesn't work when used full filename as filter

I'm trying setup a file watcher for one specific file C:\test.json via workspace.createFileSystemWatcher

This is the code I use:

const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern("C:\\", "test.json"));
watcher.onDidChange(uri => console.log("change", uri));
watcher.onDidCreate(uri => console.log("create", uri));
watcher.onDidDelete(uri => console.log("delete", uri));

For some reason events are not triggered, unless I replace filter test.json with *.json - then it works just fine.

Any ideas why complete filename doesn't work?

Upvotes: 2

Views: 422

Answers (1)

Mark
Mark

Reputation: 180855

I see your question(s) ;>} on github. I'll post there as well.

It is interesting that this works:

const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(vscode.Uri.file("C:\\Testing"), "test.json"));

Note that test.json is in a folder Testing.

This does not work - when test.json is at the root of C:

const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(vscode.Uri.file("C:\\"), "test.json"));

So it looks like either vscode.Uri.file("C:\\") doesn't work properly at the drive root level or vscode.RelativePattern() doesn't work properly at the drive root level.

As we discussed on github (see API: createFileSystemWatcher() doesn't work when filter set to a specific file), the problem appears to be the trailing slash in C:\\. Since relative patterns like new vscode.RelativePattern(vscode.Uri.file("C:\\Testing"), "test.json") work and patterns like new vscode.RelativePattern(vscode.Uri.file("C:\\Testing\\"), "test.json") do not work.

But a backslash is required for a drive root level designation:

No, it doesn't. But it's because drive path requires trailing slash, otherwise it's treated as relative path instead:

Use a backslash as required as part of volume names, for example, the "C:" in "C:\path\file" or the "\server\share" in "\server\share\path\file" for Universal Naming Convention (UNC) names.

from your comment at https://github.com/microsoft/vscode/issues/162498#issuecomment-1295628237

so it does seem that vscode.RelativePattern() will not work at the drive root level because of the trailing slash (but the trailing slash is necessary and a simple vscode.workspace.createFileSystemWatcher(vscode.Uri.file("C:\\test.json")); does not work either).

We should update this answer with however the github issue is resolved - can your original new vscode.RelativePattern(vscode.Uri.file("C:\\"), "test.json") be made to work.

Upvotes: 1

Related Questions