Auto-reload config file on RENAME

Some editors (like Vim), create a temp file when saving, then replace
the file being edited with the temp file. This causes the FS notify
event to be RENAME, not WRITE, so auto-reload misses this.

In addition to that, the file is removed from the watcher and the
auto-reload functionality stops working entirely after the first RENAME.

https://github.com/fsnotify/fsnotify/issues/255 describes this.
This commit is contained in:
Sergio Rubio 2025-02-17 19:02:40 +01:00
parent d8a4d39849
commit f7f333ad52

View File

@ -286,6 +286,20 @@ func configFilesWatcher(
} }
if event.Has(fsnotify.Write) { if event.Has(fsnotify.Write) {
debouncedCallback() debouncedCallback()
} else if event.Has(fsnotify.Rename) {
// wait for file to be available
for i := 0; i < 20; i++ {
_, err := os.Stat(mainFileAbsPath)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
err := watcher.Add(mainFileAbsPath)
if err != nil {
onErr(fmt.Errorf("watching file:", err))
}
debouncedCallback()
} else if event.Has(fsnotify.Remove) { } else if event.Has(fsnotify.Remove) {
func() { func() {
mu.Lock() mu.Lock()