dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

Improve Source Link navigation for simple async methods #64336

Closed vsfeedback closed 1 year ago

vsfeedback commented 1 year ago

This issue has been moved from a ticket on Developer Community.

Original title: F12 on C# extension method (partial class) opens wrong SourceLink .cs file


I'm using Nuget Telegram.Bot 18.0.0, which is compiled with SourceLink information. The library has 2 different .cs files for the static partial class TelegramBotClientExtensions (see https://github.com/TelegramBots/Telegram.Bot/tree/master/src/Telegram.Bot)

When I press F12 in my code on a call to an extension method that resides in the TelegramBotClientExtensions.ApiMethods.cs file, Visual Studio navigates to the wrong file TelegramBotClientExtensions.Polling.cs (even though the tab title says otherwise, see screenshot)

Example source: press F12 on GetMeAsync

using Telegram.Bot;

private static async void Test()
{
    var bot = new TelegramBotClient("dummy");
    await bot.GetMeAsync();
}

image


Original Comments

Feedback Bot on 21/09/2022, 11:56 AM:

(private comment, text removed)

Feedback Bot on 27/09/2022, 06:10 PM:

(private comment, text removed)


Original Solutions

(no solutions)

davidwengier commented 1 year ago

I think we can do a better job of handling async methods. What is happening here is that first we see if we can find source file information for the GetMeAsync method, and use that. Unfortunately that check fails because there is no document info for that method, because it contains only the very basic async state machinery code:

image image

We can probably do a better job of detecting that the method is an async method, and trying to navigate to the MoveNext method on the generated async state machine in that scenario, which does have document info in the PDB. If that were to happen, then we would find the exact right file, and navigation would succeed:

image

Since that doesn’t happen, we revert back to trying to find source document information for the containing type (TelegramBotClientExtensions), which is a partial type, so we navigate to the first one. Better handling for scenarios where there are multiple potential source files is already tracked by https://github.com/dotnet/roslyn/issues/59713

ryzngard commented 1 year ago

Closing as dupe of #59713, but feel free to reopen if you prefer to keep separate @davidwengier