Skip to content

Commit

Permalink
fix: stop double-processing generation in watch mode, now that the ge…
Browse files Browse the repository at this point in the history
…neration is identical
  • Loading branch information
a-h committed Jan 1, 2025
1 parent 3643c9b commit 1f94c7b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ go.work

# direnv
.direnv

# templ txt files.
*_templ.txt
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.812
0.3.817
19 changes: 1 addition & 18 deletions cmd/templ/generatecmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 13 additions & 5 deletions cmd/templ/generatecmd/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "", "")
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1f94c7b

Please sign in to comment.