andrewabest / Conventional

A suite of convention specifications for enforcing type and style conventions in your codebase
Microsoft Public License
97 stars 29 forks source link

Fix: Use GetSemanticModelAsync instead of TryGetSemanticModel to make available the SemanticModel in Roslyn conventions #95

Closed paulegradie closed 5 months ago

paulegradie commented 5 months ago

Description

This PR swaps document.TryGetSemanticModel for semanticModel = document.GetSemanticModelAsync().Result.

This PR is based on the details provided in this related issue: https://github.com/andrewabest/Conventional/issues/94 There is a reproduction if this issue linked in the issue.

The summary is: TryGetSemanticModel doesn't seem to work as expected. Testing indicates that GetSemanticModelAsync may be a suitable alternative.

andrewabest commented 5 months ago

@paulegradie looking at the Roslyn source, we should indeed be using GetSemanticModelAsync: https://github.com/dotnet/roslyn/blob/82dac747490ba681577b9d1a47524b9f4e0af137/src/Workspaces/Core/Portable/Workspace/Solution/Document.cs#L236-L245

 /// <summary>
 /// Gets the current semantic model for this document if the model is already computed and still cached.
 /// In almost all cases, you should call <see cref="GetSemanticModelAsync"/>, which will compute the semantic model
 /// if necessary.
 /// </summary>

So this PR should be good to go. One small comment for the review.

andrewabest commented 5 months ago

Fixes #94

paulegradie commented 5 months ago

Ahh - good find. PR's been updated @andrewabest

I'll leave any additional conversions to GetAwaiter() for now.