Wednesday, December 11, 2019

Error: "Microsoft.Net.Compilers is only supported on MSBuild v16.3 and above"

After installing "Microsoft.Net.Compilers.3.4.0", I was getting errors when building ASP .NET projects in Visual Studio 2019:

"...\packages\Microsoft.Net.Compilers.3.4.0\build\Microsoft.Net.Compilers.props(14,5): Error : Microsoft.Net.Compilers is only supported on MSBuild v16.3 and above"

This is a bug in Microsoft.Net.Compilers.3.4.0. Someone at Microsoft made a change in the file "Microsoft.Net.Compilers.props" without understanding the different terms. The section in question is

<Target Name="ValidateMSBuildToolsVersion" Condition="'$(BuildingProject)' == 'true'">
 <!-- The new editorconfig support requires MSBuild version 16.3. --> 

 <Error Text="Microsoft.Net.Compilers is only supported on MSBuild v16.3 and above" Condition="'$(MSBuildToolsVersion)' &lt; '16.3'" />
</Target>

Please note that this section is named "ValidateMSBuildToolsVersion", not "ValidateMSBuildVersion". While the "MSBuildVersion" is 16.4 in the "C:\Program Files (x86)\Microsoft Visual Studio\2019\<editionnamehere>\MSBuild\$(MSBuildToolsVersion)\, MSBuildToolsVersion itself is not 16.4! In fact, it may not even be a number, if Visual Studio decides to use the "Current" version. That is why in the prior version of "Microsoft.Net.Compilers.props", the check looked like this:

<Target Name="ValidateMSBuildToolsVersion" Condition="'$(BuildingProject)' == 'true'">
  <Error Text="Microsoft.Net.Compilers is only supported on MSBuild v15.0 and above"
Condition="'$(MSBuildToolsVersion)' == '2.0' OR

 '$(MSBuildToolsVersion)' == '3.5' OR 
 '$(MSBuildToolsVersion)' == '4.0' OR 
 '$(MSBuildToolsVersion)' == '12.0' OR 
 '$(MSBuildToolsVersion)' == '14.0'" />
</Target>


Of course, the error text in the prior version is a little misleading as well: it referred to "MSBuild v15.0", not "MSBuild Tools v15.0".

Right now, the MSBuildToolsVersion is set to "15.0" when you create a new project in Visual Studio 2019. You can use a text editor to open the project file and verify that. If you migrated your project from an older version of Visual Studio, the number could be smaller than 15.0.

But Visual Studio 2019 does not necessarily use this number. If Visual Studio 2019 does not find what it is looking for in "C:\Program Files (x86)\Microsoft Visual Studio\2019\<editionnamehere>\MSBuild\$(MSBuildToolsVersion)\", it would automatically set MSBuildToolsVersion to "Current", and use the folder "C:\Program Files (x86)\Microsoft Visual Studio\2019\<editionnamehere>\MSBuild\Current\". That was why the prior version of "Microsoft.Net.Compilers.props" does not use "Condition="'$(MSBuildToolsVersion)' < '15.0'", since a numeric comparison would not work on "Current"!

So, for now, the fix is to change this section to

<Target Name="ValidateMSBuildToolsVersion" Condition="'$(BuildingProject)' == 'true'">  
 <Error Text="Microsoft.Net.Compilers is only supported on MSBuild Tools v15.0 and above"
Condition="'$(MSBuildToolsVersion)' == '2.0' OR 

 '$(MSBuildToolsVersion)' == '3.5' OR 
 '$(MSBuildToolsVersion)' == '4.0' OR 
 '$(MSBuildToolsVersion)' == '12.0' OR 
 '$(MSBuildToolsVersion)' == '14.0'" />
</Target>


Now I am able to use "Microsoft.Net.Compilers.3.4.0" without errors.