Skip to content

Add global:: qualifier to System namespaces in OptionsValidatorGenerator - #125882

Merged
svick merged 2 commits into
dotnet:mainfrom
haltandcatchwater:fix/options-validator-global-qualifier
Mar 24, 2026
Merged

svick merged 2 commits into
dotnet:mainfrom
haltandcatchwater:fix/options-validator-global-qualifier

Conversation

@haltandcatchwater

Copy link
Copy Markdown
Contributor

Note

This contribution was authored with the assistance of AI (Claude Code).

Resolves #118476. Following up on report by @Ternstrom.

Summary

The OptionsValidatorGenerator emitted [System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(...)] without the global:: prefix. When the consuming project contains a namespace like XXX.System, the compiler resolves System.Diagnostics against that namespace instead of the global one, causing:

CS0234: The type or namespace name 'Diagnostics' does not exist in the namespace 'XXX.System'

All other attributes in the emitter already use global:: consistently — this was the only one missed.

Changes

  • Emitter.cs: Add global:: prefix to the UnconditionalSuppressMessage attribute emission
  • 7 baseline files: Updated to match the new generated output

Test Plan

  • Existing source generator baseline tests verify the generated output matches (these are exact string comparisons)
  • CI validates the fix compiles and all existing tests pass

…datorGenerator

The source generator emitted [System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage]
without the global:: prefix. When the consuming project contains a namespace like
XXX.System, the compiler resolves System.Diagnostics against that namespace instead of
the global one, causing CS0234.

All other attributes in the emitter already use global:: consistently; this was the
only one missed.

Fix dotnet#118476
@rosebyte

Copy link
Copy Markdown
Member

@copilot please invoke the code-review skill and post your analysis/results as a comment on this PR

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

Updates the Microsoft.Extensions.Options source generator output to consistently qualify UnconditionalSuppressMessage with global::, preventing namespace shadowing issues (e.g., consumer-defined XXX.System) that can break compilation.

Changes:

  • Emit [global::System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage(...)] in OptionsValidatorGenerator output.
  • Update generator baseline .g.cs files to reflect the new emitted attribute text.

Reviewed changes

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

Show a summary per file
File Description
src/libraries/Microsoft.Extensions.Options/gen/Emitter.cs Adds global:: qualifier when emitting UnconditionalSuppressMessage to avoid ambiguous System namespace resolution in consuming projects.
src/libraries/Microsoft.Extensions.Options/tests/SourceGenerationTests/Baselines/NetCoreApp/Validators.g.cs Updates baseline output to match the new globally-qualified attribute emission.
src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Baselines/UsingInterfaceAsPropertyTypeForLengthAttributesTests.netcore.g.cs Baseline update for globally-qualified UnconditionalSuppressMessage.
src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Baselines/OptionsExtendingSystemClassTest.netcore.g.cs Baseline update for globally-qualified UnconditionalSuppressMessage.
src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Baselines/GeneratedAttributesTest.netcore.lang11.g.cs Baseline update for globally-qualified UnconditionalSuppressMessage.
src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Baselines/GeneratedAttributesTest.netcore.lang10.g.cs Baseline update for globally-qualified UnconditionalSuppressMessage.
src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Baselines/EmitterWithCustomValidator.netcore.g.cs Baseline update for globally-qualified UnconditionalSuppressMessage.
src/libraries/Microsoft.Extensions.Options/tests/SourceGeneration.Unit.Tests/Baselines/DataAnnotationAttributesWithParams.g.cs Baseline update for globally-qualified UnconditionalSuppressMessage.

@svick

svick commented Mar 23, 2026

Copy link
Copy Markdown
Member

I have added the global:: prefix to more places in this source generator where they were missing.

svick
svick approved these changes Mar 23, 2026 •
@svick
svick requested a review from rosebyte March 24, 2026 10:10
@rosebyte rosebyte changed the title Add global:: qualifier to UnconditionalSuppressMessage in OptionsValidatorGenerator Add global:: qualifier to System namespaces in OptionsValidatorGenerator Mar 24, 2026
@svick
svick merged commit 6cbc8ed into dotnet:main Mar 24, 2026
83 of 85 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Extensions-Options community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants