Skip to content

fix: dont delete dev-mode text files during a one-off generate#1434

Open
Napam wants to merge 1 commit into
a-h:mainfrom
Napam:fix-templ-generate
Open

fix: dont delete dev-mode text files during a one-off generate#1434
Napam wants to merge 1 commit into
a-h:mainfrom
Napam:fix-templ-generate

Conversation

@Napam

@Napam Napam commented Jul 24, 2026

Copy link
Copy Markdown

Problem

Running a one-off templ generate while a templ generate --watch dev server is up breaks every page the running dev server tries to render.

--watch writes each template's literals to a temporary _templ.txt file; a server run with TEMPL_DEV_MODE=true renders from those files. But generate called deleteWatchModeTextFiles() on every run, including non-watch ones which don't own these files. So a one-off generate deletes the .txt files of a separate running watch session, and every render then fails with a missing-file error. The watch session doesn't recover either. It skips rewriting files whose literals are unchanged.

Repro: start a watch-mode dev server (templ generate --watch plus an app run with TEMPL_DEV_MODE=true that renders from the .txt files), then run a plain templ generate (e.g. a CI/check step that verifies generated code is up to date). The one-off generate wipes the server's .txt files and every page then fails to render until the dev session is restarted.

Fix

  • cmd.go: only run deleteWatchModeTextFiles() in watch mode a one-off generate must not delete files it doesn't own.
  • eventhandler.go: rewrite a _templ.txt file if it's missing from disk, even when literals are unchanged, so a watch session self-heals.

Tests

Two regression tests in main_test.go; a non-watch generate leaves existing .txt files intact, and a deleted .txt file is recreated on the next regeneration.

My use case

I run a dev server while also running static checks quite regularly (especially when using AI agents). Each check runs a plain templ generate, which was wiping the dev server's _templ.txt files and leaving it unable to render any page until I restarted it.

AI disclaimer

I used Claude to help write the tests and suggest the fix.

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

Successfully merging this pull request may close these issues.

1 participant