dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.7k stars 1.06k forks source link

.NET6 - MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found #23311

Open tomabu opened 2 years ago

tomabu commented 2 years ago

.NET6 - MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found

Describe the bug

After installing .NET6 on Windows Server 2016 every project that I try to build on that machine fails due to MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found

To Reproduce

Install latest SDK (.NET6) and Runtime on Windows Server 2016. Doesn't matter if I use .NET installer or binaries or even Visual Studio Build Tools 2022

Exceptions (if any)

On my local dev machine - Windows 10, everything works fine

Further technical details

This machine used to have all the previous .NET Core SDKs and Runtimes. I have uninstalled all of them so my machine is clean. I have also checked Environment Variables and they also look normal.

Windows Server 2016 dotnet --info

.NET SDK (reflecting any global.json):
 Version:   6.0.101
 Commit:    ef49f6213a

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.101\

Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc

.NET SDKs installed:
  6.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
StrajnarFilip commented 2 years ago

I'm having the same error, MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found The issue is only present on .NET 6 SDK, .NET 5 and 3.1 are working fine.

dsplaisted commented 2 years ago

Does this happen when you run dotnet build, or msbuild, or both?

ebmarquez commented 2 years ago

Same issue for myself

msbuild Version

>msbuild -version
Microsoft (R) Build Engine version 17.2.1+52cd2da31 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

17.2.1.25201

dotnet build

>dotnet build
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
  * You intended to execute a .NET program:
      The application 'build' does not exist.
  * You intended to execute a .NET SDK command:
      A compatible installed .NET SDK for global.json version [6.0.300] from [C:\OnRamp\global.json] was not found.
      Install the [6.0.300] .NET SDK or update [C:\OnRamp\global.json] with an installed .NET SDK:
        5.0.408 [C:\Program Files\dotnet\sdk]
        6.0.105 [C:\Program Files\dotnet\sdk]

Build Tools

> Get-VSSetupInstance   

InstanceId          : 8c96e9e2
DisplayName         : Visual Studio Build Tools 2022
InstallationVersion : 17.2.32516.85
InstallationPath    : C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools
InstallDate         : 5/17/2022 10:22:51 AM

Dotnet --info

k>dotnet --info

Host (useful for support):
  Version: 6.0.5
  Commit:  70ae3df4a6

.NET SDKs installed:
  5.0.408 [C:\Program Files\dotnet\sdk]
  6.0.105 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
nh43de commented 2 years ago

Also having this issue in Azure CI

VaslD commented 2 years ago

I think I've found the cause of this problem.

My system environment variables contain an entry for MSBuildSDKsPath, which points to C:\Program Files\dotnet\sdk. It looks harmless.

In C:\Program Files\dotnet\sdk\6.0.301\Current\SolutionFile\ImportAfter\Microsoft.NET.Sdk.Solution.targets, the target definition/template references $(MSBuildSDKsPath)\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets, which resolves to C:\Program Files\dotnet\sdk\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets given MSBuildSDKsPath but there's nothing there.

Deleting that MSBuildSDKsPath from environment variables (and restarting affected terminals and IDEs to apply the change) fixed it for me.

PS: I found out about this because when I did a dotnet restore and the error log became:

