@@ -264,6 +264,12 @@ func (w *watcher) watchLoop(ctx context.Context) error {
264264 return errors .New ("fsnotify watcher closed" )
265265 }
266266 w .ms .Log .Debug .Printf ("received file system event %v" , ev )
267+
268+ if isTemp , reason := isBackupFile (ev .Name ); isTemp {
269+ w .ms .Log .Debug .Printf ("skipping event for %q: detected as %s" , w .ms .HumanPath (ev .Name ), reason )
270+ continue
271+ }
272+
267273 mt , err := w .ensureAddWatch (ctx , ev .Name )
268274 if err != nil {
269275 return err
@@ -349,6 +355,11 @@ func (w *watcher) ensureAddWatch(ctx context.Context, path string) (time.Time, e
349355}
350356
351357func (w * watcher ) addWatch (ctx context.Context , path string ) (time.Time , error ) {
358+ if isTemp , reason := isBackupFile (path ); isTemp {
359+ w .ms .Log .Debug .Printf ("skipping watch for %q: detected as %s" , w .ms .HumanPath (path ), reason )
360+ return time.Time {}, nil
361+ }
362+
352363 err := w .fw .Add (path )
353364 if err != nil {
354365 return time.Time {}, err
@@ -671,3 +682,41 @@ func (tfs *trackedFS) Open(name string) (fs.File, error) {
671682 }
672683 return f , err
673684}
685+
686+ func isBackupFile (path string ) (bool , string ) {
687+ ext := filepath .Ext (path )
688+ baseName := filepath .Base (path )
689+
690+ // This list is based off of https://github.com/gohugoio/hugo/blob/master/commands/hugobuilder.go#L795
691+ switch {
692+ case strings .HasSuffix (ext , "~" ):
693+ return true , "generic backup file (~)"
694+ case ext == ".swp" :
695+ return true , "vim swap file"
696+ case ext == ".swx" :
697+ return true , "vim swap file"
698+ case ext == ".tmp" :
699+ return true , "generic temp file"
700+ case ext == ".DS_Store" :
701+ return true , "OSX thumbnail"
702+ case ext == ".bck" :
703+ return true , "Helix backup"
704+ case baseName == "4913" :
705+ return true , "vim temp file"
706+ case strings .HasPrefix (ext , ".goutputstream" ):
707+ return true , "GNOME temp file"
708+ case strings .HasSuffix (ext , "jb_old___" ):
709+ return true , "IntelliJ old backup"
710+ case strings .HasSuffix (ext , "jb_tmp___" ):
711+ return true , "IntelliJ temp file"
712+ case strings .HasSuffix (ext , "jb_bak___" ):
713+ return true , "IntelliJ backup"
714+ case strings .HasPrefix (ext , ".sb-" ):
715+ return true , "Byword temp file"
716+ case strings .HasPrefix (baseName , ".#" ):
717+ return true , "Emacs lock file"
718+ case strings .HasPrefix (baseName , "#" ):
719+ return true , "Emacs temp file"
720+ }
721+ return false , ""
722+ }
0 commit comments