Skip to content

Commit cdb2209

Browse files
authored
Merge pull request #2345 from alixander/watch-ignore-backups
cli: ignore watch events from backup files
2 parents 4a73ca9 + 7d070d2 commit cdb2209

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

ci/release/changelogs/next.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
- d2cli:
3030
- Support `validate` command. [#2415](https://github.com/terrastruct/d2/pull/2415)
31+
- Watch mode ignores backup files (e.g. files created by certain editors like Helix). [#2131](https://github.com/terrastruct/d2/issues/2131)
3132
- d2compiler:
3233
- `link`s can be set to root path, e.g. `/xyz`. [#2357](https://github.com/terrastruct/d2/issues/2357)
3334

d2cli/watch.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

351357
func (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

Comments
 (0)