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 package commands
import ( import (
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"net/http" "net/http"
@ -79,8 +80,19 @@ func setupWatcher() (*fsnotify.Watcher, error) {
return 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 // chmod events are noisy, ignore them
if !event.Has(fsnotify.Chmod) { if !event.Has(fsnotify.Chmod) {
continue
}
fmt.Printf("\nFile %s changed, triggering rebuild.\n", event.Name) fmt.Printf("\nFile %s changed, triggering rebuild.\n", event.Name)
// since new nested directories could be triggering this change, and we need to watch those too // since new nested directories could be triggering this change, and we need to watch those too
@ -94,7 +106,6 @@ func setupWatcher() (*fsnotify.Watcher, error) {
fmt.Println("error:", err) fmt.Println("error:", err)
return return
} }
}
case err, ok := <-watcher.Errors: case err, ok := <-watcher.Errors:
if !ok { if !ok {

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 { err = filepath.WalkDir(srcDir, func(path string, entry fs.DirEntry, err error) error {
if !entry.IsDir() { 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) 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 { if err != nil || templ == nil {
return err return err
} }