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

Request: templ fmt should not write the file unless there are changes #128

Closed
jonerer opened this issue Aug 21, 2023 · 2 comments
Closed

Comments

@jonerer
Copy link
Contributor

jonerer commented Aug 21, 2023

Thanks for this awesome project -- it feels like a perfect fit for htmx+alpine etc. Love the type safety.

I would like to be able to run templ fmt automatically on changes. But right now running templ fmt will trigger a restart of air since it rewrites files even though fmt doesn't create any changes. It would be awesome if templ fmt doesn't write the file when there's no changes.

I believe a change somewhere around here: err = atomic.WriteFile(fileName, w) could fix it.

Ideally I'd like to put something like
cmd = "templ fmt . && templ generate && go build -o ./tmp/main ."
my .air.toml. But right now that causes an infinite loop.

Thanks!

@joerdav
Copy link
Collaborator

joerdav commented Aug 22, 2023

This could potentially leverage the outcome of #123 if the hash option is developed. To avoid having to reload the file to do a diff check before writing the file.

Although, we do already load the whole file into memory anyway:

fc, err := os.ReadFile(fileName)

@a-h
Copy link
Owner

a-h commented Aug 28, 2023

Thanks for the report @jonerer!

You're right @joerdav - the fmt command is already holding the file in RAM so it's not much work to do an additional check, and in a lot of cases, it will skip a filesystem write, which should result in a net save of performance overall.

I've made a quick tweak to that section of code.

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