Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 15, 2026

DiagnosticInfo stored Roslyn Location objects 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

  • Added LocationInfo record struct — Stores serializable location data (file path, text span, line span) instead of Roslyn Location objects
  • Converted DiagnosticInfo to record struct — Uses LocationInfo? instead of Location?; record equality automatically includes all fields
  • Reconstruct locations on demandLocationInfo.ToLocation() creates fresh Location when building diagnostics
// Before: Location excluded from equality, causing stale cache hits
private readonly struct DiagnosticInfo
{
    public Location? Location { get; }
    public bool Equals(DiagnosticInfo other) =>
        Id == other.Id && MethodName == other.MethodName; // Location not compared!
}

// After: Record struct includes all fields in equality
private readonly record struct LocationInfo(string FilePath, TextSpan TextSpan, LinePositionSpan LineSpan);
private readonly record struct DiagnosticInfo(string Id, LocationInfo? Location, string MethodName);

Warning

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
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore (http block)
  • http://crl3.digicert.com:443/DigiCertTrustedG4RSA4096SHA256TimeStampingCA.crl
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore (http block)
  • http://crl3.digicert.com:443/NETFoundationProjectsCodeSigningCA2.crl
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore (http block)
  • http://crl3.digicert.com:443/sha2-assured-cs-g1.crl
    • Triggering command: /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

// For incremental generator caching, we compare only the logical content, not the Location object

To Reproduce
Steps to reproduce the behavior:

  1. Open the repo and branch at: https://github.com/coreai-microsoft/vs-mcp/tree/initial-mcp in VS Code
  2. Position the caret at one of the many warnings.
  3. Try to activate a Fix code action.

Expected behavior
It works.

Logs
The fix fails with the error given above.

C# output window pane
2026-01-14 19:56:42.717 [info] Locating .NET runtime version 10.0.0
2026-01-14 19:56:47.690 [info] Dotnet path: C:\Program Files\dotnet\dotnet.exe
2026-01-14 19:56:47.690 [info] Activating C# + C# Dev Kit...
2026-01-14 19:56:48.232 [info] [stdout] info: Program[0]
      Server started with process ID 223696

2026-01-14 19:56:48.879 [info] [stdout] {"pipeName":"\\\\.\\pipe\\e8d2b360"}

2026-01-14 19:56:48.879 [info] received named pipe information from server
2026-01-14 19:56:48.879 [info] client has connected to server
2026-01-14 19:56:48.940 [info] [Program] Language server initialized
2026-01-14 19:56:49.517 [info] [serviceBroker/connect] [WorkspaceProjectFactoryService] Project E:\src\Microsoft.DevDiv.VisualStudio.Mcp\test\Microsoft.DevDiv.VisualStudio.Mcp.Tests\Microsoft.DevDiv.VisualStudio.Mcp.Tests.csproj loaded by C# Dev Kit
2026-01-14 19:56:49.605 [info] [serviceBroker/connect] [WorkspaceProjectFactoryService] Project E:\src\Microsoft.DevDiv.VisualStudio.Mcp\src\Microsoft.DevDiv.VisualStudio.Mcp\Microsoft.DevDiv.VisualStudio.Mcp.csproj loaded by C# Dev Kit
2026-01-14 19:56:49.763 [info] [textDocument/_vs_getProjectContexts] [Microsoft.CodeAnalysis.MSBuild.BuildHostProcessManager] .NET BuildHost started from c:\Users\andarno\.vscode\extensions\ms-dotnettools.csharp-2.111.2-win32-x64\.roslyn\Microsoft.CodeAnalysis.LanguageServer.exe reloading to start from C:\Program Files\dotnet\dotnet.exe to match necessary SDK location.
2026-01-14 19:56:50.633 [warning] [textDocument/_vs_getProjectContexts] [LanguageServerProjectLoader] Project C:\Users\andarno\AppData\Local\Temp\roslyn-canonical-misc\ce7648a5-c814-4034-87b9-bfe3f37db873\Canonical.csproj has unresolved dependencies
2026-01-14 19:56:50.731 [info] [textDocument/_vs_getProjectContexts] [LanguageServerProjectLoader] Successfully completed load of C:\Users\andarno\AppData\Local\Temp\roslyn-canonical-misc\ce7648a5-c814-4034-87b9-bfe3f37db873\Canonical.cs
2026-01-14 19:56:50.733 [info] [textDocument/_vs_getProjectContexts] [LanguageServerProjectLoader] Restoring Canonical.cs: Running dotnet restore on C:\Users\andarno\AppData\Local\Temp\roslyn-canonical-misc\ce7648a5-c814-4034-87b9-bfe3f37db873\Canonical.cs
2026-01-14 19:56:50.735 [info] [textDocument/_vs_getProjectContexts] [Microsoft.CodeAnalysis.LanguageServer.DotnetCliHelper] Using dotnet executable configured on the PATH
2026-01-14 19:56:51.357 [info] [textDocument/_vs_getProjectContexts] [LanguageServerProjectLoader] Restoring Canonical.cs:   Determining projects to restore...
2026-01-14 19:56:51.858 [info] [textDocument/_vs_getProjectContexts] [LanguageServerProjectLoader] Restoring Canonical.cs:   Restored C:\Users\andarno\AppData\Local\Temp\roslyn-canonical-misc\ce7648a5-c814-4034-87b9-bfe3f37db873\Canonical.csproj (in 330 ms).
2026-01-14 19:56:51.905 [info] [textDocument/_vs_getProjectContexts] [LanguageServerProjectLoader] Restore complete
2026-01-14 19:56:51.906 [info] [textDocument/_vs_getProjectContexts] [LanguageServerProjectLoader] Completed (re)load of all projects in 00:00:02.3532891
2026-01-14 19:56:52.850 [info] [workspace/didChangeWatchedFiles] [Microsoft.CodeAnalysis.MSBuild.BuildHostProcessManager] .NET BuildHost started from c:\Users\andarno\.vscode\extensions\ms-dotnettools.csharp-2.111.2-win32-x64\.roslyn\Microsoft.CodeAnalysis.LanguageServer.exe reloading to start from C:\Program Files\dotnet\dotnet.exe to match necessary SDK location.
2026-01-14 19:56:53.621 [warning] [workspace/didChangeWatchedFiles] [LanguageServerProjectLoader] Project C:\Users\andarno\AppData\Local\Temp\roslyn-canonical-misc\ce7648a5-c814-4034-87b9-bfe3f37db873\Canonical.csproj has unresolved dependencies
2026-01-14 19:56:53.622 [info] [workspace/didChangeWatchedFiles] [LanguageServerProjectLoader] Successfully completed load of C:...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes modelcontextprotocol/csharp-sdk#1152

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

Copilot AI and others added 2 commits January 15, 2026 22:16
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 <[email protected]>
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
Copilot AI requested a review from stephentoub January 15, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants