Skip to content

Commit

Permalink
resolve symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
fatanugraha committed Jan 9, 2025
1 parent 15645e5 commit 87bd6ed
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/utils/filewatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ func NewFileWatcher(path string) (*FileWatcher, error) {
}

func (fw *FileWatcher) Start(changeHandler func()) error {
// Ensure that the target that we're watching is not a symlink as we won't get any events when we're watching
// a symlink.
fileRealPath, err := filepath.EvalSymlinks(fw.path)
if err != nil {
return fmt.Errorf("adding watcher failed: %s", err)
}

// watch the directory instead of the individual file to ensure the notification still works when the file is modified
// through moving/renaming rather than writing into it directly (like what most modern editor does by default).
// ref: https://github.com/fsnotify/fsnotify/blob/a9bc2e01792f868516acf80817f7d7d7b3315409/README.md#watching-a-file-doesnt-work-well
err := fw.w.Add(filepath.Dir(fw.path))
if err != nil {
if err = fw.w.Add(filepath.Dir(fileRealPath)); err != nil {
return fmt.Errorf("adding watcher failed: %s", err)
}

Expand All @@ -47,7 +53,7 @@ func (fw *FileWatcher) Start(changeHandler func()) error {
return // watcher is closed.
}

if event.Name != fw.path {
if event.Name != fileRealPath {
continue // we don't care about this file.
}

Expand Down

0 comments on commit 87bd6ed

Please sign in to comment.