Skip to content

OptionsValidatorGenerator emits ambiguous System.Diagnostics reference — should use global::System.Diagnostics #118476

Closed

Description

Description

The OptionsValidatorGenerator from Microsoft.Extensions.Options.SourceGeneration emits attributes like this:

namespace XXX { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "9.0.12.31616")] internal sealed partial class __AppSettingsValidator__ { /// <summary> /// Validates a specific named options instance (or all when <paramref name="name"/> is <see langword="null" />). /// </summary> /// <param name="name">The name of the options instance being validated.</param> /// <param name="options">The options instance.</param> /// <returns>Validation result.</returns> [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Options.SourceGeneration", "9.0.12.31616")] [System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2026:RequiresUnreferencedCode", Justification = "The created ValidationContext object is used in a way that never call reflection")]

Reproduction Steps

If the consuming project has a namespace like XXX.System, the generated code fails to compile
The type or namespace name 'Diagnostics' does not exist in the namespace 'XXX.System' (are you missing an assembly reference?)

namespace XXX;

[OptionsValidator]
public partial class AppSettingsValidator : IValidateOptions<AppSettings>
{
}
namespace XXX.System
public class SomeOtherClass {}

Expected behavior

The generator should emit the attribute as:
[global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(...)]

Actual behavior

The generator emits the attribute as:
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(...)]

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Aug 7, 2025
  2. dotnet-policy-service commented on Aug 7, 2025

    @dotnet-policy-service
    Contributor

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

  3. added this to the Future milestone on Aug 7, 2025
  4. removed
    untriaged
    on Aug 7, 2025
  5. added
    source-generator
    on Mar 16, 2026
  6. rosebyte commented on Mar 17, 2026

    @rosebyte
    Member

    Triage: we will fix it when we have capacity.

  7. added a commit that references this issue on Mar 21, 2026
    c8d065d
  8. added a commit that references this issue on Mar 24, 2026
    6cbc8ed
  9. locked and limited conversation to collaborators on Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Optionsbugsource-generator

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions