msbuild: Version mismatch between Roslyn in MSBuild and CodeStyle analyzers in the SDK
Not sure if it’s a bug, or whether it should go here or in the SDK.
There may be a situation where MSBuild.exe is started from C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\MSBuild.exe
, which ships Microsoft.CodeAnalysis.dll
, say, 4.2.0.0.
Which MSBuild.exe gets loaded depends on your PATH and which Developer Command Prompt you have open.
At the same time global.json determines which .NET SDK is resolved during the build. So you may end up in a situation where a CodeStyle analyzer is loaded from a location such as:
C:\Program Files\dotnet\sdk\6.0.400-preview.22330.6\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CodeStyle.dll
and that version may be built against Roslyn 4.3.0.0
. This will result in warnings such as:
CSC warning CS8032: An instance of analyzer Microsoft.CodeAnalysis.MakeFieldReadonly.MakeFieldReadonlyDiagnosticAnalyzer cannot be created from C:\Program Files\dotnet\sdk\6.0.400-preview.22330.6\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CodeStyle.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..
It feels like there’s a fundamental design flaw that can often lead to a mismatch like this, since Roslyn .dlls ship with MSBuild and the CodeStyle analyzers ship with the SDK. I don’t know how to fix this, but perhaps we should consider either shipping the analyzers with MSBuild, or shipping the Roslyn compilers and binaries with the SDK. I’m guessing the former will be drastically simpler than the latter.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 29 (24 by maintainers)
Commits related to this issue
- Framework compilers package for MSBuild consumption https://github.com/dotnet/msbuild/issues/7832 — committed to jaredpar/roslyn by jaredpar 2 years ago
- Framework compilers package for SDK consumption This is the Roslyn side of the fix for [msbuild 7832](https://github.com/dotnet/msbuild/issues/7832) It provides a NuPkg that contains _only_ the .NET ... — committed to jaredpar/roslyn by jaredpar 2 years ago
- Framework compilers package for SDK consumption This is the Roslyn side of the fix for [msbuild 7832](https://github.com/dotnet/msbuild/issues/7832) It provides a NuPkg that contains _only_ the .NET ... — committed to jaredpar/roslyn by jaredpar 2 years ago
- Framework compilers package for SDK consumption This is the Roslyn side of the fix for [msbuild 7832](https://github.com/dotnet/msbuild/issues/7832) It provides a NuPkg that contains _only_ the .NET ... — committed to jaredpar/roslyn by jaredpar 2 years ago
- Framework compilers package for SDK consumption This is the Roslyn side of the fix for [msbuild 7832](https://github.com/dotnet/msbuild/issues/7832) It provides a NuPkg that contains _only_ the .NET ... — committed to jaredpar/roslyn by jaredpar 2 years ago
- Framework compilers package for SDK consumption (#66206) * Framework compilers package for SDK consumption This is the Roslyn side of the fix for [msbuild 7832](https://github.com/dotnet/msbuild/i... — committed to dotnet/roslyn by deleted user a year ago
Forgot to publish our meeting notes on how we’re going to move forward with this. In short:
<UseRoslynSdk>
. When this is truthy and building from .NET Framework the desktop compilers will be installed and used vs. grabbing the compilers from the MSBuild installThis will fix the analyzer / generator inconsistency issues because it means the desktop compiler will match the expected API for analyzers and generators.
This broke DocFx too:
https://github.com/dotnet/docfx/issues/8097
the 7.0.3xx port was approved for release in July. There won’t be earlier ports as 7.0.2xx is out of support and the Roslyn side changes aren’t in 17.4 (7.0.1xx).
We changed to using BuildWithNetFrameworkHostedCompiler instead of UseRoslynSdk.
PR is here: https://github.com/dotnet/sdk/pull/29787
So yes! We now support that. I also have a PR out to backport it to 7.0.3xx; we’ll see if we need it in an earlier branch or not later.
The code style analyzers are version-locked to the version of Roslyn they are developed against. Other analyzers target earlier versions of Roslyn, but these cannot. The only available option is to downgrade global.json to an earlier SDK, or update Visual Studio to a newer release that matches the SDK.
The code style analyzers are frequently updated to use new syntax APIs as the compiler creates new features, and there is no straightforward way to lock the code style analyzer package to an earlier version of Roslyn that does not provide those APIs. We originally tried to avoid this, and it resulted in multiple prolonged cases where the code style analyzers in the SDK failed to recognize new language features with many user-visible bugs as a result.
It’s unfortunately not that easy. It requires a significant amount of reworking of the targets in addition to that.
I agree it’s a win but it’s also work that doesn’t actually fix the problem. It’s a “fix Microsoft but leave the community behind” type of fix.
I think even if it fixes just the CodeStyle analyzers, it will be a huge win. In my view we need to decouple this from the general “fix all analyzers” problem as that seems much more monumental.
I just cloned a repo that targets 17.3 on a machine with 17.2 and getting these:
I honestly think it’s a bad experience for our ecosystem and we need to move the analyzers from SDK into the MSBuild toolset next to the Roslyn compiler binaries.