Roslyn-analyzers: Intermittent NullReferenceException in CSharpDiagnosticAnalyzerApiUsageAnalyzer

Created on 8 Aug 2018  路  11Comments  路  Source: dotnet/roslyn-analyzers

_From @yaakov-h on August 7, 2018 3:58_

Version Used:
Microsoft.CodeAnalysis.Analyzers v2.6.1

Steps to Reproduce:

  1. Clone https://github.com/WiseTechGlobal/WTG.Analyzers at commit 223095d2
  2. Build from source

Expected Behavior:

No null reference exceptions in MS analyzers

Actual Behavior:

Sometimes null reference exception in MS analyzers

CSC : error AD0001: Analyzer 'Microsoft.CodeAnalysis.CSharp.Analyzers.MetaAnalyzers.CSharpDiagnosticAnalyzerApiUsageAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. [C:\projectswtg-analyzersWTG.Analyzers.TestWTG.Analyzers.Test.csproj]

_Copied from original issue: dotnet/roslyn#29119_

Area-Microsoft.CodeAnalysis.Analyzers Bug Resolution-Fixed

All 11 comments

_From @sharwell on August 7, 2018 16:24_

@jinujoseph Can you move this to dotnet/roslyn-analyzers

@yaakov-h I am unable to build your solution:

1>------ Build started: Project: WTG.Analyzers.Utils, Configuration: Debug Any CPU ------
1>c:\WTG.Analyzers\WTG.Analyzers.Utils\WTG.Analyzers.Utils.csproj(11,3): error MSB4019: The imported project "c:\WTG.Analyzers\.paket\Paket.Restore.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
1>Done building project "WTG.Analyzers.Utils.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I'm using the latest bits and I still get this warning. I don't want to see any warnings at all. How do I get rid of these?

Here's how you can solve this for the time being: https://github.com/agracio/edge-js/pull/61

@camainc @niklaskihl Do you have a repro that I can use to investigate?

Meanwhile, you can workaround by adding a /nowarn:RS1022 or a ruleset entry: <Rule Id="RS1022" Action="None" />, you don't need to downgrade the analyzer package or Microsoft.CodeAnalysis package version.

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="15.0">
  <Rules AnalyzerId="Microsoft.CodeAnalysis.Analyzers" RuleNamespace="Microsoft.CodeAnalysis.Analyzers">
    <Rule Id="RS1022" Action="None" />
  </Rules>
</RuleSet>

@camainc @niklaskihl Can you see if this comment matches your repro: https://github.com/dotnet/roslyn-analyzers/issues/1888#issuecomment-454969167? If so, can you try to add an explicit package reference to Microsoft.CodeAnalysis.Analyzers version 2.6.3 in your failing project?

@camainc @niklaskihl Can you see if this comment matches your repro: #1888 (comment)? If so, can you try to add an explicit package reference to Microsoft.CodeAnalysis.Analyzers version 2.6.3 in your failing project?

@mavasani This seems to have solved it for us anyway! Thanks!

I'm writing my own analyzer and ran into this exception in the project were I used the analyzer. After some time I finally found out that the way I references Microsoft.CodeAnalysis.Analyzers in the analyzer code is very important. The PrivateAssets="all" is important. If this is not there, then I get the NullReferenceException in the project using the analyzer.

From my analyzer csproj:
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.6.1" PrivateAssets="all" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.9.0" PrivateAssets="all" />

This comment might be misplaced, but I fixed "the issue" for me :)

I looked at the whole thread, I am using VS 2019 Preview, latest patch and I have always had this error on my .Net Core 3.0 projects.

Severity Code Description Project File Line Suppression State Warning AD0001 Analyzer 'Microsoft.CodeAnalysis.CSharp.Diagnostics.RemoveUnnecessaryCast.CSharpRemoveUnnecessaryCastDiagnosticAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. Cats.DeepBook 1 Active

None of the above fixes works for me, I also do not reference Microsoft.CodeAnalysis.Analyzers.

@PeterPann23 - The analyzer in your repro (CSharpRemoveUnnecessaryCastDiagnosticAnalyzer) is different from the analyzer in this issue (CSharpDiagnosticAnalyzerApiUsageAnalyzer)

Was this page helpful?
0 / 5 - 0 ratings