Skip to content

Analyzers crashing in 9.0.309 when used in msbuild #82010

@jaredpar

Description

@jaredpar

Version Used:

9.0.309 .NET SDK and VS 17.14 latest. But this will reproduce with any 17.* version of VS.

Steps to Reproduce:

Create a new console application and add the following into the build file

<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisLevel>latest</AnalysisLevel>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>

Then build the project using msbuild from VS.

> msbuild -v:m -m -restore

This will result in a series of warnings about analyzers not being able to be created

warning CS8032: An instance of analyzer Microsoft.CodeAnalysis.CSharp.ConvertNamespace.ConvertToFileScopedNamespaceDiagnosticAnalyzer cannot be created from C:\Users\jaredpar\temp\console\.complog\export\console-net472\analyzers\group1\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll : Exception has been thrown by the target of an invocation..
warning CS8032: An instance of analyzer Microsoft.CodeAnalysis.CSharp.UseCollectionInitializer.CSharpUseCollectionInitializerDiagnosticAnalyzer cannot be created from C:\Users\jaredpar\temp\console\.complog\export\console-net472\analyzers\group1\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll : Exception has been thrown by the target of an invocation..
warning CS8032: An instance of analyzer Microsoft.CodeAnalysis.CSharp.UseCoalesceExpression.CSharpUseCoalesceExpressionForTernaryConditionalCheckDiagnosticAnalyzer cannot be created from C:\Users\jaredpar\temp\console\.complog\export\console-net472\analyzers\group1\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll : Exception has been thrown by the target of an invocation..
warning CS8032: An instance of analyzer Microsoft.CodeAnalysis.CSharp.UseUtf8StringLiteral.UseUtf8StringLiteralDiagnosticAnalyzer cannot be created from C:\Users\jaredpar\temp\console\.complog\export\console-net472\analyzers\group1\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll : Exception has been thrown by the target of an invocation..

This is happening because of #81315. That PR changed the version of System.Memory referenced by the Roslyn analyzers in the .NET SDK. This was shipped with the .NET SDK but not with the C# compiler in VS. Hence the version used the analyzers in the .NET SDK is now 4.0..20 but the version that is deployed by the C# compiler inside of VS is 4.0.1.2. This leads to an assembly load issue when creating the analyzers.

This does not impact the 10.0 SDK as it uses the C# compiler from the SDK even when invoked from msbuild.

dotnet/sdk PR: dotnet/sdk#52460

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions