-
-
Notifications
You must be signed in to change notification settings - Fork 294
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
cmd: templ generate with watch can generate corrupted Go files, because of race condition with fsnotify WRITE events #525
Comments
Thanks @stefanohrmann for the thorough bug report, and your time spent investigating. I've marked this as NeedsInvestigation because I think it could be worth looking into the viability of a fix which doesn't involve removing any commands. Such as ignoring events where the file is empty. |
@joerdav You are right and I think it is totally possible to fix this issue in the current implementation if you apply the dedup stuff from fsnotify here: templ/cmd/templ/generatecmd/watcher/watch.go Lines 73 to 99 in 753ba39
And with my suggestion i didn't want to remove any subcommands. I wanted to add a dedicated one for watching. But it is ok if this doesn't fit in your structure. Edit note: Changed the link to right place for the fix. |
Ah, not to dismiss your idea, just wanted to explore all options first, and I think minimal change to the outside API would be preferable. |
Great find, and nice write up. From reading the I think the logic to handle either a true deduplication (i.e. within a window of 100ms, if there is more than one operation of the same time, discard the earlier ones, and emit the latest), or debounce would go here: templ/cmd/templ/generatecmd/watcher/watch.go Lines 73 to 99 in 753ba39
|
@a-h You are right about the dedup and debounce stuff. I didn't want to change the terminology of fsnotify and I marked the wrong place for the fix. I will correct it. |
I've created a debounce implementation, for now I've put the window at 100ms I'm hoping that this will be enough. |
I used the following command:
to watch my template directory and to generate the Go files on changes. In the first place I used the command with the
proxy
and thecmd
option to automatically update the displayed website, but the issue also occurs if I only use the thewatch
option. I got the following trace:I omitted the entries for the first several tries to reproduce the issue. For full disclosure I replaced the for following line in the source:
templ/cmd/templ/generatecmd/cmd.go
Line 171 in 753ba39
with the following to investigate the issue (you see the result in the trace above)
After the last change in the trace above the
layout_templ.go
file has the following content and isn't compilable:The issue is that the generate acts on the first WRITE notification for the
layout.tmpl
file, but in this case the file is empty and this produces the corrupted Go file. I don't know if there should be a warning.As far as I know this is a known issue with
fsnotify
and they recommend to dedup the WRITE notifications.I try to create a test for the issue:
generate_racecondition_test.zip
This is not 100% reliable, because this depends on the file system.
I suggest that whole watch functionality is moved to an own subcommand and that the generate subcommand only does the generation of the Go files. That would imho result in a straight forward (in the current implementation the collection of the template files is done by using a channel and fsnotify events even if no watch is selected) and faster implementation of the generate subcommand without any concurrency. A fast generate subcommand could be better for 3rd party tools like Air. But maybe I'm not fully understand all the implications of such a change.
If such a change would align with the vision of this project, then I would be happy to provide PRs for this in the following order:
The text was updated successfully, but these errors were encountered: