Fix config binding generator CS0103 for required/init property with matching ctor param#130483
Draft
svick wants to merge 2 commits into
Draft
Fix config binding generator CS0103 for required/init property with matching ctor param#130483svick wants to merge 2 commits into
svick wants to merge 2 commits into
Conversation
…atching ctor param The generated Initialize method re-assigns required/init properties that also flow through a matching constructor parameter, so custom init/set accessors run after the constructor. It used the property name as the value expression (`Name = Name`), but the bound value lives in the local named after the constructor parameter. When the parameter name differed from the property (e.g. only in casing, `name` vs `Name`), no such local existed and the generated code failed to compile with CS0103. Use the matching constructor parameter's local name as the assigned value when present, falling back to the property name otherwise. Output is unchanged when the parameter and property names match exactly (e.g. positional records), so no baselines change. Fixes dotnet#128390 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e830d4aa-6a35-4105-9979-86d92e5255d2
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Configuration Binding source-generator compile failure when an init/required property is bound via a matching constructor parameter whose name differs from the property name (e.g., name vs Name). The change updates emitted object-initializer assignments to use the constructor-parameter local when applicable, and adds regression coverage in both generator compilation tests and binder parity tests.
Changes:
- Update generator emission for
Initializemethods to assign init-only properties from the matching constructor-parameter local (MatchingCtorParam.Name) when present. - Refactor generator test helper to assert successful compilation without allocating/returning an assembly image.
- Add regression tests covering differently-cased ctor parameter/property names for both source-generation compilation and runtime/sourcegen binder behavior parity.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.Helpers.cs | Replaces CreateAssemblyImage with a compilation assertion helper used by generator tests. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.cs | Adds generator compilation regression theory for required/init property + matching ctor parameter (case-different and exact-match). |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs | Adds a new test type with differently-cased ctor parameter names to validate parameter/property binding behavior. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs | Adds a parity test ensuring properties are set after the constructor even when ctor parameter casing differs. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs | Fixes emitted object-initializer assignments to use the ctor-parameter local when a property matches a constructor parameter. |
This was referenced Jul 10, 2026
Open
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
The Configuration Binding source generator produced non-compiling code (
CS0103) for arequired/initproperty that is bound through a matching constructor parameter whose name differs from the property (for example only in casing: parameternamevs. propertyName).Fixes #128390.
Root cause
In the generated
Initializemethod, arequired/initproperty that also has a matching constructor parameter is deliberately re-assigned in the object initializer so any custominit/setaccessor logic runs after the constructor (the behavior covered byCanBindOnParametersAndProperties_PropertiesAreSetAfterTheConstructor).The emitter used the property name as the value expression:
but the bound value lives in the local named after the constructor parameter (
name). When the two names matched exactly (e.g. positional records, or the existingClassWithMatchingParametersAndProperties) the code happened to compile; when they differed it did not.Note
This pull request was authored with the assistance of GitHub Copilot.