Describe the bug
File watcher prints empty string data after file gets modified a couple of times.
To Reproduce
- Run the go snippet below
- Modify
rpc value from config.yaml file
- Save
config.yaml
- Repeat steps 2-3 until issue happens
main.go:
package main
import (
"fmt"
"log"
"time"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
)
func main() {
k := koanf.New(".")
provider := file.Provider("config.yaml")
err := k.Load(provider, yaml.Parser())
if err != nil {
panic(err)
}
provider.Watch(func(event interface{}, err error) {
if err != nil {
log.Printf("watch error: %v", err)
return
}
// Throw away the old config and load a fresh copy.
log.Println("config changed. Reloading ...")
k = koanf.New(".")
err = k.Load(provider, yaml.Parser())
if err != nil {
log.Printf("watch error: %v", err)
return
}
})
go func() {
for {
fmt.Println(k.String("rpc"))
time.Sleep(time.Second)
}
}()
<-make(chan bool)
}
config.yaml:
Screenshot showing the described behavior:

Expected behavior
Updated rpc value is correctly shown on logs.
Please provide the following information):
- OS: Linux 6.5.0-41-generic # 41~22.04.2-Ubuntu SMP PREEMPT_DYNAMIC
- Koanf Version: v2.1.1
- go version: go version go1.21.6 linux/amd64
Additional context
Describe the bug
File watcher prints empty string data after file gets modified a couple of times.
To Reproduce
rpcvalue fromconfig.yamlfileconfig.yamlmain.go:config.yaml:Screenshot showing the described behavior:

Expected behavior
Updated
rpcvalue is correctly shown on logs.Please provide the following information):
Additional context