If you came across this post it most likely mean that you're hitting the dreadful "SDK targeting" error while trying to build, compile or upgrade a .NET Core 3 project using either Visual Studio 2017, Visual Studio 2019, the dotnet CLI or other building tools.
Here's what the error looks like:
The current .NET SDK does not support targeting .NET Core 3.0. Either target .NET Core 2.1 or lower, or use a version of the .NET SDK that supports .NET Core 3.0.
C:\Program Files\dotnet\sdk\2.1.500\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets 137
The above message could be slightly different, mostly depending on the .NET Core SDKs installed on the machine. For example, it could be any of the following:
... and so on.
As we can easily guess by reading the error, the problem is due to the fact that our system is trying to use the wrong .NET Core SDK (2.x) to compile a project that is targeting .NET Core 3.0. To quickly confirm that, open the <ProjectName>.csjproj project file - which is located in the project root - and check for the presence of the following line:
In this post we'll briefly enumerate a number of workarounds that could be used to fix that issue for good.
#1. Check if .NET Core SDK 3.x is installed
This might sound stupid, but the first thing to do is to be 100% sure that there is a .NET Core SDK 3.x installed on our machine and available within our system PATH. To check this, open a command prompt and type the following command:
> dotnet --list-sdks
Once you hit Enter, you'll see a list of .NET Core SDK installed on your machine. Ensure that there is at least a .NET Core SDK 3.x there, just like shown in the screenshot below:
If there is none, go to the official .NET Core 3 Download page and get it, then install it and try again.
If you have one (or more), check if it's a preview release or not: if it is, such as the one in the above screenshot, go to Workaround #2: if it's not, jump it and go to Workaround #3.
#2. Enable .NET Core SDKs Previews
If you're using a .NET Core 3 preview, there's a high chance that your issue depends on the fact that your Visual Studio installation has not been configured to use previews of the .NET Core SDK. To quickly check that, launch Visual Studio, go to Tools > Options > Environment > Preview Features and ensure that the "Use previews of the .NET Core SDK" option is enabled.
- If the option is not checked, enable it, then close Visual Studio and any dotnet CLI instance/command prompt you might have opened (do not forget to do that): once done, try to compile your project again.
- If the option is checked, or if the Preview Features element is not present, go to the next workaround.
#3. Use Visual Studio 2019
.NET Core 3.0 preview1 was the last version of .NET Core 3.0 that worked with Visual Studio 15.9 (aka Visual Studio 2017): any subsequent release of .NET Core 3.0 (including nightlies) require VS 16.0+, aka Visual Studio 2019. If you have Visual Studio 2017, install VS2019, then try again.
#4. Check the PATH environment variables
Open your Control Panel, then navigate to System > Advanced System Settings > Environment Variables.
Once there, check the PATH variable on both the User Variables and System Variables panels and be sure that the folder referenced by the error message - for example C:\Program Files\dotnet\sdk\2.0.3\sdks\ - is not there: in the unlikely case you find it, delete it. The only folder that you should find there is the root folder, i.e. C:\Program Files\dotnet\ .
#5. Check the MSBuildSDKsPath environment variable
While you are in the Environment Variables modal window, check for the presence of the MSBuildSDKsPath environment variable on both the User Variables and System Variables panels: if you find it, be sure to delete it (better) or to change it to point to your latest .NET Core 3.x SDK installation path (as shown in Workaround #1).
As a matter of fact, deleting that variable is usually a better approach, since the dotnet CLI will always use the most recent SDK, which will most likely be what you'll want it to do in almost any scenario: however, you can try to explicitly set it up as a "last resort workaround", if anything else fails (including the next workarounds).
#6. Watch out for a global.json file
Navigate to your project's root folder and look for a global.json file: if you find one, open it with a text editor and be sure it doesn't contain a SDK reference such as the following:
If it does, you found your culprit: delete the "sdk" key (better) or the whole global.json file (even better, assuming it doesn't contain anything else) and try again. Alternatively, you might specify your latest .NET Core 3.0 SDK installed version (as shown in Workaround #1). Again, using a global.json file is not the best thing you can do, since you'll prevent the dotnet CLI from automatically using the most recent SDK: however, you can try to explicitly set it up as a "last resort workaround", if anything else fails (including the next workarounds).
#7. Check for global settings
Last but not least, be sure to check that the project/solution experiencing the issue has not been created within another solution folder structure which sets the framework version globally - such as a Directory.Build.props file (thanks to Wolf for the suggestion - see comments section).
Regardless of the step you're trying, don't forget to reboot! (thanks to Paul for the advice it in comments).
That's it, at least for now: I sincerely hope that one of these workarounds will work for your scenario.
- Visual Studio Developer Community - issue #388470
- Visual Studio Developer Community - issue #389896
- Visual Studio Developer Community - issue #470616
- StackOverflow - issue #57487260