Upgrading Microsoft.CodeAnalysis.CSharp for Roslyn analyzer - c#

I'm working on a Roslyn C# analyzer, and need to use the RecordDeclarationSyntax type from newer versions of the Microsoft.CodeAnalysis.CSharp nuget package from 3.3.1 to 4.2.0. Unfortunately, it seems that if I upgrade the package alone, I get the following compile error:
error NU1605: Detected package downgrade: Microsoft.CodeAnalysis.Analyzers from 3.3.3 to 2.9.8. Reference the package directly from the project to select a different version.
This makes sense, since the Microsoft.CodeAnalysis.Analyzers version should have the version corresponding with the Microsoft.CodeAnalysis.CSharp release.
If I upgrade both packages (Microsoft.CodeAnalysis.CSharp to 4.2.0 and Microsoft.CodeAnalysis.Analyzers to 3.3.3), the solution compiles, but the analyzer fails to run.
Is there any way that I can use the latest Microsoft.CodeAnalysis.CSharp APIs within an analyzer?

Be mindful when updating the version of the Roslyn packages, because it's a tooling breaking change: greater versions of Roslyn require greater versions of the .NET SDK / Visual Studio (which ships with a .NET SDK in-box) as a consumer.
Have a look at the compatibility of the Roslyn NuGet packages.
If you publish your .NET Analyzers publicly (e.g. via NuGet.org), I suggest targeting the lowest version of Roslyn necessary (where the respective major version of the .NET SDK is still supported) for the widest range of compatibility:
When you just need the C# 9.0 RecordDeclarationSyntax, then prefer targeting Microsoft.CodeAnalysis.CSharp 3.8.0, which requires the .NET SDK 5.0.100 / Visual Studio 2019 v16.8.0.
When you also require the C# 10 ClassOrStructKeyword on record, then prefer targeting Microsoft.CodeAnalysis.CSharp 4.0.1, which requires the .NET 6 SDK 6.0.100 / Visual Studio 2022 v17.0.0.
If you publish and consume your .NET Analyzers within a controlled environment (e.g. within your organization), then I guess targeting the latest version of Roslyn is fair, with the requirement of keeping the .NET SDK / Visual Studio also up to date.

In case someone else has this issue, it took a little bit of digging around, but I ended up needing to update Visual Studio to the latest version, and the .NET SDK to the latest LTS.

Related

issue while installing Square nuget pacge in visual stdio

Error Could not install package 'Square 4.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
i face this issue on version 4.5 , all version of square not installed on this version of website
If you check the page for that package over on nuget.org you'll see that it is built against .NET Standard 1.3. If you then check what .NET version that corresponds to over on Microsofts documentation for .NET Standard you'll also see that .NET Standard corresponds to .NET 4.6. So you can't install this package in a project that targets .NET 4.5.
Also, it is the same requirements for Square 8.1.0 so you should probably get that version instead of 4.0.0.

error : Version 5.0.10 0-alpha1-014696 of the .NET Core SDK requires at least version 16.3.0 of MSBuild

I am trying to build ASP.NET Core Source code locally. As per the instruction mentioned on https://github.com/aspnet/AspNetCore/blob/master/docs/BuildFromSource.md, when i execute
.\restore.cmd
i am getting below error
error : Version 5.0.100-alpha1-014696 of the .NET Core SDK requires at
least version 16.3.0 of MSBuild. The current available version of
MSBuild is 16.0.461.62831. Change the .NET Core SDK specified in
global.json to an older version that requires the MSBuild version
currently available.
I have Windows 10 Pro 1903 and Visual Studio 2019 Version 16.3.5 installed.
Make sure you have run ./eng/scripts/InstallVisualStudio.ps1 to ensure you have the tools required.
Next make sure you enable the option to use previews of the .NET Core SDK in VS2019:
Tools > Options and check Use previews of the .NET Core SDK under
Environment > Preview Features
And yes, I agree, the fact that a setting in VS IDE alters a command-line build is surprising and it seems to be a common error when trying to build the source to miss this setting.
Close Visual Studio and rerun .\restore.cmd

Microsoft.Net.Compilers package breaks when updated

I was wondering what causes the Nuget package Microsoft.Net.Compilers to try to use .NET 4.6 after updating it to its latest version?
The server that my intranet site is being deployed to only has .NET 4.5 and when I update the Microsoft.Net.Compilers package and deploy the site I get an error that says its trying to reference .NET 4.6 even though in my project setting the version is set to .NET 4.5. Is there a fix for this problem or should I just not update?
The Rosyln compiler itself has a dependency on the .NET Framework. This build-time dependency is irrespective of the Framework version your code is targetting.
The Nuget page for Microsoft.Net.Compilers states (at version 2.8.0):
This package can be used to compile code targeting any platform, but
can only be run using the desktop .NET 4.6+ Full Framework.
If you cannot install a newer version of the Framework your best option is to use an earlier version of Microsoft.Net.Compilers. The newest version I can find which appears to support .NET 4.5 is 2.0.1.

Roslyn compiler version used for projects missing Microsoft.Net.Compilers

Solution consists of 2 dll libraries, 1 console app and 1 web app. Only the last project, the web one, uses Microsoft.Net.Compilers NuGet package. Package has been updated to version 2.3.0 now, what means C# 7.1 and VB 15.3.
source: Roslyn NuGet packages
As 3 other projects do not have Microsoft.Net.Compilers NuGet package installed, what Roslyn compiler and C# version do they use?
I use VS.Net 2017 15.2 with .NET Framework 4.7, so I would guess it shall C#7.0, but still Microsoft.Net.Compilers in version 2.2 should be installed there, but is not.
The Microsoft.Net.Compilers package overrides (by setting properties in your project file) the default version of Roslyn included with MSBuild / Visual Studio.
Regular class libraries that don't reference it are compiled using the Roslyn build included with your version of Visual Studio & MSBuild.

nuget package manager version issue VS 2013

I am using VS 2013 and .Net 4.5/4.6. I am not getting this error message:
The 'System.Collections 4.0.10' package requires NuGet client version '3.0' or above, but the current NuGet version is '2.8.60723.765'.
My understanding is that this is due to a ddl that use a higher version of .Net. Just curious is there a way to fix this (e.g. determine the responsibel dll[s])?
Those packages are primarily designed for .NET Core ecosystem,
https://www.nuget.org/packages/System.Collections/4.0.10
That means it should not be used for .NET 4.5 and Visual Studio 2013. I guess that's probably why Microsoft only releases NuGet 3.* for Visual Studio 2015. It makes sense to avoid polluting Visual Studio 2013 users with the brand new bits.
A workaround is to specify a version of a NuGet your client is actually compatible with.
i.e.
System.Collections Version=x.x.x picking of course a slightly older version than the latest one.
You need to update your nuget package manager.
You can download the latest version over here:
https://visualstudiogallery.msdn.microsoft.com/4ec1526c-4a8c-4a84-b702-b21a8f5293ca

Categories