Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templ generate --watch --cmd="go run ." tries to create _templ.txt files in gopath modules #1093

Open
wsor4035 opened this issue Mar 10, 2025 · 11 comments

Comments

@wsor4035
Copy link

wsor4035 commented Mar 10, 2025

Describe the bug
basically what it says in the title, this fails as far as i can tell due to gopath only being read and execute permissions, not write. see golang/go#27161 and golang/go#31481 while i could edit the directory permissions to allow this, that feels suboptimal. not sure why temp needs to cache templ code in gopath modules? (even go workspaces modules would be outside of gopath)

running templ generate and then go run . works fine

To Reproduce
can make one if needed

Expected behavior
code to run and not throw error (which i had to discover via error handler rather than cli error)

the documentation says

watches the current directory for changes and generates Go code if changes are detected.

which obviously is not the case as gopath is outside the project

Screenshots
If applicable, add screenshots or screen captures to help explain your problem.

Logs
If the issue is related to IDE support, run through the LSP troubleshooting section at https://templ.guide/commands-and-tools/ide-support/#troubleshooting-1 and include logs from templ

templ info output
(✓) os [ goos=linux goarch=amd64 ]
(✓) go [ location=/usr/local/go/bin/go version=go version go1.24.0 linux/amd64 ]
(✓) gopls [ location=/home/REDACTED/go/bin/gopls version=golang.org/x/tools/gopls v0.18.0 ]
(✓) templ [ location=/home/REDACTED/go/bin/templ version=v0.3.833 ]

Desktop (please complete the following information):

  • OS: Linux
  • templ CLI version: 0.3.833
  • Go version: go version go1.24.0 linux/amd64
  • gopls version: golang.org/x/tools/gopls v0.18.0

Additional context
N/A

@wsor4035
Copy link
Author

playing with the watch-pattern cli arg, scoping just to the directory in question, was not able to prevent the error. might be doing something wrong however

@joerdav
Copy link
Collaborator

joerdav commented Mar 11, 2025

@wsor4035 upon initial inspection, I think we will need a minimal reproduction if possible. This seems like it's something todo with your setup so that would help us to narrow it down.

@wsor4035
Copy link
Author

@joerdav https://github.com/wsor4035/templwatchsample let me know if you need more than that

@a-h
Copy link
Owner

a-h commented Mar 12, 2025

@wsor4035 - you haven't explained what commands you're running (and where you're running them), any errors you're seeing etc.

You can also run templ generate with -v to provide verbose logs.

I can't really understand why you would run templ generate in the GOPATH, rather than your project's directory. It's hard for me to visualise what the problem is.

@wsor4035
Copy link
Author

wsor4035 commented Mar 12, 2025

@a-h

@wsor4035 - you haven't explained what commands you're running (and where you're running them), any errors you're seeing etc.

the command being run in question is in the issue title. templ generate --watch --cmd="go run ." (it is being run in the project directory root - see logs further down)

the exact error is templ: failed to cache strings: templ: failed to open /home/USERNAME/go/pkg/mod/github.com/!kuto!u!i/[email protected]/components/button_templ.txt: open /home/USERNAME/go/pkg/mod/github.com/!kuto!u!i/[email protected]/components/button_templ.txt: no such file or directory

You can also run templ generate with -v to provide verbose logs.

this error is being pulled from the webpage, since it is caught by the templ error handler

USERNAME@SERVER~/templwatchsample$ templ generate -v --watch --cmd="go run ."
(✓) Starting post-generation handler
(✓) Walking directory [ path=/home/USERNAME/templwatchsample devMode=true ]
(✓) Starting event handler
(✓) Processing file [ file=/home/USERNAME/templwatchsample/page.templ ]
(✓) Processing file [ file=/home/USERNAME/templwatchsample/main.go ]
(✓) File updated [ file=/home/USERNAME/templwatchsample/main.go ]
(✓) Watching files
(✓) Processing file [ file=/home/USERNAME/templwatchsample/page_templ.go ]
(✓) File not updated [ file=/home/USERNAME/templwatchsample/page_templ.go ]
(✓) Waiting for context to be cancelled to stop watching files
(✓) Generated code [ file=/home/USERNAME/templwatchsample/page.templ in=929.167µs ]
(✓) File updated [ file=/home/USERNAME/templwatchsample/page.templ ]
(✓) Executing command [ command=go run . ]
(✓) Processing file [ file=/home/USERNAME/templwatchsample/page_templ.go ]
(✓) File not updated [ file=/home/USERNAME/templwatchsample/page_templ.go ]
(✓) Processing file [ file=/home/USERNAME/templwatchsample/page_templ.txt ]
(✓) File not updated [ file=/home/USERNAME/templwatchsample/page_templ.txt ]
(✓) Processing file [ file=/home/USERNAME/templwatchsample/page_templ.txt ]
(✓) File not updated [ file=/home/USERNAME/templwatchsample/page_templ.txt ]
(✓) First post-generation event received, starting proxy
(✓) No proxy URL specified, not starting proxy
Listening on :3420

I can't really understand why you would run templ generate in the GOPATH, rather than your project's directory. It's hard for me to visualise what the problem is.

im not running the command in gopath, im running it from the project and somehow templ is trying to write files in the gopath module of its own accord which doesnt make much sense, hence the issue.

as stated in the original issue, this doesnt occur if i run templ generate && go run ., but then one has to rerun that command every time and edit is made, which isnt ideal

happy to provide further information/answer any questions as needed

@a-h
Copy link
Owner

a-h commented Mar 12, 2025

Thanks. I think I understand the issue.

It looks like a library that you're importing uses templ, and when it sees that watch mode is enabled, the templ code attempts to write to the location next to it.

I think I'll need to update templ to be able to distinguish which package is being ran in watch mode, and/or move watch files to a tmp directory or user directory instead, which a few people have already suggested for other reasons.

Sorry about that!

@wsor4035
Copy link
Author

wsor4035 commented Mar 12, 2025

i assume that might take some time to fix/improve/etc? (doesnt appear to be a simple fix i could contribute, and seems you might have architectural ideas on it) i assume for the meantime i should just use templ generate && go run . hooked into something like air, etc? or do you have a better idea in mind?

note: go workspaces also allow getting around this, but then you lose all versions of dependencies very quickly

@a-h
Copy link
Owner

a-h commented Mar 15, 2025

I think this PR should fix it: #1099

Care to give it a try out?

@wsor4035
Copy link
Author

testing with go install github.com/a-h/templ/cmd/templ@1613ed7 (nothing else changed)
i get the following error
templ: failed to cache strings: templ: failed to open /home/USER/templwatchsample/page_templ.txt: open /home/USER/templwatchsample/page_templ.txt: no such file or directory

@a-h
Copy link
Owner

a-h commented Mar 16, 2025

This is a runtime change, you'd have to install the latest version of the templ library as well as use the latest CLI.

You should have seen a warning about a version mismatch when you ran templ generate.

@wsor4035
Copy link
Author

so ran go install github.com/a-h/templ/cmd/templ@1613ed7
templ version = v0.3.842
go get github.com/a-h/templ@1613ed7 to upgrade the library
get warning (!) templ version check: generator v0.3.842 is newer than templ version v0.3.834-0.20250315144248-1613ed75616b found in go.mod file, consider running go get -u github.com/a-h/templ to upgrade
i assume im missing something here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants