You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Fact]publicasyncTaskNullableEnableShippedAPI_FixAll(){varsource=@"#nullable enablepublic class C1{ public string? Field;}";varshippedText1=@"";varunshippedText=@"C1C1.C1() -> voidC1.Field -> string";varshippedText2=@"";varfixedShippedText=@"#nullable enable";vartest=newCSharpCodeFixTest<DeclarePublicApiAnalyzer,NullableEnablePublicApiFix,XUnitVerifier>();test.TestState.Sources.Add(source);test.TestState.AdditionalFiles.Add((DeclarePublicApiAnalyzer.ShippedFileName,shippedText1));test.TestState.AdditionalFiles.Add((DeclarePublicApiAnalyzer.UnshippedFileName,unshippedText));test.SolutionTransforms.Add((solution,projectId)=>{varproject1=solution.GetProject(projectId);varproject2=solution.AddProject("Project2","Project2",LanguageNames.CSharp).AddDocument("File.cs",source).Project.AddAdditionalDocument(DeclarePublicApiAnalyzer.ShippedFileName,shippedText2).Project.AddAdditionalDocument(DeclarePublicApiAnalyzer.UnshippedFileName,unshippedText).Project.AddMetadataReferences(project1.MetadataReferences).WithCompilationOptions(project1.CompilationOptions);returnproject2.Solution;});test.TestState.ExpectedDiagnostics.AddRange(new[]{// /0/Test0.cs(5,20): warning RS0037: PublicAPI.txt is missing '#nullable enable', so the nullability annotations of API isn't recorded. It is recommended to enable this tracking.newDiagnosticResult(DeclarePublicApiAnalyzer.ShouldAnnotateApiFilesRule).WithSpan(5,20,5,25),// File.cs(5,20): warning RS0037: PublicAPI.txt is missing '#nullable enable', so the nullability annotations of API isn't recorded. It is recommended to enable this tracking.newDiagnosticResult(DeclarePublicApiAnalyzer.ShouldAnnotateApiFilesRule).WithSpan("File.cs",5,20,5,25),});test.FixedState.Sources.Add(source);test.FixedState.AdditionalFiles.Add((DeclarePublicApiAnalyzer.ShippedFileName,fixedShippedText));test.FixedState.AdditionalFiles.Add((DeclarePublicApiAnalyzer.UnshippedFileName,unshippedText));test.FixedState.ExpectedDiagnostics.AddRange(new[]{// /0/Test0.cs(5,20): warning RS0036: Symbol 'Field' is missing nullability annotations in the declared API.newDiagnosticResult(DeclarePublicApiAnalyzer.AnnotateApiRule).WithSpan(5,20,5,25).WithArguments("Field"),// File.cs(5,20): warning RS0037: PublicAPI.txt is missing '#nullable enable', so the nullability annotations of API isn't recorded. It is recommended to enable this tracking.newDiagnosticResult(DeclarePublicApiAnalyzer.ShouldAnnotateApiFilesRule).WithSpan("File.cs",5,20,5,25),});awaittest.RunAsync();}
Got the below null reference exception in test execution:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
CodeFixContext.ctor(Document document, TextSpan span, ImmutableArray`1 diagnostics, Action`2 registerCodeFix, Boolean verifyArguments, CancellationToken cancellationToken)
CodeFixContext.ctor(Document document, Diagnostic diagnostic, Action`2 registerCodeFix, CancellationToken cancellationToken)
CodeFixTest`1.FixEachAnalyzerDiagnosticAsync(ImmutableArray`1 analyzers, ImmutableArray`1 codeFixProviders, Nullable`1 codeFixIndex, String codeFixEquivalenceKey, Project project, Int32 numberOfIterations, IVerifier verifier, CancellationToken cancellationToken) line 495
CodeFixTest`1.VerifyFixAsync(String language, ImmutableArray`1 analyzers, ImmutableArray`1 codeFixProviders, SolutionState oldState, SolutionState newState, Int32 numberOfIterations, Func`9 getFixedProject, IVerifier verifier, CancellationToken cancellationToken) line 404
CodeFixTest`1.VerifyFixAsync(SolutionState testState, SolutionState fixedState, SolutionState batchFixedState, IVerifier verifier, CancellationToken cancellationToken) line 371
CodeFixTest`1.RunAsync(CancellationToken cancellationToken) line 247
NullableEnablePublicApiAnalyzerTests.NullableEnableShippedAPI_FixAll() line 263
--- End of stack trace from previous location where exception was thrown ---
The text was updated successfully, but these errors were encountered:
It looks like this occurs if you have a code fix provider registered for a diagnostic which gets reported in an additional file, as opposed to a source file. The scenario was missed in testing because it doesn't work in the UI either.
Add the below unit test to https://github.com/dotnet/roslyn-analyzers/blob/master/src/PublicApiAnalyzers/UnitTests/NullableEnablePublicApiAnalyzerTests.cs
Got the below null reference exception in test execution:
The text was updated successfully, but these errors were encountered: