Versions of source generator dependencies

October 14th 2022 Roslyn Visual Studio

When referencing NuGet packages of third-party libraries, you usually want to use the latest stable version available. But you probably should not do that with Microsoft.CodeAnalysis.CSharp if you are developing your own source generator. Otherwise, you unnecessarily limit the versions of Visual Studio in which your source generator will work.

I was revisiting my source generator sample project and decided to update all NuGet dependencies to the latest version. After doing this, I received the following error in the client project that references my source generator:

An instance of analyzer BuildInfo.Generator.BuildInfoGenerator cannot be created from C:\Users\damir\Git\Blog\dnc-source-generators\BuildInfo.Generator\bin\Debug\netstandard2.0\BuildInfo.Generator.dll : Could not load file or assembly Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 or one of its dependencies. The system cannot find the file specified..

The problem disappeared when I downgraded the Microsoft.CodeAnalysis.CSharp NuGet package by a few minor versions.

This made me curious about what the maximum supported version of the package is in a particular version of Visual Studio. I found the information I was looking for in Help > About Microsoft Visual Studio:

About Microsoft Visual Studio 2022

Note the highlighted version of C# Tools. Its version matches the highest supported minor version of Microsoft.CodeAnalysis.CSharp, which in this case is 4.3.x.

Earlier versions of Visual Studio 2022 contain an older version of C# Tools. For example, version 17.1.5 contains C# Tools 4.1.0, so the source generators must reference Microsoft.CodeAnalysis.CSharp 4.1.x or older, or they will not work in this version of Visual Studio.

I could not find this documented anywhere, but it appears that the minor version of Visual Studio 2022 matches the minor version of C# Tools (e.g. Visual Studio version 17.3.5 includes C# Tools 4.3.0). This would mean that if you want your source generator to work with any version of Visual Studio 2022, you should reference Microsoft.CodeAnalysis.CSharp 4.0.x. This version already includes the IIncrementalGenerator interface, which is required to implement an incremental source generator. So until the next version of Visual Studio or the next new source generator feature, this is probably the best Microsoft.CodeAnalysis.CSharp version to use.

If for some reason you want to continue supporting Visual Studio 2019, you will need to reference Microsoft.CodeAnalysis.CSharp 3.x instead. The latest version of Visual Studio 2019 includes C# Tools 3.11.0:

About Microsoft VIsual Studio 2022

It's difficult to test source generators with older versions of Visual Studio 2022, but from my research, you can not reference the latest version of Microsoft.CodeAnalysis.CSharp and expect your source generator to work with versions of Visual Studio 2022 that are not fully updated. It is better if you reference Microsoft.CodeAnalysis.CSharp 4.0.x instead, so that the source generator will work with the very first version of Visual Studio 2022.

If you want the source generator to work in Visual Studio 2019, then reference Microsoft.CodeAnalysis.CSharp 3.0.x instead. However, this means that you cannot implement incremental source generators that were first introduced in Microsoft.CodeAnalysis.CSharp 4.0.x.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License