Skip to content

fix(render): gofmt generated source before returning - #17

Merged
jmfrees merged 1 commit into
mainfrom
jmfrees/format-generated-source
Jul 7, 2026
Merged

jmfrees merged 1 commit into
mainfrom
jmfrees/format-generated-source

Conversation

@jmfrees

@jmfrees jmfrees commented Jul 7, 2026

Copy link
Copy Markdown
Owner

What

Run the enumr template output through go/format (gofmt) in generateEnumSource before returning it.

Why

The generator previously returned raw text/template output, which is not guaranteed to be gofmt-clean. In practice it produced:

  • Extra spaces inside struct literals: SortByBehavior{ name: "..." } instead of SortByBehavior{name: "..."}
  • Unaligned = in generated var (...) blocks
  • A trailing blank line at end of file

Downstream repos (e.g. shipt/search) that enforce gofmt -l in CI then fail on the committed generated enum files. Formatting at generation time makes output canonical and robust to template whitespace, so consumers never have to hand-fix or re-gofmt generated code.

Changes

  • pkg/enumr/render.go: run format.Source() on the buffer, wrapping errors with %w.
  • pkg/enumr/render_test.go: update the var-block snippet assertion to the gofmt-canonical form (no inner-brace spaces, aligned =).
  • pkg/enumr/testdata/myenum_enum.go.golden: regenerated (drops trailing blank line).

Testing

  • go test -race ./... — pass
  • gofmt -l . — clean
  • go generate ./... produces no diff (deterministic, matches CI "Verify Clean Generation")

The generator emitted raw template output, which is not guaranteed to be
gofmt-clean (extra spaces inside struct literals, unaligned `=` in var
blocks, trailing blank line). Downstream repos that run `gofmt -l` in CI
then fail on the generated enum files.

Run the template output through go/format so callers always receive
canonically formatted code, independent of template whitespace.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jmfrees
jmfrees merged commit 3856d6a into main Jul 7, 2026
1 check passed
@jmfrees
jmfrees deleted the jmfrees/format-generated-source branch July 7, 2026 16:20
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