Stabilize codegen ordering for byte-identical output across runs#196
Merged
Conversation
The MethodFrameArranger gathered InjectedFields and Setters by iterating DependencyGatherer.Variables.Keys(), which is backed by an ImHashMap. ImHashMap iterates in hash-code order, and string/Type hash codes are randomized per process - so the same logical generated type produced constructor parameters and field declarations in different orders across runs, breaking byte-level file comparisons. Sort the iterations by (VariableType.FullName, Usage/PropName) before filling AllInjectedFields and Setters so the generated source is stable. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This was referenced May 10, 2026
Closed
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.
Summary
MethodFrameArranger.findInjectedFieldsandfindSetterspopulatedAllInjectedFields/Settersby iteratingDependencyGatherer.Variables.Keys(), which is backed by anImHashMap. ImHashMap iterates in hash-code order, and string/Typehash codes are randomized per process — so the same logical generated type produced constructor parameters and field declarations in different orders across runs, breaking byte-level file comparisons.(VariableType.FullName, Usage/PropName)(ordinal) before filling, giving a stable, process-independent order. Base constructor arguments are unaffected (they are populated inInheritsFromviaGetParameters()declaration order, then deduped viaFill).CodegenTests/CodeGeneration_ordering_determinism.cswith four tests:AllInjectedFieldsare sorted alphabetically,The first two failed on
main(constructor was emitted asbravo, alpha, zebra, delta, charlie, mango) and pass with this change.Test plan
dotnet test src/CodegenTests/CodegenTests.csproj— 309 passeddotnet test src/CoreTests/CoreTests.csproj— 389 passed (×3 TFMs)dotnet test src/CommandLineTests/CommandLineTests.csproj— 280 passed (×3 TFMs)dotnet test src/EventTests/EventTests.csproj— 222 passed (×3 TFMs)CodeGeneration_ordering_determinismtests fail onmainand pass with this change🤖 Generated with Claude Code