Skip to content

Commit 1f8a6ca

Browse files
authored
fix: watcher bug (#284)
* fix: watcher bug * fix: linting
1 parent 9a5ffa9 commit 1f8a6ca

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

common/watcher/watcher.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package watcher
33
import (
44
"context"
55
"fmt"
6+
"io/fs"
67
"os"
78
"path/filepath"
89
"time"
@@ -161,9 +162,34 @@ func (w *FileWatcher) handleEvent(event fsnotify.Event) {
161162
switch event.Op {
162163
case fsnotify.Create, fsnotify.Write:
163164
// Check if this is actually a file (not a directory)
164-
if info, err := os.Stat(filePath); err == nil && !info.IsDir() {
165+
info, err := os.Stat(filePath)
166+
if err == nil && !info.IsDir() {
165167
w.handler.OnFileChanged(filePath)
166168
}
169+
if err == nil && info.IsDir() {
170+
err := w.watcher.Add(filePath)
171+
if err != nil {
172+
w.log.Error().Err(err).Str("path", filePath).Msg("failed to add directory to watcher")
173+
return
174+
}
175+
err = filepath.WalkDir(filePath, func(path string, d fs.DirEntry, err error) error {
176+
if err != nil {
177+
return err
178+
}
179+
// Skip directories
180+
if d.IsDir() {
181+
return nil
182+
}
183+
w.handler.OnFileChanged(path)
184+
185+
return nil
186+
})
187+
if err != nil {
188+
w.log.Error().Err(err).Str("path", filePath).Msg("failed to walk directory")
189+
return
190+
}
191+
}
192+
167193
case fsnotify.Rename, fsnotify.Remove:
168194
w.handler.OnFileDeleted(filePath)
169195
default:

0 commit comments

Comments
 (0)