Sort ProvidedTypes iteration for deterministic source output#413
Merged
eiriktsarpalis merged 2 commits intoeiriktsarpalis:mainfrom Apr 15, 2026
Merged
Conversation
The source generator iterates provider.ProvidedTypes.Values in two places: SourceFormatter.AddAllSourceFiles and SourceFormatter.FormatGetShapeProviderMethod. Since ProvidedTypes is backed by Dictionary<TKey, TValue>, iteration order is non-deterministic, causing the generated source to vary between compilations even when inputs are identical. This breaks incremental build in projects that reference assemblies using PolyType, because the reference assembly content changes on every build, forcing downstream projects to recompile unnecessarily. Fix: sort by SourceIdentifier (a unique, stable, type-name-derived key) before iterating.
…generator Sort iterations over ImmutableEquatableSet and ImmutableEquatableDictionary collections that feed into generated source output: - ShapeableImplementations (ImmutableEquatableSet<TypeId>): sorted by FullyQualifiedName in three iteration sites that emit interface declarations, method implementations, and type checks. - EnumShapeModel.Members (ImmutableEquatableDictionary<string, string>): sorted by key when emitting enum member dictionary initializers. - AssociatedTypes (ImmutableEquatableSet<AssociatedTypeId>): sorted by ClosedTypeReflectionName when emitting associated type switch cases. Updated snapshot baselines to reflect the new deterministic ordering. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Author
|
Thanks for making this more comprehensive. :) |
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.
The source generator iterates
provider.ProvidedTypes.Valuesin two places:SourceFormatter.AddAllSourceFiles— emits per-type source filesSourceFormatter.FormatGetShapeProviderMethod— emitsswitchcases inGetTypeShapeSince
ProvidedTypesis anImmutableEquatableDictionarybacked byDictionary<TKey, TValue>, iteration order is non-deterministic. This causes the generated source to vary between compilations even when inputs are identical.Impact
This breaks incremental build for downstream projects. When a project referencing a PolyType-using library is built:
<>Odisplay class)In our case, a single implementation-only change in one project caused
cscto run for a downstream project that should have been skipped entirely.Fix
Sort by
SourceIdentifier(a unique, stable, type-name-derived key) before iterating in both locations. This is a two-line change.