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

VSTHRD002 code fix throws NRE when compile errors are present #454

Open
AArnott opened this issue Jan 15, 2019 · 4 comments
Open

VSTHRD002 code fix throws NRE when compile errors are present #454

AArnott opened this issue Jan 15, 2019 · 4 comments
Assignees
Milestone

Comments

@AArnott
Copy link
Member

AArnott commented Jan 15, 2019

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.

@AArnott AArnott added this to the v15.8 milestone Jan 15, 2019
@AArnott AArnott self-assigned this Jan 15, 2019
@cezarypiatek
Copy link
Contributor

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:

image

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>)

@icnocop
Copy link

icnocop commented Mar 13, 2022

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

@bwilliams1
Copy link

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](IEnumerable1 source)
at async Microsoft.VisualStudio.Threading.Analyzers.FixUtils.MakeMethodAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.VisualStudio.Threading.Analyzers.VSTHRD002UseJtfRunCodeFixWithAwait.<>c__DisplayClass3_0.b__0()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.b__0()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsyncT`

@bwilliams1
Copy link

bwilliams1 commented Dec 19, 2022

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>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants