Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not call FindReferencesAsync for null symbol #2089

Merged
merged 5 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
Expand All @@ -6,6 +7,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging;
using OmniSharp.Helpers;
using OmniSharp.Mef;
using OmniSharp.Models;
Expand All @@ -17,11 +19,14 @@ namespace OmniSharp.Roslyn.CSharp.Services.Navigation
public class FindUsagesService : IRequestHandler<FindUsagesRequest, QuickFixResponse>
{
private readonly OmniSharpWorkspace _workspace;
private readonly ILogger<FindUsagesService> _logger;

[ImportingConstructor]
public FindUsagesService(OmniSharpWorkspace workspace)
public FindUsagesService(OmniSharpWorkspace workspace, ILoggerFactory loggerFactory)
{
_workspace = workspace;
_logger = loggerFactory.CreateLogger<FindUsagesService>();

}

public async Task<QuickFixResponse> Handle(FindUsagesRequest request)
Expand All @@ -30,13 +35,20 @@ public async Task<QuickFixResponse> Handle(FindUsagesRequest request)
var document = await _workspace.GetDocumentFromFullProjectModelAsync(request.FileName);
if (document == null)
{
_logger.LogWarning($"No document found. File: {request.FileName}.");
return new QuickFixResponse();
}

var semanticModel = await document.GetSemanticModelAsync();
var sourceText = await document.GetTextAsync();
var position = sourceText.Lines.GetPosition(new LinePosition(request.Line, request.Column));
var symbol = await SymbolFinder.FindSymbolAtPositionAsync(semanticModel, position, _workspace);
if (symbol is null)
{
_logger.LogWarning($"No symbol found. File: {request.FileName}, Line: {request.Line}, Column: {request.Column}.");
return new QuickFixResponse();
}

var definition = await SymbolFinder.FindSourceDefinitionAsync(symbol, _workspace.CurrentSolution);
var usages = request.OnlyThisFile
? await SymbolFinder.FindReferencesAsync(definition ?? symbol, _workspace.CurrentSolution, ImmutableHashSet.Create(document))
Expand All @@ -60,6 +72,7 @@ public async Task<QuickFixResponse> Handle(FindUsagesRequest request)
.OrderBy(q => q.FileName)
.ThenBy(q => q.Line)
.ThenBy(q => q.Column));

}
}
}
22 changes: 22 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,28 @@ public Foo Clone() {
Assert.Equal("a.cs", usages.QuickFixes.ElementAt(0).FileName);
}

[Theory]
[InlineData("public Foo(string $$event)")]
[InlineData("pu$$blic Foo(string s)")]
public async Task DoesNotCrashOnInvalidReference(string methodDefinition)
{
var code = @$"
public class Foo
{{
{methodDefinition}
{{
}}
}}";

var exception = await Record.ExceptionAsync(async () =>
{
var usages = await FindUsagesAsync(code);
Assert.NotNull(usages);
});

Assert.Null(exception);
}

private Task<QuickFixResponse> FindUsagesAsync(string code, bool excludeDefinition = false)
{
return FindUsagesAsync(new[] { new TestFile("dummy.cs", code) }, false, excludeDefinition);
Expand Down