templ icon indicating copy to clipboard operation
templ copied to clipboard

feat: make templ generate ignore the vendor directory by default

Open BillBuilt opened this issue 4 months ago • 9 comments

We use vendoring. It would great if templ generate could have an exclude argument so we could ignore that directory.

BillBuilt avatar Aug 19 '25 18:08 BillBuilt

Good point. I'd argue that should actually be the default.

joerdav avatar Aug 19 '25 19:08 joerdav

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
}

BillBuilt avatar Aug 19 '25 19:08 BillBuilt

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.

a-h avatar Aug 19 '25 20:08 a-h

I assume it was supposed to do this: https://github.com/a-h/templ/blob/abb427c0fb8d5bfdc69586ebd7a7d88dae60e637/internal/skipdir/skipdir.go#L13

a-h avatar Aug 19 '25 20:08 a-h

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?

BillBuilt avatar Aug 20 '25 18:08 BillBuilt

Actually, goaster works fine when launching the server directly (go run .) so I don't think that's the issue.

BillBuilt avatar Aug 20 '25 18:08 BillBuilt

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
}

BillBuilt avatar Aug 21 '25 13:08 BillBuilt

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.

a-h avatar Aug 31 '25 15:08 a-h

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?

axadrn avatar Nov 17 '25 06:11 axadrn