1
0
mirror of https://github.com/a-h/templ.git synced 2025-02-06 10:03:16 +00:00

fix: stop double-processing generation in watch mode, now that the generation is identical

This commit is contained in:
Adrian Hesketh 2025-01-01 13:36:40 +00:00
parent 3643c9b565
commit 1f94c7b135
No known key found for this signature in database
GPG Key ID: 9E01387222323123
5 changed files with 20 additions and 24 deletions

3
.gitignore vendored
View File

@ -29,3 +29,6 @@ go.work
# direnv
.direnv
# templ txt files.
*_templ.txt

View File

@ -1 +1 @@
0.3.812
0.3.817

View File

@ -185,26 +185,9 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
)
postGenerationEventsWG.Wait()
cmd.Log.Debug(
"All post-generation events processed, running walk again, but in production mode",
"All post-generation events processed",
slog.Int64("errorCount", errorCount.Load()),
)
// Reset to reprocess all files in production mode.
fseh = NewFSEventHandler(
cmd.Log,
cmd.Args.Path,
false, // Force production mode.
opts,
cmd.Args.GenerateSourceMapVisualisations,
cmd.Args.KeepOrphanedFiles,
cmd.Args.FileWriter,
cmd.Args.Lazy,
)
errorCount.Store(0)
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.WatchPattern, events); err != nil {
cmd.Log.Error("Post dev mode WalkFiles failed", slog.Any("error", err))
errs <- FatalError{Err: fmt.Errorf("failed to walk files: %w", err)}
return
}
}()
// Start process to handle events.

View File

@ -133,17 +133,25 @@ func (h *FSEventHandler) HandleEvent(ctx context.Context, event fsnotify.Event)
return GenerateResult{}, nil
}
// Handle .templ files.
if !strings.HasSuffix(event.Name, ".templ") {
return GenerateResult{}, nil
}
// If the file hasn't been updated since the last time we processed it, ignore it.
lastModTime, updatedModTime := h.UpsertLastModTime(event.Name)
if !updatedModTime {
h.Log.Debug("Skipping file because it wasn't updated", slog.String("file", event.Name))
return GenerateResult{}, nil
}
// Process anything that isn't a templ file.
if !strings.HasSuffix(event.Name, ".templ") {
// If it's a Go file, mark it as updated.
if strings.HasSuffix(event.Name, ".go") {
result.GoUpdated = true
}
result.Updated = true
return result, nil
}
// Handle templ files.
// If the go file is newer than the templ file, skip generation, because it's up-to-date.
if h.lazy && goFileIsUpToDate(event.Name, lastModTime) {
h.Log.Debug("Skipping file because the Go file is up-to-date", slog.String("file", event.Name))

View File

@ -208,6 +208,7 @@ func generateCmd(stdout, stderr io.Writer, args []string) (code int) {
includeVersionFlag := cmd.Bool("include-version", true, "")
includeTimestampFlag := cmd.Bool("include-timestamp", false, "")
watchFlag := cmd.Bool("watch", false, "")
watchPatternFlag := cmd.String("watch-pattern", "(.+\\.go$)|(.+\\.templ$)|(.+_templ\\.txt$)", "")
openBrowserFlag := cmd.Bool("open-browser", true, "")
cmdFlag := cmd.String("cmd", "", "")
proxyFlag := cmd.String("proxy", "", "")
@ -252,6 +253,7 @@ func generateCmd(stdout, stderr io.Writer, args []string) (code int) {
Path: *pathFlag,
FileWriter: fw,
Watch: *watchFlag,
WatchPattern: *watchPatternFlag,
OpenBrowser: *openBrowserFlag,
Command: *cmdFlag,
Proxy: *proxyFlag,