Tuesday, August 2, 2022

CSC : warning CS8032: An instance of analyzer RoslynBasics.MySourceGenerator cannot be created from. Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.2.0.0,

When we are developing .Net Compiler Platform (Roslyn)¹ related code and using it in Visual Studio there are chances of getting the below exception when compiling in Visual Studio 2022.

In my scenario, I got this exception when developing a SourceGenerator².

CSC : warning CS8032: An instance of analyzer RoslynBasics.MySourceGenerator cannot be created from C:\..\RoslynBasics\bin\Debug\netstandard2.0\RoslynBasics.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..

There are many resolutions available on google. Below is the one that worked for me.
Downgrade the Microsoft.CodeAnalysis to 4.1.00

Root cause

Though the latest version is 4.2.0.0 but the Visual Studio 2022 (17.1.1) is unaware of it. Hence it failed. We can check the version of Microsoft.CodeAnalysis that Visual Studio uses in the below folder location.

C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Roslyn

The location of Visual Studio 2019 and other versions is different.

Solution

The right solution can be to upgrade Visual Studio to 17.2.x. 

Once VS 2022 is upgraded the nuget 4.2.0.0 started working fine. 

Above is the new dll version after the upgrade of Visual Studio

References


No comments: