-
-
Notifications
You must be signed in to change notification settings - Fork 579
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
Add a file "rename" event #303
Comments
Can't get this consistently from the underlying APIs (primarily from node core), and replacing the current add/unlink with a rename would be a breaking change. Keeping the behavior as consistent as we can regardless of platform is a high priority, so even in the circumstances where this is possible it's unlikely to get exposed by chokidar. |
Open to ideas though, and maybe there is a strategy that would allow this to be added as a non-breaking change (such as being enabled by an option or emitting rename in addition to the other two existing events). |
Since I desperately could do with a rename event as well, may I suggest at the very least you can add a rename argument to |
I think it is possible now to make the event on your own. Try something like this: var recentlyMoved = {};
var recentlyMovedTimers = {};
var removeRecentlyMoved = function(path) {
if (!recentlyMovedTimers[path]) return;
clearTimeout(recentlyMovedTimers[path]);
delete recentlyMovedTimers[path]
delete removeRecentlyMoved[path];
};
var checkIfWasMoved = function(path) {
if (recentlyMoved[path]) {
emit('rename', path);
removeRecentlyMoved(path);
} else {
recentlyMoved[path] = true;
recentlyMovedTimers[path] = setTimeout(removeRecentlyMoved.bind(null, path), 1000);
}
};
watcher.on('add', checkIfWasMoved);
watcher.on('remove', checkIfWasMoved); |
@CxRes that's a really good idea for how to handle the API when the data is available. I remain concerned that it will be difficult to relate the corresponding @paulmillr it's a nice idea, but also non-deterministic I'm afraid. Looks very vulnerable to races and edge cases. |
@es128 I too am having second thoughts about having code common between add & rename or unlink & rename. Well, a little code repetition is a small price to pay for this!!! But, I think there is no problem, if the user is prudent enough to do the following as long as Chokidar can guarantee that
On an unrelated note, I find that having |
Code repitition was not my concern - it's controlling the flow of events such that chokidar can guarantee that the It's not like chokidar is taking underlying |
Oh ok! I am afraid that even after staring at the code I do not understand the internals. Thanks for looking into it and would really appreciate a rename event soon. |
You could try @paulmillr's sample code to begin with and see if it works out for your particular use-case. I wouldn't expect this to be resolved very soon within chokidar (not by me, anyway). |
same needs here, or if only the events were always arriving in the same order… |
I'm also interested by a "rename" event. @es128 @paulmillr the sample code does not work. If I understand correctly, it detects files that are created and removed in the same second (and deleted and recreated). |
@nono the problem is: there is no "file system level" rename event per se, for all platforms and cases. So, this may be the best thing we can create |
Well, it's a bit more complicated than that:
I understand that it needs a lot of code and effort to have something usable and that masks the differences between the platform. Maybe using timers is a good way for handling renames. But I still think it should be included in chokidar, as it's not easy to get it right. It's like |
@nono I think it would be ok to explore ways to optionally expose the event in the modes/platforms where it is already made available by the lower-level layers, but then we have to take great care to reconcile the inconsistencies in behavior that could lead to. I do not think it will be possible to successfully shim this for situations like polling that do not provide the information. So a |
What about using inode numbers? We can compare them to check whether the paths of subsequent I know and I have checked that inode numbers stay the same for the following processes on both, files and directories under *nix environment:
Files only:
I am certain that windows doesn't have inode numbers and I strongly believe that windows lacks an alternative to it (please prove me wrong, internet). |
Any update on this issue ? I've the same request: I would like to know if a files is renamed/moved or added/removed.. For infos: it seems that fs.watch has a "rename" event. |
WONTFIX. Unreliable. Feel free to implement it on the user side. |
I wonder why no one paid attention to @zhirzh comment:
I tested Seems absolutely legit, what is stopping us? |
Feel free to fork chokidar and send a pull request. If it gets used and tested reliably, that could be fine. |
@NeekSandhu you should also test that |
@nono @NeekSandhu How about using |
@NeekSandhu You are right! I did not know that regarding API: I had made one suggestion above which @es128 agrees with. Alternatively one can have a boolean flag |
My two cents. The API should absolutely be broken. Chock it up (no pun intended) to what a library is supposed to do: upgrade semver when the need is real. This is a very appropriate, and I would argue, correct example to break the API over. |
My 2¢: After doing lots of research I can say that cross-platform rename in node leaves much to be desired. We couldn't find a way to do it without timeouts, which creates dangerous race conditions. FWIW we went with https://github.com/gorakhargosh/watchdog, which emits a |
I've implemented this here, it was somewhat tricky as the order of the "add" and "unlink" event is not consistent across platforms. I'm probably going to publish it on NPM for easier consumption in the future. |
For your information, several files/folders can have the same |
@nono thanks for pointing that out! It seems the problem would be solvable if the stats object were to include bigints, any way to make sure chokidar provides me stats objects with bigints? |
You can use make a pull request for making chokidar calls |
Using chokidar with NodeAll the File The Here's a short piece of code to do the same
|
@manav5hah there are some subtleties to be aware of, a simplistic approach like that doesn't actually work:
I've sort of rewritten chokidar but with rename detection support btw, the best option is probably to just use my watcher if you need that: https://github.com/fabiospampinato/watcher |
if event is "unlink", how i get fs.stats??? |
i think "add" and "remove" events path is different ,can you check by path? |
Watcher seems to only support ESM, not CommonJS. And as it stands, I believe this won't work in Electron due to Electron only supporting CommonJS. At least until 28 is released as stable. Am I missing something here? |
@lindenkron I use it in Electron myself, so I guess it works there? Maybe you are missing a build step or something? |
Hmm, perhaps. I attempted to use Watcher with You are using Watcher with CommonJS? Finding it odd that I would get an error like that only when adding watcher, but Chokidar had no issues with CommonJS. Not sure what else I can try, but thanks for the quick reply 🙏 |
You should probably open an issue in But basically watcher doesn't support commonjs, you'd have to |
I'd imagine this might have the possibility to vary by file system, so it'd probably be good to specify whether tested on ext4, btrfs, etc. And of course if you move a file to a different disk or something it will end up with a new inode, which is a caveat to document. |
I don't think that's Node-specific, but is due to how
I would expect they're using timeouts as well.
|
I think that it could be done without timeouts if using
|
would be very conveinent if chokidar emitted 'rename' events when a file is renamed. Currently you get an "add" and "unlink" event, and it is challenging to detect that a file was renamed. Especially because it seems that the "unlink" and "add" events don't always emit in the same order when renaming a file.
The text was updated successfully, but these errors were encountered: