Skip to content

Fix JsonSourceGenerator crash with predefined type aliases#126944

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/fix-jsonsourcegenerator-unreachable-error
Closed

Fix JsonSourceGenerator crash with predefined type aliases#126944
Copilot wants to merge 3 commits intomainfrom
copilot/fix-jsonsourcegenerator-unreachable-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 15, 2026

Description

JsonSourceGenerator throws InvalidOperationException("Unreachable") when a compilation unit contains a C# 12+ using alias targeting a predefined type (e.g., using A = int;) alongside any attribute-annotated class.

In CSharpSyntaxHelper.AddAliases, usingDirective.Name returns null for predefined type aliases since PredefinedTypeSyntax is not a NameSyntax. The null value was then passed to the GetUnqualifiedName extension method, which doesn't match any NameSyntax subtype in its switch expression and hits the default throw.

// Previously crashed the source generator
using A = int;

[System.Text.Json.Serialization.JsonSerializable(typeof(MyType))]
internal partial class JsonContext : JsonSerializerContext { }

Fix:

  • Store usingDirective.Name in a local and use ?.GetUnqualifiedName() with pattern matching to safely skip null/non-NameSyntax values
  • Add regression test covering int, double, object, string, nuint

The fix applies to CSharpSyntaxHelper.cs in Common/src/Roslyn/, which is shared by both the System.Text.Json and Microsoft.Extensions.Logging Roslyn 4.0 source generators.

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

… int)

In CSharpSyntaxHelper.AddAliases, store usingDirective.Name in a local
variable and use the null-conditional operator when calling
GetUnqualifiedName. This prevents the "Unreachable" exception when
processing C# 12+ type aliases that target predefined types
(e.g., using A = int;), where Name returns null.

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/2c604c37-485d-4953-8e7d-8edbf70f4d1f

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 15, 2026 11:45
Copilot AI changed the title [WIP] Fix JsonSourceGenerator unreachable error for predefined types Fix JsonSourceGenerator crash with predefined type aliases Apr 15, 2026
Copilot AI requested a review from eiriktsarpalis April 15, 2026 11:48
// With C# 12+, aliases can target predefined/built-in types (e.g. `using A = int;`),
// in which case Name will be null. Skip those.
var name = usingDirective.Name;
if (name?.GetUnqualifiedName() is not SimpleNameSyntax simpleName)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot please use braces in the branch following convention elsewhere in the repo.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 063b3c9.

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/16c1618e-a229-4042-9b1e-f0ff206a4903

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonSourceGenerator throws with message "Unreachable" when aliasing a predefined type

2 participants