feat(mdatagen): add gofmt validation to prevent template formatting regressions#14721
feat(mdatagen): add gofmt validation to prevent template formatting regressions#14721atilsensalduz wants to merge 1 commit intoopen-telemetry:mainfrom
Conversation
Your LLM is wrong. These are not "bugs". I don't think this is the right approach. Doing this kind of check in pure-go overly complicates things. |
|
Hi @dmathieu, thank you for the review. If the only viable path is detecting template formatting issues in CI, we need to remove format.Source() from the write path in generateFile (keeping it only for syntax validation), then add a CI step that runs If this direction sounds good, I'll proceed with it. |
|
How about adding a |
07ab274 to
d2a79df
Compare
ec14c39 to
d0861fe
Compare
Signed-off-by: atilsensalduz <atil.sensalduz@gmail.com>
d0861fe to
f4dfb33
Compare
|
Hey @dmathieu , thanks for the recommendation. That’s definitely a better approach, simple and clean. I’ve updated the PR. Right now |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| noformat := os.Getenv("MDATAGEN_NOFORMAT") == "1" |
There was a problem hiding this comment.
We don't use env vars to set flags anywhere within mdatagen. I don't think starting this pattern is a good fit for this PR.
| noformat := os.Getenv("MDATAGEN_NOFORMAT") == "1" | |
| var noFormat bool |
|
I would open a PR fixing all the issues independently from this one (link to here in that new PR), so both can be reviewed independently. |
| $(MAKE) fmt | ||
|
|
||
| .PHONY: check-generate-fmt | ||
| check-generate-fmt: |
There was a problem hiding this comment.
We will need to run this on CI.
###Summary
Adds a structural gofmt validation step to the
mdatagencode generator that fails at generation time when a template produces output thatformat.Source()would reformat, rather than silently accepting the fix from a downstreamgoimportsrun. This catches real issues (wrong tab depth, missing spaces, incorrect indentation) while ignoring cosmetic differences such as blank lines, import grouping separators, and struct-field alignment. As a prerequisite, 13 template files that had pre-existing formatting issues are fixed so the new check passes.###Why the existing CI did not catch template formatting issues
The CI go:generate step runs make gogenerate, which expands to:
Because
goimportsruns aftergo generate, any formatting mistake in a template (wrong tab depth, missing blank line between import groups, extra space, etc.) is silently corrected before the subsequentgit diff --exit-codecheck. The diff that CI sees is always empty, so the template issue is never surfaced.The fix makes the generator self-policing: generateFile() now compares the raw template output against what format.Source() (gofmt) would produce. If they differ in any meaningful way (tab depth, missing spaces, wrong indentation), the generation step fails immediately with an error pointing to the offending file, before goimports has a chance to mask the problem.
Fixes: #14698