| 271 | } |
| 272 | |
| 273 | func newWatcher(paths []string) (Notify, error) { |
| 274 | fsw, err := fsnotify.NewWatcher() |
| 275 | if err != nil { |
| 276 | if strings.Contains(err.Error(), "too many open files") && runtime.GOOS == "linux" { |
| 277 | return nil, fmt.Errorf("hit OS limits creating a watcher.\n" + |
| 278 | "Run 'sysctl fs.inotify.max_user_instances' to check your inotify limits.\n" + |
| 279 | "To raise them, run 'sudo sysctl fs.inotify.max_user_instances=1024'") |
| 280 | } |
| 281 | return nil, fmt.Errorf("creating file watcher: %w", err) |
| 282 | } |
| 283 | MaybeIncreaseBufferSize(fsw) |
| 284 | |
| 285 | err = fsw.SetRecursive() |
| 286 | isWatcherRecursive := err == nil |
| 287 | |
| 288 | wrappedEvents := make(chan FileEvent) |
| 289 | notifyList := make(map[string]bool, len(paths)) |
| 290 | if isWatcherRecursive { |
| 291 | paths = pathutil.EncompassingPaths(paths) |
| 292 | } |
| 293 | for _, path := range paths { |
| 294 | path, err := filepath.Abs(path) |
| 295 | if err != nil { |
| 296 | return nil, fmt.Errorf("newWatcher: %w", err) |
| 297 | } |
| 298 | notifyList[path] = true |
| 299 | } |
| 300 | |
| 301 | wmw := &naiveNotify{ |
| 302 | notifyList: notifyList, |
| 303 | watcher: fsw, |
| 304 | events: fsw.Events, |
| 305 | wrappedEvents: wrappedEvents, |
| 306 | errors: fsw.Errors, |
| 307 | isWatcherRecursive: isWatcherRecursive, |
| 308 | } |
| 309 | |
| 310 | return wmw, nil |
| 311 | } |
| 312 | |
| 313 | var _ Notify = &naiveNotify{} |
| 314 | |