Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion watch/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (d *Daemon) eventLoop() {
continue
}

if debouncer.shouldSkip(event, time.Now()) {
if d.shouldSkipEvent(debouncer, event, time.Now()) {
continue
}

Expand All @@ -132,6 +132,26 @@ func (d *Daemon) eventLoop() {
}
}

func (d *Daemon) shouldSkipEvent(debouncer *eventDebouncer, event fsnotify.Event, now time.Time) bool {
if !debouncer.shouldSkip(event, now) {
return false
}
info, err := os.Stat(event.Name)
if err != nil {
return false
}
relPath, err := filepath.Rel(d.root, event.Name)
if err != nil {
return false
}

d.graph.mu.RLock()
cached := d.graph.State[relPath]
matches := cached != nil && cached.Size == info.Size()
d.graph.mu.RUnlock()
return matches
}

// isSourceFile checks if a file should be tracked.
// Derives from the canonical extension registry in scanner.
func (d *Daemon) isSourceFile(path string) bool {
Expand Down
36 changes: 36 additions & 0 deletions watch/events_debounce_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
package watch

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/fsnotify/fsnotify"
)

func TestDaemonDoesNotDebounceWriteWhenCachedSizeIsStale(t *testing.T) {
root := t.TempDir()
path := filepath.Join(root, "main.go")
if err := os.WriteFile(path, []byte("package main\n\nfunc changed() {}\n"), 0o644); err != nil {
t.Fatal(err)
}

d := &Daemon{
root: root,
graph: &Graph{State: map[string]*FileState{
"main.go": {Size: 0},
}},
}
debouncer := newEventDebouncer(100 * time.Millisecond)
event := fsnotify.Event{Name: path, Op: fsnotify.Write}
base := time.Unix(0, 0)

if d.shouldSkipEvent(debouncer, event, base) {
t.Fatal("first write should not be skipped")
}
if d.shouldSkipEvent(debouncer, event, base.Add(10*time.Millisecond)) {
t.Fatal("final write should refresh stale cached state")
}

info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
d.graph.State["main.go"].Size = info.Size()
if !d.shouldSkipEvent(debouncer, event, base.Add(20*time.Millisecond)) {
t.Fatal("duplicate write should be skipped once cached state is current")
}
}

func TestEventDebouncerSkipsRapidWrites(t *testing.T) {
debouncer := newEventDebouncer(100 * time.Millisecond)
base := time.Unix(0, 0)
Expand Down
2 changes: 1 addition & 1 deletion watch/more_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestDaemonStartTracksWriteEventsAndState(t *testing.T) {

waitForWatchCondition(t, 2*time.Second, func() bool {
events := d.GetEvents(0)
return len(events) > 0
return len(events) > 0 && events[len(events)-1].Path == "main.go" && events[len(events)-1].Delta > 0
})

events := d.GetEvents(0)
Expand Down