From fa217fdab64e19e607f7b163b9be2eca893937d0 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 1 Dec 2022 03:40:59 -0800 Subject: [PATCH] watch.go: Ignore first error in ensureAddWatch Will prevent the benign watching errors we've been seeing. I feel like there was an open issue for this but I cannot find it. --- ci/release/changelogs/next.md | 4 +++- watch.go | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 25d993081..87fc8f796 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -36,5 +36,7 @@ [#159](https://github.com/terrastruct/d2/issues/159) - Fixes markdown newlines created with a trailing double space or backslash. [#214](https://github.com/terrastruct/d2/pull/214) -- Fixes images not loading in PNG exports +- Fixes images not loading in PNG exports. [#224](https://github.com/terrastruct/d2/pull/224) +- Avoid logging benign file watching errors. + [#293](https://github.com/terrastruct/d2/pull/293) diff --git a/watch.go b/watch.go index a09034e4d..97a5fe521 100644 --- a/watch.go +++ b/watch.go @@ -251,7 +251,7 @@ func (w *watcher) watchLoop(ctx context.Context) error { // We missed changes. lastModified = mt } - // The purpose of eatBurstTimer is to wait at least 32 milliseconds after a sequence of + // The purpose of eatBurstTimer is to wait at least 16 milliseconds after a sequence of // events to ensure that whomever is editing the file is now done. // // For example, On macOS editing with neovim, every write I see a chmod immediately @@ -261,7 +261,7 @@ func (w *watcher) watchLoop(ctx context.Context) error { // Another example would be a very large file where one logical edit becomes write // events. We wouldn't want to try to compile an incomplete file and then report a // misleading error. - eatBurstTimer.Reset(time.Millisecond * 32) + eatBurstTimer.Reset(time.Millisecond * 16) case <-eatBurstTimer.C: w.ms.Log.Info.Printf("detected change in %v: recompiling...", w.inputPath) w.requestCompile() @@ -284,7 +284,7 @@ func (w *watcher) requestCompile() { } func (w *watcher) ensureAddWatch(ctx context.Context) (time.Time, error) { - interval := time.Second + interval := time.Millisecond * 16 tc := time.NewTimer(0) <-tc.C for { @@ -292,11 +292,16 @@ func (w *watcher) ensureAddWatch(ctx context.Context) (time.Time, error) { if err == nil { return mt, nil } - w.ms.Log.Error.Printf("failed to watch inputPath %q: %v (retrying in %v)", w.inputPath, err, interval) + if interval >= time.Second { + w.ms.Log.Error.Printf("failed to watch inputPath %q: %v (retrying in %v)", w.inputPath, err, interval) + } tc.Reset(interval) select { case <-tc.C: + if interval < time.Second { + interval = time.Second + } if interval < time.Second*16 { interval *= 2 }