Skip to content

Commit eb8d310

Browse files
committed
Fix race
1 parent cb61cb8 commit eb8d310

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

internal/project/watch.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type WatchedFiles[T any] struct {
5454
watchKind lsproto.WatchKind
5555
computeGlobPatterns func(input T) patternsAndIgnored
5656

57+
mu sync.RWMutex
5758
input T
5859
computeWatchersOnce sync.Once
5960
watchers []*lsproto.FileSystemWatcher
@@ -72,6 +73,8 @@ func NewWatchedFiles[T any](name string, watchKind lsproto.WatchKind, computeGlo
7273

7374
func (w *WatchedFiles[T]) Watchers() (WatcherID, []*lsproto.FileSystemWatcher) {
7475
w.computeWatchersOnce.Do(func() {
76+
w.mu.Lock()
77+
defer w.mu.Unlock()
7578
result := w.computeGlobPatterns(w.input)
7679
globs := result.patterns
7780
ignored := result.ignored
@@ -91,6 +94,9 @@ func (w *WatchedFiles[T]) Watchers() (WatcherID, []*lsproto.FileSystemWatcher) {
9194
w.id = watcherID.Add(1)
9295
}
9396
})
97+
98+
w.mu.RLock()
99+
defer w.mu.RUnlock()
94100
return WatcherID(fmt.Sprintf("%s watcher %d", w.name, w.id)), w.watchers
95101
}
96102

@@ -111,6 +117,8 @@ func (w *WatchedFiles[T]) WatchKind() lsproto.WatchKind {
111117
}
112118

113119
func (w *WatchedFiles[T]) Clone(input T) *WatchedFiles[T] {
120+
w.mu.RLock()
121+
defer w.mu.RUnlock()
114122
return &WatchedFiles[T]{
115123
name: w.name,
116124
watchKind: w.watchKind,

0 commit comments

Comments
 (0)