Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createFileSystemWatcher API not fired when parents renamed #60813

Closed
NTaylorMullen opened this issue Oct 12, 2018 · 12 comments
Closed

createFileSystemWatcher API not fired when parents renamed #60813

NTaylorMullen opened this issue Oct 12, 2018 · 12 comments
Assignees
Labels
debt Code quality issues file-watcher File watcher *out-of-scope Posted issue is not in scope of VS Code

Comments

@NTaylorMullen
Copy link
Member

  • VSCode Version: 1.28.0
  • OS Version: Win10-x64

Steps to Reproduce:

  1. Create a file system watcher that monitors adds/removes:
...
        const watcher = vscode.workspace.createFileSystemWatcher('**/*.csproj*');
        const createRegistration = watcher.onDidCreate(async (uri: vscode.Uri) => {
            console.log('CSProj added: ' + uri.toString());
        });

        const deleteRegistration = watcher.onDidDelete(async (uri: vscode.Uri) => {
            console.log('CSProj removed: ' + uri.toString());
        });
...
  1. Change the parent folder of a csproj

Expected: remove => create gets called on the watched files
Actual: Nothing fires

Additional Notes

Seeing this in practice in the Razor VSCode extension: https://github.com/aspnet/Razor.VSCode/blob/05b60813dd446fc4a21529a3513aeec186886465/src/Microsoft.AspNetCore.Razor.VSCode/src/RazorProjectTracker.ts#L25-L32

@vscodebot
Copy link

vscodebot bot commented Oct 12, 2018

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

@jrieken jrieken assigned bpasero and unassigned jrieken Oct 15, 2018
@bpasero bpasero added the file-watcher File watcher label Oct 15, 2018
@bpasero
Copy link
Member

bpasero commented Oct 15, 2018

This is a consequence of us optimizing to only report the folder rename as a change event and not the files within. We might support this scenario with a new file system implementation.

@bpasero bpasero changed the title createFileSystemWatcher doesn't notify if parent folder results in watched file path change createFileSystemWatcher API not fired when parents renamed Oct 15, 2018
@NTaylorMullen
Copy link
Member Author

This is a consequence of us optimizing to only report the folder rename as a change event and not the files within. We might support this scenario with a new file system implementation.

Interesting choice. Should I change my bits to use the onChange method?

@bpasero
Copy link
Member

bpasero commented Oct 16, 2018

@NTaylorMullen well there is currently no workaround unless you throw in a glob pattern with the name of the folder that was renamed.

@NTaylorMullen
Copy link
Member Author

Oh geez that's pretty bad. I guess we'll have to leave the behavior broken for now then. Is there another issue I can watch that tracks the effort to "support this scenario with a new file system implementation"?

@vscodebot
Copy link

vscodebot bot commented Oct 18, 2019

We closed this issue because we don't plan to address it in the foreseeable future. You can find more detailed information about our decision-making process here. If you disagree and feel that this issue is crucial: We are happy to listen and to reconsider.

If you wonder what we are up to, please see our roadmap and issue reporting guidelines.

Thanks for your understanding and happy coding!

@Timmmm
Copy link
Contributor

Timmmm commented Jun 18, 2020

Is this API completely broken then? If I'm writing a C extension (for example) and a user opens Foo.cpp which contains #include "bar/Foo.h", I want to add a watch for Foo.h... but then if they rename the bar directory my extension won't be notified? Is that right?

@Mrazator
Copy link

This is a pretty big limitation for stateful language servers which cannot track project-wide diagnostics and changes - it's almost 3 years old, anyone using some workarounds (except for custom language client and filewatcher)?

@NTaylorMullen well there is currently no workaround unless you throw in a glob pattern with the name of the folder that was renamed.

Theoretically, this could work by throwing in the glob all existing folders, but once there is some folder for example copied (as it was not existing), then the server is unnotified again.

@sakivns
Copy link

sakivns commented Jun 3, 2022

This is a pretty big limitation for stateful language servers which cannot track project-wide diagnostics and changes - it's almost 3 years old, anyone using some workarounds (except for custom language client and filewatcher)?

@NTaylorMullen well there is currently no workaround unless you throw in a glob pattern with the name of the folder that was renamed.

Theoretically, this could work by throwing in the glob all existing folders, but once there is some folder for example copied (as it was not existing), then the server is unnotified again.

I have used the glob pattern "**/*" which is triggering didChangeWatchedFile for each folder delete or rename(in this case created and deleted). In case of copying a folder, the didChangeWatchedFile event is triggered for folder creation, and for all the nested specific files for which patterns are registered.

dabreadman pushed a commit to THIS-IS-NOT-A-BACKUP/vscode that referenced this issue Aug 12, 2022
…d / deleted (microsoft#157956)

Fix markdown link diagnostics not updated when directories are renamed/deleted

Turns our that `createFileSystemWatcher` will not fire if a parent dir is renamed / deleted. See microsoft#60813

To fix this, I believe we have to create watchers for all parent directories too (or watch everything in the entire workspace)
@bmewburn
Copy link

bmewburn commented Jan 2, 2023

I've found this is a problem when trying to watch file changes in PHP projects when using composer. Composer appears to save files to a temp directory before renaming the dir. This results in a bunch of create events for files that only exist briefly and no events for the rename.

@znorman-harris
Copy link

znorman-harris commented May 31, 2023

Not gonna lie, I 'm having a hard time understanding why the file watching API is so broken here. If I set up a file watcher, I expect:

  • Events on creation
  • Events on updates
  • Events on deletion
  • Events on rename

In reality, you only get events when a "file" has the first three happen to it. Meaning if a "file" is deleted because a folder is deleted, you are not alerted which indicates that you are, in fact, not actually watching the files.

I would expect a watched file to count as any file in your workspace that matches your glob pattern on creation. If that file changes, I should always have an event that gets fired without having to care about the origin of the event being the file or from a parent folder. Otherwise the naming of this API does not reflect what it actually does and it will cause bugs for extension developers just like it did for me.

@Timmmm
Copy link
Contributor

Timmmm commented Aug 26, 2023

I still don't understand how we're supposed to use this API at all given this bug.

Suppose I'm writing an LSP for Java. I want to know about all .java files in the workspace and receive a notification when any of them is created, modified, renamed or deleted.

It sounds like the only way to do that currently is:

  1. Use this API but watch everything, and then add custom logic to invalidate folder contents when the folder is deleted/renamed.
  2. Just give up on this API and do the watching yourself.

Ironically the LSP specification recommends using this API because:

in our experience getting file system watching on disk right is challenging

Well, yeah. Can you really say "use our API - it's hard to get it right", when your API is so fundamentally broken?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debt Code quality issues file-watcher File watcher *out-of-scope Posted issue is not in scope of VS Code
Projects
None yet
Development

No branches or pull requests

9 participants