Repository navigation
fix: make the order of external imports deterministic - #2582
Merged
mromaszewicz merged 1 commit intoSep 26, 2026
Merged
Conversation
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]>
Contributor
|
This was referenced Sep 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong.
importMap.GoImportsranges over a map, so theexternalRefN "..."lines thatimport-mappingproduces, and the lines thatx-go-type-importproduces, 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 withoutput-options.skip-fmt: truethe same binary wrote a different file on successive runs. It showed up oninternal/test/extensions/x_go_type,internal/test/references/external/path_item_refs/fooserviceandinternal/test/references/multipackage.What changed.
GoImportssorts 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.
TestImportMapGoImportsSorted, builds animportMapwith ten entries and checksGoImportsreturns them sorted, twenty times over. It fails on every iteration without the fix.make generatechanges no committed file, so formatted output is byte-identical before and after.skip-fmtforced 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 testandmake lintpass.🤖 Generated with Claude Code