vs-threading
vs-threading copied to clipboard
VSTHRD002 code fix throws NRE when compile errors are present
Bug description
VSTHRD002 code fix provider throws NRE when activated on a diagnostic with a compile error for one of the arguments.
Repro steps
I had this method, which violated VSTHRD002. Note that there is no using System.Threading;
line for this file, so CancellationToken.None
did not resolve when I activated the light bulb to look at code fixes.
public void Serialize<T>(Stream stream, T value, IFormatterResolver resolver = null)
{
this.SerializeAsync(stream, value, resolver, CancellationToken.None).GetAwaiter().GetResult();
}
Expected behavior
The code fix is not offered, or works.
Actual behavior
System.NullReferenceException : Object reference not set to an instance of an object.
at Microsoft.VisualStudio.Threading.Analyzers.FixUtils.UpdateStatementsForAsyncMethod(BlockSyntax body,SemanticModel semanticModel,Boolean hasResultValue,Boolean returnTypeChanged,CancellationToken cancellationToken)
at async Microsoft.VisualStudio.Threading.Analyzers.FixUtils.MakeMethodAsync(<Unknown Parameters>)
at async Microsoft.VisualStudio.Threading.Analyzers.FixUtils.MakeMethodAsync(<Unknown Parameters>)
at async Microsoft.VisualStudio.Threading.Analyzers.VSTHRD002UseJtfRunCodeFixWithAwait.<>c__DisplayClass3_0.<RegisterCodeFixesAsync>b__0(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
- Version used: 15.8.192
Additional context
This happened while working in the MessagePack-CSharp
repo, fix9
branch.
I'm getting System.InvalidOperationException : Sequence contains no elements
while trying to invoking use await instead
code fix. Here's the repro example:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace TestAsyncAnalyzers
{
class Program
{
static void Main(string[] args)
{
DoSomething(default);
Console.WriteLine("Hello World!");
}
void DoSomething(CancellationToken cancellationToken)
{
Thread.Sleep(1);
Task.Delay(2, cancellationToken).Wait(cancellationToken); // <<-------- Execute code fix here
}
}
}
Current behavior:
System.InvalidOperationException : Sequence contains no elements
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at async Microsoft.VisualStudio.Threading.Analyzers.FixUtils.MakeMethodAsync(<Unknown Parameters>)
at async Microsoft.VisualStudio.Threading.Analyzers.VSTHRD002UseJtfRunCodeFixWithAwait.<>c__DisplayClass3_0.<RegisterCodeFixesAsync>b__0(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
Also occurs in Microsoft Visual Studio 2022 (64-bit) Version 17.1.1
System.InvalidOperationException : Sequence contains no elements
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at async Microsoft.VisualStudio.Threading.Analyzers.Utils.MakeMethodAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.VisualStudio.Threading.Analyzers.VSTHRD002UseJtfRunCodeFixWithAwait.<>c__DisplayClass3_0.<RegisterCodeFixesAsync>b__0(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
Microsoft.VisualStudio.Threading.Analyzers 17.0.64 and Microsoft.VisualStudio.Threading.Analyzers 17.1.46
This also triggered when opening a solution for the first time with the analyzer installed. Microsoft Visual Studio Enterprise 2022 (64-bit) - Current Version 17.4.3
System.InvalidOperationException : Sequence contains no elements at System.Linq.Enumerable.Single[TSource](IEnumerable
1 source)
at async Microsoft.VisualStudio.Threading.Analyzers.FixUtils.MakeMethodAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.VisualStudio.Threading.Analyzers.VSTHRD002UseJtfRunCodeFixWithAwait.<>c__DisplayClass3_0.<RegisterCodeFixesAsync>b__0(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsyncT`
while trying to use the code fixer "use await instead" the same error/stacktrace is observed.
myobject.DoWorkAsync().GetAwaiter().GetResult();
would trigger the error VSTHRD110. by following the suggestion of "use await instead" from the context menu it wouldn't replace .GetAwaiter().GetResult()
with await myobject.DoWorkAsync();
. The following error and stacktrace was reported.
System.InvalidOperationException : Sequence contains no elements at System.Linq.Enumerable.Single[TSource](IEnumerable``1 source) at async Microsoft.VisualStudio.Threading.Analyzers.FixUtils.MakeMethodAsync(<Unknown Parameters>) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.VisualStudio.Threading.Analyzers.VSTHRD002UseJtfRunCodeFixWithAwait.<>c__DisplayClass3_0.<RegisterCodeFixesAsync>b__0(<Unknown Parameters>) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)