Skip to content

Configuration Binder SG: ignore unresolvable metadata types to prevent ambiguous/missing-type emissions - #130118

Merged
rosebyte merged 12 commits into
mainfrom
copilot/fix-configuration-binder-errors
Jul 24, 2026
Merged

rosebyte merged 12 commits into
mainfrom
copilot/fix-configuration-binder-errors

Conversation

Copilot AI commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

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.DstsExtensions with trimming, involving CredentialDescription / HttpRequestMessage).

Fixes #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.

Copilot AI requested review from Copilot and removed request for Copilot July 2, 2026 10:50
Copilot AI requested review from Copilot and removed request for Copilot July 2, 2026 11:34
Copilot AI changed the title [WIP] Fix namespace ambiguity and not found error in Configuration Binder Configuration Binder SG: ignore unresolvable metadata types to prevent ambiguous/missing-type emissions Jul 2, 2026
Copilot AI requested a review from rosebyte July 2, 2026 11:35
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added this to the 11.0.0 milestone Jul 2, 2026
Copilot AI review requested due to automatic review settings July 14, 2026 19:09
@rosebyte
rosebyte temporarily deployed to copilot-pat-pool July 14, 2026 19:09 — with GitHub Actions Inactive
@rosebyte
rosebyte temporarily deployed to copilot-pat-pool July 14, 2026 19:10 — with GitHub Actions Inactive
@rosebyte
rosebyte marked this pull request as ready for review July 14, 2026 19:14
@rosebyte
rosebyte temporarily deployed to copilot-pat-pool July 14, 2026 19:14 — with GitHub Actions Inactive
@azure-pipelines

Copy link
Copy Markdown
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.

@rosebyte
rosebyte temporarily deployed to copilot-pat-pool July 14, 2026 19:15 — with GitHub Actions Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI requested review from Copilot and removed request for Copilot July 14, 2026 19:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 23, 2026 16:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@rosebyte
rosebyte requested a review from tarekgh July 23, 2026 16:50
@tarekgh

tarekgh commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

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. new-shadowed error-typed property warns twice. When a derived type re-declares an error-typed property with new (e.g. public new Missing? Foo), both the base and derived copies are error-typed, so both get skipped and neither is added to properties. Because the dedup only keys off properties, neither entry suppresses the other and SYSLIB1101 fires twice for the same name. The override case is unaffected (still one warning). This is only cosmetic (a duplicate warning, not a compile error).

2. Nested Outer<Unresolved>.Inner is not detected. For a member typed Outer<Missing>.Inner, Roslyn gives the Inner symbol TypeKind.Class with its own TypeArguments empty; the error symbol lives in ContainingType.TypeArguments. ContainsErrorType only inspects TypeArguments and array elements, so it returns false, the member is not skipped, and the emitter names global::Outer<global::Missing>.Inner -> CS0234. A one-liner would cover it:

if (type.ContainingType is not null && ContainsErrorType(type.ContainingType))
{
    return true;
}

Both are pretty exotic, so no worries either way. Thanks!

Copilot AI review requested due to automatic review settings July 24, 2026 08:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 24, 2026 11:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 24, 2026 13:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

@rosebyte
rosebyte enabled auto-merge (squash) July 24, 2026 14:18
@rosebyte
rosebyte merged commit 2a16a4c into main Jul 24, 2026
85 checks passed
@rosebyte
rosebyte deleted the copilot/fix-configuration-binder-errors branch July 24, 2026 17:22
hez2010 pushed a commit to hez2010/runtime that referenced this pull request Jul 26, 2026
…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]>
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-rc1 Jul 26, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Extensions-Configuration source-generator Indicates an issue with a source generator feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configuration Binder source generator creates namespace ambiguity and not found error

5 participants