Repository navigation
Configuration Binder SG: ignore unresolvable metadata types to prevent ambiguous/missing-type emissions - #130118
Conversation
Co-authored-by: rosebyte <[email protected]>
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR improves robustness of the Microsoft.Extensions.Configuration.Binder source generator by filtering out members whose types resolve to Roslyn error symbols (e.g., missing/ambiguous metadata types), preventing the generator from emitting uncompilable references. It also adds a regression test that simulates a missing transitive dependency via in-memory metadata references.
Changes:
- Treat
TypeKind.Error(including within arrays/generics) as unsupported during parsing, and skip emitting binding for those members. - Add test infrastructure support for supplying extra
MetadataReferences to the generator test project. - Add a regression test that omits a transitive reference to ensure unresolved member types are excluded while valid members still bind.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.Helpers.cs | Extends test helper to pass optional MetadataReferences through to the test driver. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.cs | Adds regression test covering unresolved metadata member types. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/ConfigBindingGenTestDriver.cs | Adds support for injecting extra Roslyn metadata references into the test compilation. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingGenerator.Parser.cs | Skips members containing Roslyn error types and emits SYSLIB1101 warnings for skipped members to avoid uncompilable output. |
|
Nice work on this, the fix looks solid. Two small edge cases I noticed while going through it, both pre-existing rather than anything this PR introduces, so it would be great to cover them if it is easy: 1. 2. Nested if (type.ContainingType is not null && ContainsErrorType(type.ContainingType))
{
return true;
}Both are pretty exotic, so no worries either way. Thanks! |
…t ambiguous/missing-type emissions (dotnet#130118) ## Summary When the Configuration Binder source generator processes a type whose members reference types from a **transitive assembly that isn't directly referenced** by the consumer (or that resolve ambiguously), those member types degrade to Roslyn *error symbols*. The generator previously emitted binding code that named these types, producing **uncompilable output** — missing-type (`CS0012`/`CS0234`) and namespace-ambiguity errors. The reflection-based binder doesn't hit this (the assemblies are present at runtime), so the source generator was strictly worse: it broke the build. This is the failure reported in dotnet#109759 (`Microsoft.IdentityModel.S2S.DstsExtensions` with trimming, involving `CredentialDescription` / `HttpRequestMessage`). Fixes dotnet#109759. ## Changes ### 1. Robust unsupported-type detection `IsUnsupportedType` now treats a member type as unsupported when it — **or a nested array element or generic type argument** — is an error symbol, via a new recursive `ContainsErrorType` helper. This covers: - direct error-typed members — e.g. `HttpRequestMessage` (unresolved reference type), - `Nullable<T>` over an unresolved value type — e.g. `ValueTypeMessage?`, - error types nested inside a **resolvable** generic — e.g. `Wrapper<Missing>`, `System.Tuple<int, Missing>`, `KeyValuePair<int, Missing>?`, `Missing[]`. ### 2. Skips are no longer silent (`SYSLIB1101`) When a property is skipped **specifically because** its type contains unresolvable metadata, the generator now emits the existing `SYSLIB1101` warning (*"Property '{0}' on type '{1}' is not supported."*) at the binder call site, so users know binding was dropped and can add the missing reference. Other intentionally-unsupported types (`IntPtr`, delegates, …) remain silent, matching existing behavior. This makes the reflection-vs-source-gen parity gap visible and actionable without falling back to reflection (which would undermine trimming in the reported scenario). ### 3. Regression coverage without disk I/O `IgnorePropertiesWithUnresolvableMetadataTypes` composes a transitive-dependency assembly, a model assembly referencing it, and a generator input that references **only the model** — so the affected member types become unresolved error symbols. It asserts: - the unresolvable members (value-type nullable, reference type, and generic wrappers) are excluded from the generated code, - the bindable member still binds, - a `SYSLIB1101` warning is reported for each skipped member. The test references the model as an **in-memory `MetadataReference`** (the test driver was extended to accept `IEnumerable<MetadataReference>`), replacing the previous approach of writing assemblies to disk and using `Assembly.LoadFrom` — which locked the files on Windows and leaked a temp directory on every run. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: rosebyte <[email protected]> Co-authored-by: Jaroslav Ruzicka <[email protected]>
Summary
When the Configuration Binder source generator processes a type whose members reference types from a transitive assembly that isn't directly referenced by the consumer (or that resolve ambiguously), those member types degrade to Roslyn error symbols. The generator previously emitted binding code that named these types, producing uncompilable output — missing-type (
CS0012/CS0234) and namespace-ambiguity errors.The reflection-based binder doesn't hit this (the assemblies are present at runtime), so the source generator was strictly worse: it broke the build. This is the failure reported in #109759 (
Microsoft.IdentityModel.S2S.DstsExtensionswith trimming, involvingCredentialDescription/HttpRequestMessage).Fixes #109759.
Changes
1. Robust unsupported-type detection
IsUnsupportedTypenow treats a member type as unsupported when it — or a nested array element or generic type argument — is an error symbol, via a new recursiveContainsErrorTypehelper. This covers:HttpRequestMessage(unresolved reference type),Nullable<T>over an unresolved value type — e.g.ValueTypeMessage?,Wrapper<Missing>,System.Tuple<int, Missing>,KeyValuePair<int, Missing>?,Missing[].2. Skips are no longer silent (
SYSLIB1101)When a property is skipped specifically because its type contains unresolvable metadata, the generator now emits the existing
SYSLIB1101warning ("Property '{0}' on type '{1}' is not supported.") at the binder call site, so users know binding was dropped and can add the missing reference. Other intentionally-unsupported types (IntPtr, delegates, …) remain silent, matching existing behavior.This makes the reflection-vs-source-gen parity gap visible and actionable without falling back to reflection (which would undermine trimming in the reported scenario).
3. Regression coverage without disk I/O
IgnorePropertiesWithUnresolvableMetadataTypescomposes a transitive-dependency assembly, a model assembly referencing it, and a generator input that references only the model — so the affected member types become unresolved error symbols. It asserts:SYSLIB1101warning is reported for each skipped member.The test references the model as an in-memory
MetadataReference(the test driver was extended to acceptIEnumerable<MetadataReference>), replacing the previous approach of writing assemblies to disk and usingAssembly.LoadFrom— which locked the files on Windows and leaked a temp directory on every run.