feat: make templ generate ignore the vendor directory by default
We use vendoring. It would great if templ generate could have an exclude argument so we could ignore that directory.
Good point. I'd argue that should actually be the default.
It's causing errors for me and I can't use the --watch flag because of it.
{
"Status": "internal server error",
"Message": "templ: failed to get watched strings for \"/Users/me/projects/my/project/vendor/github.com/indaco/goaster/components/css/colors_templ.go\": templ: failed to open /var/folders/h3/l2zycd3j7qs7kxlxc5xq8by80000gn/T/templ_f6df7a6f2b9e515410cce4c4e3b12602a1c45ef6808a88c3d0b8bdfa5ad1aedb.txt: open /var/folders/h3/l2zycd3j7qs7kxlxc5xq8by80000gn/T/templ_f6df7a6f2b9e515410cce4c4e3b12602a1c45ef6808a88c3d0b8bdfa5ad1aedb.txt: no such file or directory",
"Code": 500
}
Oh yeah, definitely an oversight.
I saw that there's a new feature coming in go.mod to ignore various vendor files, because Go ends up traversing through node_modules and friends too when it's looking to build files.
I assume it was supposed to do this: https://github.com/a-h/templ/blob/abb427c0fb8d5bfdc69586ebd7a7d88dae60e637/internal/skipdir/skipdir.go#L13
Well looky there.....
That error message templ: failed to get watched strings for comes from runtime.WriteString()
Is is possible that templ is still trying to access to the vendor dir? goaster is a toast package that also uses templ. So it's calling runtime.WriteString() too. In which case GetDevModeTextFileName() will be trying to traverse/access the vendor dir and I would imagine that Go is not going to allow that?
Actually, goaster works fine when launching the server directly (go run .) so I don't think that's the issue.
I cloned the repo and removed the name == "vendor" and added logging. I expected to see /vendor get scanned but it didn't. In fact it wasn't even considered. I wonder if the problem is due to the fact that the external templ is NOT being scanned. Seems the file watcher may have some issues regarding external templ components.
func ShouldSkip(path string) (skip bool) {
log.Printf("checking %s", path)
if path == "." {
log.Print("path is .")
return false
}
_, name := filepath.Split(path)
if name == "node_modules" {
log.Print("path is node_modules")
return true
}
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
log.Print("name starts with '.' or '_'")
return true
}
log.Print("do not skip")
return false
}
I think the issue here is that the templ runtime has different behaviour when the TEMPL_DEV_MODE environment variable is set, and in the case of using the goaster component, there aren't any watched strings, because it isn't being watched, hence the error.
So, I think the fix would be that the watch mode behaviour should identify when it's attempting to write a string for a vendored or 3rd party module (i.e. not in the same go.mod as the one being built), and not try to load watched strings for those.
Hey,
I'm hitting the same issue while trying to make templUI importable as a library alongside the CLI mode.
When importing components from GOPATH:
templ: failed to get watched strings for
"/Users/.../go/pkg/mod/github.com/templui/templui@v0.../components/button/button_templ.go":
templ: failed to open /var/folders/.../T/templ_xxx.txt: no such file or directory
Is there a solution for this yet?