#717·fsnotify

macOS 监视器通知中遗漏了对已删除并重新创建的文件的写入

作者: prashantv创建于 2025年10月16日更新于 2026年3月30日
标签bugkqueue

// The test watches a directory with file config.yaml, initially with value "initial". // Once the watcher is created and setup, the file is removed, then recreated with value "recreated". // The expectation is that the watcher eventually sees the new "recreated" value when reading the file on a watcher event. func runTest(t testing.TB, dir, filename string) { watcher, err := fsnotify.NewWatcher() require.NoError(t, err) defer watcher.Close() require.NoError(t, watcher.Add(dir)) gotContent := make(chan string, 10) wg := newStoppableWG() defer wg.Stop() wg.Go(func(ctx context.Context) { for { select { case <-ctx.Done(): return case err, ok := <-watcher.Errors: if !ok { t.Log("fsnotify.Watcher.Errors channel closed") return } if err == nil { t.Fatal("fsnotify.Watcher.Errors returned a nil error") } else { t.Fatal("watcher got error:", err) } case ev, ok := <-watcher.Events: if !ok { t.Log("fsnotify.Watcher.Events channel closed") return } t.Log("fsnotify.Watcher event", ev.Op, filepath.Base(ev.Name)) content, err := os.ReadFile(filename) if err != nil { if os.IsNotExist(err) && ev.Op == fsnotify.Remove { // REMOVE notification followed by not-found error is expected. continue } t.Log("ReadFile failed: ", err) continue } t.Logf("Read file content: %q", content) gotContent <- string(content) } } } }

内容来源: fsnotify/fsnotify