Fix source generator diagnostic locations for incremental caching#1153
Merged
stephentoub merged 3 commits intomainfrom Jan 19, 2026
Merged
Fix source generator diagnostic locations for incremental caching#1153stephentoub merged 3 commits intomainfrom
stephentoub merged 3 commits intomainfrom
Conversation
The DiagnosticInfo struct was storing Location objects directly but not including them in equality comparisons. This caused stale Location objects from earlier compilations to be reused when the generator cache returned cached values, leading to exceptions in Roslyn. Fix by: 1. Add LocationInfo record struct that stores serializable location data (file path, text span, line position span) 2. Update DiagnosticInfo to use LocationInfo instead of Location 3. Use record struct equality which automatically includes all fields 4. Reconstruct Location in CreateDiagnostic using Location.Create() Added a test to verify diagnostic locations are properly preserved. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix source generator to return correct diagnostic locations
Fix source generator diagnostic locations for incremental caching
Jan 15, 2026
stephentoub
approved these changes
Jan 19, 2026
eiriktsarpalis
approved these changes
Jan 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DiagnosticInfostored RoslynLocationobjects but excluded them from equality comparisons. When the incremental generator cache returned cached values, stale locations from earlier compilations were reused, causing Roslyn to throw exceptions.Changes
LocationInforecord struct — Stores serializable location data (file path, text span, line span) instead of RoslynLocationobjectsDiagnosticInfoto record struct — UsesLocationInfo?instead ofLocation?; record equality automatically includes all fieldsLocationInfo.ToLocation()creates freshLocationwhen building diagnosticsWarning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
http://crl3.digicert.com:443/DigiCertCSRSA4096RootG5.crl/usr/share/dotnet/dotnet dotnet restore(http block)http://crl3.digicert.com:443/DigiCertTrustedG4RSA4096SHA256TimeStampingCA.crl/usr/share/dotnet/dotnet dotnet restore(http block)http://crl3.digicert.com:443/NETFoundationProjectsCodeSigningCA2.crl/usr/share/dotnet/dotnet dotnet restore(http block)http://crl3.digicert.com:443/sha2-assured-cs-g1.crl/usr/share/dotnet/dotnet dotnet restore(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>Source Generator returns diagnostics with locations outside the current compilation</issue_title>
<issue_description>Originally reported by @AArnott at dotnet/vscode-csharp#8887
Describe the bug
Roslyn is throwing an exception which is being caused by the diagnostics returned from the MCP source generator. The locations being returned for the diagnostics are not from the current Compilation. This is likely caused by not including the location in the equality check leading to returning locations from earlier iterations. See
csharp-sdk/src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs
Line 533 in 774411b
To Reproduce
Steps to reproduce the behavior:
Expected behavior
It works.
Logs
The fix fails with the error given above.
C# output window pane