Skip to content

respect output file path on gofmt failure#2356

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2340
Apr 30, 2026
Merged

respect output file path on gofmt failure#2356
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2340

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

When imports.Process rejected the generated source, Generate() discarded the code and stuffed a line-numbered dump of it into the returned error. The CLI then printed that error to stderr and never wrote the requested -o (or config output:) file, so users saw a huge stderr spew with no output file produced.

Return the raw pre-format code from Generate() alongside the error, and have the CLI write it to the configured destination before exiting. The error message itself is now a single line referencing the failing source line; users can open the written file to inspect the broken code directly. addLineNumbers is dropped — it was only used by the removed in-error code dump.

Closes: #2340

When `imports.Process` rejected the generated source, `Generate()`
discarded the code and stuffed a line-numbered dump of it into the
returned error. The CLI then printed that error to stderr and never
wrote the requested `-o` (or config `output:`) file, so users saw a
huge stderr spew with no output file produced.

Return the raw pre-format code from `Generate()` alongside the error,
and have the CLI write it to the configured destination before
exiting. The error message itself is now a single line referencing
the failing source line; users can open the written file to inspect
the broken code directly. `addLineNumbers` is dropped — it was only
used by the removed in-error code dump.

Closes: oapi-codegen#2340
@mromaszewicz mromaszewicz requested a review from a team as a code owner April 30, 2026 18:51
@mromaszewicz mromaszewicz added the bug Something isn't working label Apr 30, 2026
@mromaszewicz mromaszewicz added this to the v2.7.0 milestone Apr 30, 2026
@greptile-apps

greptile-apps Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes the UX regression from #2340 where a formatter error caused Generate() to return ("", err) and the CLI to dump line-numbered code onto stderr with no output file written. Generate() now returns the raw pre-format source alongside the error, the CLI writes it to the configured destination first, and the error is reduced to a single-line message referencing the failing line number. The helper addLineNumbers is removed as it is no longer needed.

Confidence Score: 4/5

Safe to merge; the fix is narrow and well-reasoned, with only minor documentation and test-coverage gaps.

Both findings are P2-only — missing regression test and undocumented contract change on Generate(). The logic change is correct and the CLI behaviour is clearly improved. P2s cap the score at 4/5.

pkg/codegen/codegen.go — the changed Generate() return-value contract should be documented in godoc, and a regression test for the error path would be valuable.

Important Files Changed

Filename Overview
pkg/codegen/codegen.go Generate() now returns raw unformatted code on imports.Process failure instead of ""; addLineNumbers helper removed; error message simplified to single-line with optional line number
cmd/oapi-codegen/oapi-codegen.go CLI now writes/prints any non-empty code returned by Generate() before checking genErr, ensuring broken output reaches the configured destination for inspection

Reviews (1): Last reviewed commit: "respect output file path on gofmt failur..." | Re-trigger Greptile

@mromaszewicz mromaszewicz merged commit 7517e09 into oapi-codegen:main Apr 30, 2026
19 checks passed
@mromaszewicz mromaszewicz deleted the fix/issue-2340 branch April 30, 2026 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output file option (-o) has no effect: generated code is always dumped to stdout

1 participant