knadh / koanf

Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

File watching error when saving with vi/vim or neovim

drembaru opened this issue · comments

Describe the bug

Watching for config file changes fails when the file is edited and saved with vi, vim or neovim (on Linux).

To Reproduce

Load a config file, watch for changes and modify with vim/neovim. The Watch function returns the error

"err":"lstat config.yaml: no such file or directory"

and the Watch routine is stopped.

Expected behavior

Do not return an error and do not stop the Watch routine.

Please provide the following information):

  • OS: Debian 12
  • Koanf Version: v2.1.0

Additional context

The problem seems to be related to the way vim/neovim save files. The only (somewhat) related issue I could find was from the Viper project: spf13/viper#142. Trying all the editors/commands from there I only get errors using vim/neovim. All the other editors/commands work fine.

Looking at the file Watch code I can avoid the error when I change the following:

				curPath, err := filepath.EvalSymlinks(f.path)
				if err != nil {
					cb(nil, err)
					break loop
				}

to

				curPath, err := filepath.EvalSymlinks(f.path)
				if err != nil {
					continue
				}

I doubt that this is the correct fix so I didn't open a pull request :). Since this only happens for vi/vim/neovim (from all the editors I tried) this might not be too important. If it is not important enough to be fixed this isuse could maybe help others figure out what the problem is.

Thank for creating koanf 👍

Vim: "config.toml": CHMOD, then "config.toml": REMOVE, then the watcher stops working.

Hi @drembaru. It seems to be this case (from the linked issue), where vim removes the file and re-creates this. I'm not sure how this situation could be handled. koanf uses the fsnotify lib for watching files. Perhaps you could check the issues there to see if there is a solution for this behaviour?

https://github.com/fsnotify/fsnotify