C:\Program Files\dotnet\sdk\6.0.301\Current\SolutionFile\ImportAfter\Microsoft.NET.Sdk.Solution.targets(14,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets" is correct, and that the file exists on disk.

Which explained what was happening much better, and pointed so much closer to the real issue in contrast to "SDK not found". "Not found" is like the second hardest problem to solve than "A problem occurred". Please fix this, if not the issue, the message.

primesun commented 2 years ago

Adding my experience: I wasn't getting the MSB4236 error. Instead I was getting error NETSDK1045: The current .NET SDK does not support targeting .NET 6.0. Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports .NET 6.0. after running something like dotnet build or dotnet publish, or even dotnet restore. Notably I was only getting this error in the command line, not in Visual Studio, which found the right SDK without issue.

Deleting that MSBuildSDKsPath from environment variables (and restarting affected terminals and IDEs to apply the change) fixed it for me.

This worked for me too. Mine actually had this value in it before I deleted it: C:\Program Files\dotnet\sdk\5.0.300\Sdks.

I also confirmed that updating the MSBuildSDKsPath to the newer folder (C:\Program Files\dotnet\sdk\6.0.302\Sdks) also works. But since deleting the env var entirely works, there was no point to keep it.

Now I wonder why I even had it in the first place. I wonder if it was something related to vscode...

pantonis commented 2 years ago

I dont see MSBuildSDKsPath in my environment variables. Is this issue gonna be fixed??

drewrodrigues commented 1 year ago

Add the environment variable MSBuildSDKsPath with the value of C:\Program Files\dotnet\sdk\6.0.402\Sdks\ (or whatever your sdk version is). Should do the trick!

ymeric commented 1 year ago

Add the environment variable MSBuildSDKsPath with the value of C:\Program Files\dotnet\sdk\6.0.402\Sdks\ (or whatever your sdk version is). Should do the trick!

This solution fixed my issues with VS 2019 and 2022. After I installed the latest versions of both IDEs they went MIA. 2019 could not load projects, and 2022 could not run them!!! First installed the latest NET 6 SDK which did not resolve the issue. Then applied this solution and voila!!! Thanks Drew.

YouJiYun commented 1 year ago

I think I've found the cause of this problem.

My system environment variables contain an entry for MSBuildSDKsPath, which points to C:\Program Files\dotnet\sdk. It looks harmless.

In C:\Program Files\dotnet\sdk\6.0.301\Current\SolutionFile\ImportAfter\Microsoft.NET.Sdk.Solution.targets, the target definition/template references $(MSBuildSDKsPath)\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets, which resolves to C:\Program Files\dotnet\sdk\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets given MSBuildSDKsPath but there's nothing there.

Deleting that MSBuildSDKsPath from environment variables (and restarting affected terminals and IDEs to apply the change) fixed it for me.

PS: I found out about this because when I did a dotnet restore and the error log became:

C:\Program Files\dotnet\sdk\6.0.301\Current\SolutionFile\ImportAfter\Microsoft.NET.Sdk.Solution.targets(14,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets" is correct, and that the file exists on disk.

Which explained what was happening much better, and pointed so much closer to the real issue in contrast to "SDK not found". "Not found" is like the second hardest problem to solve than "A problem occurred". Please fix this, if not the issue, the message.

It works for me.

lqlam commented 1 year ago

Does this happen when you run dotnet build, or msbuild, or both?

Thank. When I run command "dotnet build" it show error. I have to download Net 7 (x86 and x64) and worked

alpugi commented 7 months ago

Thanks guys, you put me on the right path: I had no environment variable for the sdk path and adding it only led to a more specific error (sub sdk missing). I had to change the Path environment variable (I'm on win 10):

move the "C:\Program Files\dotnet" entry on top of "C:\Program Files(x86)\dotnet"

solved the problem. For me it happened after a Visual Studio update. I did update build tools as well.

eXpl0it3r commented 3 months ago

For me the issue was with Microsoft.Build / Microsoft.Build.Utilites.Core usage in a test project.

I was still using Microsoft.Build 16.x and it seems to clash with dotnet test if your .NET SDK is 6 or 8, which kind of makes sense, when you consider that VS 2019 (V16) doesn't support .NET 6 and newer.

Updating to Microsoft.Build to 17.x fixed the error.

Additional tip: While I'm still running the tests in a NetFx 4.8 test host, I for some reason had to use Microsoft.Build 17.8.3, otherwise I'd run into conflict with System.Collections.Immutable, which was requested for V7.0.0, but Microsoft.Build > 17.8.3 uses V8.0.0.

Enaium commented 3 months ago

I have installed multi msbuild using rust-init and Rider auto-detected it, so I resolved the problem by selecting Rider's msbuild.

eXpl0it3r commented 2 months ago

Running into this issue still with Microsoft.Build in another instance, it does seem like Microsoft.Build can cause issues with MSBuild path resolutions.

This StackOverflow answer helped me: https://stackoverflow.com/a/77621490/1034248

tldr: Use Microsoft.Build.Locator and call MSBuildLocator.RegisterDefaults(); before using any Microsoft.Build functions.

See also: https://learn.microsoft.com/en-us/visualstudio/msbuild/find-and-use-msbuild-versions?view=vs-2022#use-microsoftbuildlocator