WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
16 stars 3 forks source link

TypeInitializationException in CodeContractsAnalyzer #111

Closed brian-reichle closed 4 years ago

brian-reichle commented 4 years ago

I can't get this one to happen with the BulkAnalysisRunner, only with QGL and Visual Studio. (I'm sure Visual Studio had a function to expand the AD0001 errors to get the analyzer's stack trace, but it seems to have gone missing.)

"d:\Regen\Glow\DotNet\Infrastructure\Rules.ScriptGeneration\Rules.BusinessRulesGenerator\Rules.BusinessRulesGenerator\Rules.BusinessRulesGenerator.cw.csproj" (Rebuild target) (1) ->
(CoreCompile target) -> 
  CSC : error AD0001: Analyzer 'WTG.Analyzers.CodeContractsAnalyzer' threw an exception of type 'System.TypeInitializationException' with message 'The type initializer for 'WTG.Analyzers.CodeContractsHelper' threw an exception.'. [d:\Regen\Glow\DotNet\Infrastructure\Rules.ScriptGeneration\Rules.BusinessRulesGenerator\Rules.BusinessRulesGenerator\Rules.BusinessRulesGenerator.cw.csproj]
  CSC : error AD0001: Analyzer 'WTG.Analyzers.CodeContractsAnalyzer' threw an exception of type 'System.TypeInitializationException' with message 'The type initializer for 'WTG.Analyzers.CodeContractsHelper' threw an exception.'. [d:\Regen\Glow\DotNet\Infrastructure\Rules.ScriptGeneration\Rules.BusinessRulesGenerator\Rules.BusinessRulesGenerator\Rules.BusinessRulesGenerator.cw.csproj]
brian-reichle commented 4 years ago

If I trigger the issue with a debugger attached I get:

System.TypeInitializationException: 'The type initializer for 'WTG.Analyzers.CodeContractsHelper' threw an exception.' Inner Exception FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

when CodeContractAnalyzer.AnalyzeAttribute calls CodeContractsHelper.IsCodeContractsSuppression.

When I trial-upgraded Roslyn, there was a new analyzer rule triggered because analyzers are not supposed to access anything from the workspace assemblies, only code fixers can (the members of CodeContractsHelper that access the workspace assembly are not actually used in the analyzer, they are just kept on the same class for the sake of keeping related logic together.)

I'm wondering if maybe visual studio set-up a landmine for analyzers compiled after a certain date?

yaakov-h commented 4 years ago

That might be part of the reason why StyleCop has a separate code fixes assembly... 🤔