From d6470ae8140c0824ab12c8305d86f9b22209c630 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Fri, 29 Nov 2024 21:45:50 +0000 Subject: [PATCH] Somewhat working fix for config watcher --- internal/glance/config.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/internal/glance/config.go b/internal/glance/config.go index 9568fc7..f69f1a3 100644 --- a/internal/glance/config.go +++ b/internal/glance/config.go @@ -189,6 +189,9 @@ func configFilesWatcher( updateWatchedIncludes(nil, lastIncludes) + // needed for lastContents and lastIncludes because they get updated in multiple goroutines + mu := sync.Mutex{} + checkForContentChangesBeforeCallback := func() { currentContents, currentIncludes, err := parseYAMLIncludes(mainFilePath) if err != nil { @@ -196,6 +199,9 @@ func configFilesWatcher( return } + mu.Lock() + defer mu.Unlock() + if !bytes.Equal(lastContents, currentContents) { updateWatchedIncludes(lastIncludes, currentIncludes) lastContents, lastIncludes = currentContents, currentIncludes @@ -223,17 +229,13 @@ func configFilesWatcher( } if event.Has(fsnotify.Write) { debouncedCallback() - } - // maybe also handle .Remove event? - // from testing it appears that a removed file will stop triggering .Write events - // when it gets recreated, in which case we may need to watch the directory for the - // creation of that file and then re-add it to the watcher, though that's - // a lot of effort for a hopefully rare edge case + } else if event.Has(fsnotify.Remove) { + mu.Lock() + delete(lastIncludes, event.Name) + mu.Unlock() - // TODO: update - try and fix this for v0.7.0 - // so, about that "rare edge case"... it's not so rare - // guess what happens when you run `git pull` and a file has changes? - // yeah, it gets removed and re-added ( : + debouncedCallback() + } case err, isOpen := <-watcher.Errors: if !isOpen { return