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

Exceptions from MemberDataShouldReferenceValidMember #1937

Closed
jnyrup opened this issue May 5, 2019 · 9 comments
Closed

Exceptions from MemberDataShouldReferenceValidMember #1937

jnyrup opened this issue May 5, 2019 · 9 comments

Comments

@jnyrup
Copy link
Contributor

jnyrup commented May 5, 2019

I haven't been able to construct a completely reproducible case, as I know how to exactly trigger the exceptions, but it did happen when trying to create this minimal example.

[Theory]
[MemberData(nameof(TestData))]
public void Foo<T>(T _)
    where T : Exception
{
    Bar<T>();
}

void Bar<T>() where T : Exception { }

public static IEnumerable<object[]> TestData()
{
    yield return new object[] { new AggregateException() };
}
Error	AD0001	Analyzer 'Xunit.Analyzers.MemberDataShouldReferenceValidMember' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.	<Unknown>		1	Active	Analyzer 'Xunit.Analyzers.MemberDataShouldReferenceValidMember' threw the following exception:
'Exception occurred with following context:
Compilation: [Removed]
SyntaxTree: [Removed]
SyntaxNode: MemberData [AttributeSyntax]@[1045..1055) (39,9)-(39,19)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Xunit.Analyzers.MemberDataShouldReferenceValidMember.<>c__DisplayClass2_0.<AnalyzeCompilation>b__0(SyntaxNodeAnalysisContext symbolContext)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__50`1.<ExecuteSyntaxNodeAction>b__50_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.
Severity	Code	Description	Project	File	Line	Suppression State	Detail Description
Warning	AD0001	Analyzer 'Xunit.Analyzers.MemberDataShouldReferenceValidMember' threw an exception of type 'System.ArgumentNullException' with message 'Value cannot be null.
Parameter name: syntax'.	<Unknown>		1	Active	Analyzer 'Xunit.Analyzers.MemberDataShouldReferenceValidMember' threw the following exception:
'Exception occurred with following context:
Compilation: [Removed]
SyntaxTree: [Removed]
SyntaxNode: MemberData(nameof(TestData)) [AttributeSyntax]@[1807..1835) (69,9)-(69,37)

System.ArgumentNullException: Value cannot be null.
Parameter name: syntax
   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.CheckSyntaxNode(CSharpSyntaxNode syntax)
   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetDeclaredSymbol(BaseTypeDeclarationSyntax declarationSyntax, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetDeclaredSymbol(SemanticModel semanticModel, BaseTypeDeclarationSyntax declarationSyntax, CancellationToken cancellationToken)
   at Xunit.Analyzers.MemberDataShouldReferenceValidMember.<>c__DisplayClass2_0.<AnalyzeCompilation>b__0(SyntaxNodeAnalysisContext symbolContext)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__50`1.<ExecuteSyntaxNodeAction>b__50_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.
@qualitas-software
Copy link

Hi Jonas,

I got the very same warning using XUnit on a new .NET Core 2.2, VS 2019 test project, despite the tests running/passing fine.

I upgraded all nugets, cleaned & rebuilt. Warning still present.

Restarted VS2019, re-ran tests and the warning went away.

HTH

@jnyrup
Copy link
Contributor Author

jnyrup commented Jul 31, 2019

Hi Peter.
I haven't seen the warning again, I only reported it to raise awareness about it.
But thanks for the comments anyway.

@mdsundquist
Copy link

I got the very same warning using XUnit on a new .NET Core 2.2, VS 2019 test project, despite the tests running/passing fine.

I upgraded all nugets, cleaned & rebuilt. Warning still present.

Restarted VS2019, re-ran tests and the warning went away.

Just for the record, this exact thing happened to me on the same type of project. It also went away after restarting VS2019.

@jnyrup
Copy link
Contributor Author

jnyrup commented Dec 12, 2019

I hit the exception again.
What I was doing:
(should not be seen as reproducible steps, but maybe it will give the necessary hint)

public static IEnumerable<object[]> TestData()
{
    yield return new object[] { 1 };
    // yield return new object[] { 2 };
}
  • I began running tests from the VS test explorer
  • While building or running, I uncommented the line in TestData
  • I got the same ArgumentNullException as above.

@huzhiguan
Copy link

I got similar warnings with VS2017 and all were gone after I restarted Visual Studio.

@lalexco
Copy link

lalexco commented Apr 7, 2020

I have same issue, sometimes it works sometimes it doesn't.
VS Studio for Mac profressional 8.5.1
Anyone has a permanent solution?

System.Exception : Value cannot be null.
Parameter name: source

` [JsonProperty("ListOfCifs")]
public static List ListOfCifs { get; set; }

   public static IEnumerable<object[]> GetCifs()
    {
        List<object[]> listOfCis = new List<object[]>();

        try
        {
            foreach (var item in CifList)
            {
                var cifValue = CifList.Where(x => x == item as object).ElementAt(0);
                listOfCis.Add(new object[] { cifValue });
            }

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return listOfCis;
    }`

@bradwilson
Copy link
Member

I'm going to close this because everybody seems to think it gets solved by restarting Visual Studio, which would indicate that there is some old DLL being held onto, or something similar...? If someone can provide a repro project that cannot be solved by restarting Visual Studio, please open a new issue. Thanks!

@kzu
Copy link
Contributor

kzu commented Aug 24, 2022

Same here, had to disable all diagnostics starting with "MemberData..." to void build errors, via .editorconfig:


# xUnit1014: MemberData should use nameof operator for member name
dotnet_diagnostic.xUnit1014.severity = none

# xUnit1015: MemberData must reference an existing member
dotnet_diagnostic.xUnit1015.severity = none

# xUnit1016: MemberData must reference a public member
dotnet_diagnostic.xUnit1016.severity = none

# xUnit1017: MemberData must reference a static member
dotnet_diagnostic.xUnit1017.severity = none

# xUnit1018: MemberData must reference a valid member kind
dotnet_diagnostic.xUnit1018.severity = none

# xUnit1019: MemberData must reference a member providing a valid data type
dotnet_diagnostic.xUnit1019.severity = none

# xUnit1020: MemberData must reference a property with a getter
dotnet_diagnostic.xUnit1020.severity = none

# xUnit1021: MemberData should not have parameters if the referenced member is not a method
dotnet_diagnostic.xUnit1021.severity = none

Setting them as anything other than none caused a build error again.

The source is nothing special:

    [Theory]
    [MemberData(nameof(GetTestCases))]
    [MemberData(nameof(GetTestCasesWithTabs))]

@bradwilson
Copy link
Member

@kzu Let me know if you can provide a repro. We have our integration project which doesn't show this issue.

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

No branches or pull requests

7 participants