Skip to content

Commit

Permalink
fix: delete watchmode files when shutdown is sent (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerdav authored Jan 13, 2025
1 parent 8b05ded commit 24be99e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
20 changes: 19 additions & 1 deletion cmd/templ/generatecmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -185,9 +186,26 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
)
postGenerationEventsWG.Wait()
cmd.Log.Debug(
"All post-generation events processed",
"All post-generation events processed, deleting watch mode text files",
slog.Int64("errorCount", errorCount.Load()),
)

fileEvents := make(chan fsnotify.Event)
go func() {
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.WatchPattern, fileEvents); 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
}
close(fileEvents)
}()
for event := range fileEvents {
if strings.HasSuffix(event.Name, "_templ.txt") {
if err = os.Remove(event.Name); err != nil {
cmd.Log.Warn("Failed to remove watch mode text file", slog.Any("error", err))
}
}
}
}()

// Start process to handle events.
Expand Down
54 changes: 54 additions & 0 deletions cmd/templ/generatecmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"path"
"regexp"
"testing"
"time"

"github.com/a-h/templ/cmd/templ/testproject"
"golang.org/x/sync/errgroup"
)

func TestGenerate(t *testing.T) {
Expand Down Expand Up @@ -42,6 +44,58 @@ func TestGenerate(t *testing.T) {
t.Fatalf("templates_templ.go was not created: %v", err)
}
})
t.Run("can generate a file in watch mode", func(t *testing.T) {
// templ generate -f templates.templ
dir, err := testproject.Create("github.com/a-h/templ/cmd/templ/testproject")
if err != nil {
t.Fatalf("failed to create test project: %v", err)
}
defer os.RemoveAll(dir)

// Delete the templates_templ.go file to ensure it is generated.
err = os.Remove(path.Join(dir, "templates_templ.go"))
if err != nil {
t.Fatalf("failed to remove templates_templ.go: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())

var eg errgroup.Group
eg.Go(func() error {
// Run the generate command.
return Run(ctx, log, Arguments{
Path: dir,
Watch: true,
})
})

// Check the templates_templ.go file was created, with backoff.
for i := 0; i < 5; i++ {
time.Sleep(time.Second * time.Duration(i))
_, err = os.Stat(path.Join(dir, "templates_templ.go"))
if err != nil {
continue
}
_, err = os.Stat(path.Join(dir, "templates_templ.txt"))
if err != nil {
continue
}
break
}
if err != nil {
t.Fatalf("template files were not created: %v", err)
}

cancel()
if err := eg.Wait(); err != nil {
t.Fatalf("generate command failed: %v", err)
}

// Check the templates_templ.txt file was removed.
_, err = os.Stat(path.Join(dir, "templates_templ.txt"))
if err == nil {
t.Fatalf("templates_templ.txt was not removed")
}
})
}

func TestDefaultWatchPattern(t *testing.T) {
Expand Down

0 comments on commit 24be99e

Please sign in to comment.