Skip to content

Commit c7c32aa

Browse files
authoredSep 1, 2024··
feat: support running templ generate in a symlinked directory, fixes #858 (#901)
1 parent e29dcde commit c7c32aa

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package symlink
2+
3+
import (
4+
"context"
5+
"io"
6+
"log/slog"
7+
"os"
8+
"path"
9+
"testing"
10+
11+
"github.com/a-h/templ/cmd/templ/generatecmd"
12+
"github.com/a-h/templ/cmd/templ/testproject"
13+
)
14+
15+
func TestSymlink(t *testing.T) {
16+
log := slog.New(slog.NewJSONHandler(io.Discard, nil))
17+
t.Run("can generate if root is symlink", func(t *testing.T) {
18+
// templ generate -f templates.templ
19+
dir, err := testproject.Create("github.com/a-h/templ/cmd/templ/testproject")
20+
if err != nil {
21+
t.Fatalf("failed to create test project: %v", err)
22+
}
23+
defer os.RemoveAll(dir)
24+
25+
symlinkPath := dir + "-symlink"
26+
err = os.Symlink(dir, symlinkPath)
27+
if err != nil {
28+
t.Fatalf("failed to create dir symlink: %v", err)
29+
}
30+
defer os.Remove(symlinkPath)
31+
32+
// Delete the templates_templ.go file to ensure it is generated.
33+
err = os.Remove(path.Join(symlinkPath, "templates_templ.go"))
34+
if err != nil {
35+
t.Fatalf("failed to remove templates_templ.go: %v", err)
36+
}
37+
38+
// Run the generate command.
39+
err = generatecmd.Run(context.Background(), log, generatecmd.Arguments{
40+
Path: symlinkPath,
41+
})
42+
if err != nil {
43+
t.Fatalf("failed to run generate command: %v", err)
44+
}
45+
46+
// Check the templates_templ.go file was created.
47+
_, err = os.Stat(path.Join(symlinkPath, "templates_templ.go"))
48+
if err != nil {
49+
t.Fatalf("templates_templ.go was not created: %v", err)
50+
}
51+
})
52+
}

‎cmd/templ/generatecmd/watcher/watch.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package watcher
22

33
import (
44
"context"
5+
"io/fs"
56
"os"
67
"path"
78
"path/filepath"
@@ -36,18 +37,24 @@ func Recursive(
3637
// WalkFiles walks the file tree rooted at path, sending a Create event for each
3738
// file it encounters.
3839
func WalkFiles(ctx context.Context, path string, out chan fsnotify.Event) (err error) {
39-
return filepath.WalkDir(path, func(path string, info os.DirEntry, err error) error {
40+
rootPath := path
41+
fileSystem := os.DirFS(rootPath)
42+
return fs.WalkDir(fileSystem, ".", func(path string, info os.DirEntry, err error) error {
4043
if err != nil {
4144
return nil
4245
}
43-
if info.IsDir() && shouldSkipDir(path) {
46+
absPath, err := filepath.Abs(filepath.Join(rootPath, path))
47+
if err != nil {
48+
return nil
49+
}
50+
if info.IsDir() && shouldSkipDir(absPath) {
4451
return filepath.SkipDir
4552
}
46-
if !shouldIncludeFile(path) {
53+
if !shouldIncludeFile(absPath) {
4754
return nil
4855
}
4956
out <- fsnotify.Event{
50-
Name: path,
57+
Name: absPath,
5158
Op: fsnotify.Create,
5259
}
5360
return nil

0 commit comments

Comments
 (0)
Please sign in to comment.