improve handling of spurious files when serving

This commit is contained in:
facundoolano 2024-02-15 18:02:24 -03:00
parent 11d5e0f2d3
commit 06016cb2ac
2 changed files with 23 additions and 18 deletions

View file

@ -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:

View file

@ -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
}