diff --git a/src/EditorFeatures/CSharpTest/CodeLens/CSharpCodeLensTests.cs b/src/EditorFeatures/CSharpTest/CodeLens/CSharpCodeLensTests.cs index 7c2653a6c1d28..8f43e4be2e8df 100644 --- a/src/EditorFeatures/CSharpTest/CodeLens/CSharpCodeLensTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeLens/CSharpCodeLensTests.cs @@ -350,5 +350,36 @@ private void Test() "; await RunReferenceTest(input); } + + [Fact, Trait(Traits.Feature, Traits.Features.CodeLens)] + [WorkItem(51633, "https://github.com/dotnet/roslyn/issues/51633")] + public async Task TestMethodRefSourceGeneratedDocument() + { + const string input = @" + + + + new Program(); + } +}]]> + + +"; + await RunMethodReferenceTest(input); + } } } diff --git a/src/Features/Core/Portable/CodeLens/CodeLensReferencesService.cs b/src/Features/Core/Portable/CodeLens/CodeLensReferencesService.cs index f25391ebe9b17..be4e0532a4181 100644 --- a/src/Features/Core/Portable/CodeLens/CodeLensReferencesService.cs +++ b/src/Features/Core/Portable/CodeLens/CodeLensReferencesService.cs @@ -240,13 +240,12 @@ private static ISymbol GetEnclosingMethod(SemanticModel semanticModel, Location private static async Task TryGetMethodDescriptorAsync(Location commonLocation, Solution solution, CancellationToken cancellationToken) { - var doc = solution.GetDocument(commonLocation.SourceTree); - if (doc == null) + var document = solution.GetDocument(commonLocation.SourceTree); + if (document == null) { return null; } - var document = solution.GetDocument(doc.Id); var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var fullName = GetEnclosingMethod(semanticModel, commonLocation, cancellationToken)?.ToDisplayString(MethodDisplayFormat);