Skip to content

fix: make the order of external imports deterministic - #2582

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/deterministic-import-order
Sep 26, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/deterministic-import-order

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

What was wrong. importMap.GoImports ranges over a map, so the externalRefN "..." lines that import-mapping produces, and the lines that x-go-type-import produces, came out of the generator in a different order from one run to the next. gofmt sorts the import block, which hides this in the common case, but with output-options.skip-fmt: true the same binary wrote a different file on successive runs. It showed up on internal/test/extensions/x_go_type, internal/test/references/external/path_item_refs/fooservice and internal/test/references/multipackage.

What changed. GoImports sorts the lines it returns. That is the whole change; the formatted output is unaffected because gofmt was already sorting the block.

How it's tested.

  • A new unit test, TestImportMapGoImportsSorted, builds an importMap with ten entries and checks GoImports returns them sorted, twenty times over. It fails on every iteration without the fix.
  • make generate changes no committed file, so formatted output is byte-identical before and after.
  • With skip-fmt forced on the three fixtures above, the pre-fix binary produced 2–3 distinct files over 10 runs each; the fixed binary produces one. Running gofmt over a pre-fix and a post-fix skip-fmt output gives identical files, so the only thing that moved is the order of the import lines.
  • make test and make lint pass.

🤖 Generated with Claude Code

importMap.GoImports ranged over a map, so the import-mapping and
x-go-type-import lines of the generated import block came out in a
different order from one run to the next. gofmt sorts the import block,
which hid it, but with output-options.skip-fmt the same binary produced
different files across runs, seen on internal/test/extensions/x_go_type,
internal/test/references/external/path_item_refs/fooservice and
internal/test/references/multipackage.

GoImports now sorts the lines it returns. Formatted output is unchanged:
`make generate` modifies no committed file, and gofmt of the skip-fmt
output before and after the fix is identical. With skip-fmt, the three
fixtures above generate the same file across 10 runs each, where before
they produced 2-3 distinct files. A unit test pins the sorted order; it
fails 20 out of 20 iterations without the fix.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 26, 2026 05:06
@mromaszewicz mromaszewicz added the bug Something isn't working label Sep 26, 2026
@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Sorts import statements in code generation output.

The PR appears safe to merge.

Summary

The PR sorts external import statements before rendering them, making generated output deterministic when formatting is skipped.

  • Adds a unit test covering repeated calls with multiple imports.

Reviews (1) · Last reviewed commit: "fix: make the order of external imports ..."

@mromaszewicz
mromaszewicz merged commit 77ce86c into oapi-codegen:main Sep 26, 2026
16 checks passed
@mromaszewicz
mromaszewicz deleted the fix/deterministic-import-order branch September 26, 2026 05:10
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.

1 participant