Skip to content

Commit 1f94c7b

Browse files
committed
fix: stop double-processing generation in watch mode, now that the generation is identical
1 parent 3643c9b commit 1f94c7b

File tree

5 files changed

+20
-24
lines changed

5 files changed

+20
-24
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ go.work
2929

3030
# direnv
3131
.direnv
32+
33+
# templ txt files.
34+
*_templ.txt

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.812
1+
0.3.817

cmd/templ/generatecmd/cmd.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,26 +185,9 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
185185
)
186186
postGenerationEventsWG.Wait()
187187
cmd.Log.Debug(
188-
"All post-generation events processed, running walk again, but in production mode",
188+
"All post-generation events processed",
189189
slog.Int64("errorCount", errorCount.Load()),
190190
)
191-
// Reset to reprocess all files in production mode.
192-
fseh = NewFSEventHandler(
193-
cmd.Log,
194-
cmd.Args.Path,
195-
false, // Force production mode.
196-
opts,
197-
cmd.Args.GenerateSourceMapVisualisations,
198-
cmd.Args.KeepOrphanedFiles,
199-
cmd.Args.FileWriter,
200-
cmd.Args.Lazy,
201-
)
202-
errorCount.Store(0)
203-
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.WatchPattern, events); err != nil {
204-
cmd.Log.Error("Post dev mode WalkFiles failed", slog.Any("error", err))
205-
errs <- FatalError{Err: fmt.Errorf("failed to walk files: %w", err)}
206-
return
207-
}
208191
}()
209192

210193
// Start process to handle events.

cmd/templ/generatecmd/eventhandler.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,25 @@ func (h *FSEventHandler) HandleEvent(ctx context.Context, event fsnotify.Event)
133133
return GenerateResult{}, nil
134134
}
135135

136-
// Handle .templ files.
137-
if !strings.HasSuffix(event.Name, ".templ") {
138-
return GenerateResult{}, nil
139-
}
140-
141136
// If the file hasn't been updated since the last time we processed it, ignore it.
142137
lastModTime, updatedModTime := h.UpsertLastModTime(event.Name)
143138
if !updatedModTime {
144139
h.Log.Debug("Skipping file because it wasn't updated", slog.String("file", event.Name))
145140
return GenerateResult{}, nil
146141
}
142+
143+
// Process anything that isn't a templ file.
144+
if !strings.HasSuffix(event.Name, ".templ") {
145+
// If it's a Go file, mark it as updated.
146+
if strings.HasSuffix(event.Name, ".go") {
147+
result.GoUpdated = true
148+
}
149+
result.Updated = true
150+
return result, nil
151+
}
152+
153+
// Handle templ files.
154+
147155
// If the go file is newer than the templ file, skip generation, because it's up-to-date.
148156
if h.lazy && goFileIsUpToDate(event.Name, lastModTime) {
149157
h.Log.Debug("Skipping file because the Go file is up-to-date", slog.String("file", event.Name))

cmd/templ/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func generateCmd(stdout, stderr io.Writer, args []string) (code int) {
208208
includeVersionFlag := cmd.Bool("include-version", true, "")
209209
includeTimestampFlag := cmd.Bool("include-timestamp", false, "")
210210
watchFlag := cmd.Bool("watch", false, "")
211+
watchPatternFlag := cmd.String("watch-pattern", "(.+\\.go$)|(.+\\.templ$)|(.+_templ\\.txt$)", "")
211212
openBrowserFlag := cmd.Bool("open-browser", true, "")
212213
cmdFlag := cmd.String("cmd", "", "")
213214
proxyFlag := cmd.String("proxy", "", "")
@@ -252,6 +253,7 @@ func generateCmd(stdout, stderr io.Writer, args []string) (code int) {
252253
Path: *pathFlag,
253254
FileWriter: fw,
254255
Watch: *watchFlag,
256+
WatchPattern: *watchPatternFlag,
255257
OpenBrowser: *openBrowserFlag,
256258
Command: *cmdFlag,
257259
Proxy: *proxyFlag,

0 commit comments

Comments
 (0)