From 06016cb2ace5e670d92bdf0c5c8b7873425042b4 Mon Sep 17 00:00:00 2001 From: facundoolano Date: Thu, 15 Feb 2024 18:02:24 -0300 Subject: [PATCH] improve handling of spurious files when serving --- commands/serve.go | 33 ++++++++++++++++++++++----------- site/site.go | 8 +------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/commands/serve.go b/commands/serve.go index f52ddcb..2c2151d 100644 --- a/commands/serve.go +++ b/commands/serve.go @@ -1,6 +1,7 @@ package commands import ( + "errors" "fmt" "io/fs" "net/http" @@ -79,21 +80,31 @@ func setupWatcher() (*fsnotify.Watcher, error) { return } + // some events can be received for temporary files, e.g. .#backup files generated by emacs while editing .org + // when events regarding such files arrive, discard them here instead of triggering a faulty rebuild + if _, err := os.Stat(event.Name); errors.Is(err, os.ErrNotExist) { + // path/to/whatever does not exist + // FIXME change for debug + fmt.Println("ignoring temporary file", event.Name) + continue + } // chmod events are noisy, ignore them if !event.Has(fsnotify.Chmod) { - fmt.Printf("\nFile %s changed, triggering rebuild.\n", event.Name) + continue + } - // since new nested directories could be triggering this change, and we need to watch those too - // and since re-watching files is a noop, I just re-add the entire src everytime there's a change - if err := addAll(watcher); err != nil { - fmt.Println("error:", err) - return - } + fmt.Printf("\nFile %s changed, triggering rebuild.\n", event.Name) - if err := rebuild(); err != nil { - fmt.Println("error:", err) - return - } + // since new nested directories could be triggering this change, and we need to watch those too + // and since re-watching files is a noop, I just re-add the entire src everytime there's a change + if err := addAll(watcher); err != nil { + fmt.Println("error:", err) + return + } + + if err := rebuild(); err != nil { + fmt.Println("error:", err) + return } case err, ok := <-watcher.Errors: diff --git a/site/site.go b/site/site.go index d9f023b..68595e0 100644 --- a/site/site.go +++ b/site/site.go @@ -123,14 +123,8 @@ func (site *Site) loadTemplates(srcDir string) error { err = filepath.WalkDir(srcDir, func(path string, entry fs.DirEntry, err error) error { if !entry.IsDir() { - // I've seen issues with temporary files, eg. .#name.org generated by emacs - // I'll just ignore changes to dotfiles to stay on the safe side - if strings.HasPrefix(filepath.Base(path), ".") { - return nil - } - templ, err := templates.Parse(site.templateEngine, path) - // if sometime fails or this is not a template, skip + // if something fails or this is not a template, skip if err != nil || templ == nil { return err }