Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Tuesday, October 26, 2021

Visual Studio 2019 Was "Unable to Locate .NET Core SDK"

Earlier today I noticed that Visual Studio 2019 (v16.11.5) could not load any of my .NET Core projects projects. When I tried to reload any of them, I got an error saying that "The project file cannot be opened. Unable to locate the .NET SDK" in the output pane, and then nothing happened.

A google search yielded suggestions saying that the version 2.x.xxx .NET Core SDK needed to be installed. Some said that "C:\Program Files\dotnet\" needed to be part of the "PATH" environment variable. Another said that an old "global.json" file was the cause. While each of these solutions worked for some folks, none seemed to work for me. I ran "dotnet --info" on the command line, and this is what I saw:


"No SDKs were found." Then I noticed a comment about x86 version of dotnet at https://superuser.com/questions/1431833/visual-studio-2019-unable-to-locate-net-core-sdk/1441219. I realized that I was running the x86 version of dotnet. Of course, in hindsight, the "C:\Program Files (x86)" paths in the runtime locations should have been a clear indication of the problem. Then I tried running the 64 bit version:


Well, now a whole list of SDKs showed up. It turned out that the paths to both the x86 version of dotnet and the 64 bit dotnet were in my "PATH" environment variable, and the x86 version was listed first. I do not know which installer made this change, for the projects were working fine at one time. I removed the path to the x86 version of dotnet from the "PATH" environment variable, and now everything works fine.

One interesting side-note: Window 11 complained about the size of the "PATH" environment variable, saying that its length had exceeded 2048. I replaced some folder paths with environment variable references, and reduced the length to below 2048. I wondered: when installer apps increased the "PATH" environment variable length to beyond 2048, why did not Windows complain? Would Windows just ignore the part after the first 2048 characters? Or would Windows use the whole "PATH" environment variable, even though its length exceeded 2048, just because some installer app did this? It turns out that a "PATH" environment variable longer than 2048 can cause real problems: https://superuser.com/questions/355594/windows-7s-path-and-environment-variables-are-corrupted, at least for older versions of Windows. For Windows 10, there is a discussion here: https://stackoverflow.com/questions/34491244/environment-variable-is-too-large-on-windows-10. From the look of the error dialog box, this should apply to Windows 11 as well.





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.