notify-rs/notify

Catch no events after saving a watching file with Vim editor

rhysd opened this issue · 1 comments

rhysd commented

System details

  • OS/Platform name and version: Ubuntu 22.04
  • Rust version (if building from source): rustc --version: rustc 1.72.0 (5680fa18f 2023-08-23)
  • Notify version (or commit hash if building from git): 4a00121
  • If you're coming from a project that makes use of Notify, what it is, and a link to the downstream issue if there is one: N/A
  • Filesystem type and options: ext4 (inotify)
  • On Linux: Kernel version: 6.2.0
  • On Windows: version and if you're running under Windows, Cygwin (unsupported), Linux Subsystem: N/A
  • If you're running as a privileged user (root, System): No
  • If you're running in a container, details on the runtime and overlay: No
  • If you're running in a VM, details on the hypervisor: Yes: VirtualBox 7.0

What you did (as detailed as you can)

  1. git clone https://github.com/notify-rs/notify.git && cd ./notify
  2. cargo run --example monitor_raw README.md
  3. Open another terminal window
  4. vim -u NONE -N /path/to/notify/README.md
  5. Edit some text and save it with :w
  6. Remove(File) event is reported by monitor_raw example
  7. Edit more text and save it with :w again
  8. Nothing is reported

Actual output from the example is as follows:

[2023-09-03T13:52:07Z INFO  monitor_raw] Watching README.md
[2023-09-03T13:52:28Z INFO  monitor_raw] Change: Event { kind: Modify(Name(From)), paths: ["/home/rhysd/Dev/notify/README.md"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None }
[2023-09-03T13:52:28Z INFO  monitor_raw] Change: Event { kind: Modify(Metadata(Any)), paths: ["/home/rhysd/Dev/notify/README.md"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None }
[2023-09-03T13:52:28Z INFO  monitor_raw] Change: Event { kind: Remove(File), paths: ["/home/rhysd/Dev/notify/README.md"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None }

What you expected

Files are watched after saving them by Vim.

What happened

If my understanding is correct, INotifyWatcher removes the watching target when receiving Remove event.

remove_watch_by_event(&path, &self.watches, &mut remove_watches);

This is usually not a problem. Editors like Nano or VSCode or Emacs don't remove a file when saving it. However, Vim's behavior is different. On :w, Vim's default behavior (:set nobackup writebackup) is as follows:

  1. Move current file (e.g. README.md) to a backup file (e.g. README.md.bak)
  2. Create a new file and write the current text buffer to the file
  3. When writing the file was successful, it deletes the backup file

The watching file is deleted after renaming to a backup file and a new file is created with the same name. INotifyWatcher seems to stop watching the file when Vim renames it.

EDIT: I heard that sed -i does this. It might be common to rename the original file as backup and create a new file for softwares which provides some file backup feature.

0xpr03 commented

Kind of listed in the Known-Issues section. You need to watch the whole folder in that case.