Skip to content

Commit 24be99e

Browse files
authored
fix: delete watchmode files when shutdown is sent (#1046)
1 parent 8b05ded commit 24be99e

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

cmd/templ/generatecmd/cmd.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"regexp"
1414
"runtime"
15+
"strings"
1516
"sync"
1617
"sync/atomic"
1718
"time"
@@ -185,9 +186,26 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
185186
)
186187
postGenerationEventsWG.Wait()
187188
cmd.Log.Debug(
188-
"All post-generation events processed",
189+
"All post-generation events processed, deleting watch mode text files",
189190
slog.Int64("errorCount", errorCount.Load()),
190191
)
192+
193+
fileEvents := make(chan fsnotify.Event)
194+
go func() {
195+
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.WatchPattern, fileEvents); err != nil {
196+
cmd.Log.Error("Post dev mode WalkFiles failed", slog.Any("error", err))
197+
errs <- FatalError{Err: fmt.Errorf("failed to walk files: %w", err)}
198+
return
199+
}
200+
close(fileEvents)
201+
}()
202+
for event := range fileEvents {
203+
if strings.HasSuffix(event.Name, "_templ.txt") {
204+
if err = os.Remove(event.Name); err != nil {
205+
cmd.Log.Warn("Failed to remove watch mode text file", slog.Any("error", err))
206+
}
207+
}
208+
}
191209
}()
192210

193211
// Start process to handle events.

cmd/templ/generatecmd/main_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"path"
99
"regexp"
1010
"testing"
11+
"time"
1112

1213
"github.com/a-h/templ/cmd/templ/testproject"
14+
"golang.org/x/sync/errgroup"
1315
)
1416

1517
func TestGenerate(t *testing.T) {
@@ -42,6 +44,58 @@ func TestGenerate(t *testing.T) {
4244
t.Fatalf("templates_templ.go was not created: %v", err)
4345
}
4446
})
47+
t.Run("can generate a file in watch mode", func(t *testing.T) {
48+
// templ generate -f templates.templ
49+
dir, err := testproject.Create("github.com/a-h/templ/cmd/templ/testproject")
50+
if err != nil {
51+
t.Fatalf("failed to create test project: %v", err)
52+
}
53+
defer os.RemoveAll(dir)
54+
55+
// Delete the templates_templ.go file to ensure it is generated.
56+
err = os.Remove(path.Join(dir, "templates_templ.go"))
57+
if err != nil {
58+
t.Fatalf("failed to remove templates_templ.go: %v", err)
59+
}
60+
ctx, cancel := context.WithCancel(context.Background())
61+
62+
var eg errgroup.Group
63+
eg.Go(func() error {
64+
// Run the generate command.
65+
return Run(ctx, log, Arguments{
66+
Path: dir,
67+
Watch: true,
68+
})
69+
})
70+
71+
// Check the templates_templ.go file was created, with backoff.
72+
for i := 0; i < 5; i++ {
73+
time.Sleep(time.Second * time.Duration(i))
74+
_, err = os.Stat(path.Join(dir, "templates_templ.go"))
75+
if err != nil {
76+
continue
77+
}
78+
_, err = os.Stat(path.Join(dir, "templates_templ.txt"))
79+
if err != nil {
80+
continue
81+
}
82+
break
83+
}
84+
if err != nil {
85+
t.Fatalf("template files were not created: %v", err)
86+
}
87+
88+
cancel()
89+
if err := eg.Wait(); err != nil {
90+
t.Fatalf("generate command failed: %v", err)
91+
}
92+
93+
// Check the templates_templ.txt file was removed.
94+
_, err = os.Stat(path.Join(dir, "templates_templ.txt"))
95+
if err == nil {
96+
t.Fatalf("templates_templ.txt was not removed")
97+
}
98+
})
4599
}
46100

47101
func TestDefaultWatchPattern(t *testing.T) {

0 commit comments

Comments
 (0)