KB PS Visual Studio Fails to Build NET 8 but Dotnet in Terminal Works - rpapub/WatchfulAnvil GitHub Wiki

Problem: Visual Studio Fails to Build .NET 8 Project Due to MSBuild Version

Symptoms

  • Build Fails in Visual Studio with the error:
    NETSDK1045: The current .NET SDK does not support targeting .NET 8.0.
    Either target .NET 7.0 or lower, or use a version of the .NET SDK that supports .NET 8.0.
    
  • After adding a global.json file to force .NET 8, the error changes to:
    Version 8.0.407 of the .NET SDK requires at least version 17.8.3 of MSBuild.
    The current available version of MSBuild is 17.4.1.60106.
    Change the .NET SDK specified in global.json to an older version that requires the MSBuild version currently available.
    
  • Running dotnet build in the terminal works, but building inside Visual Studio fails.

Root Cause

Visual Studio uses MSBuild, which must be compatible with the .NET SDK version.

  • Without a global.json file, Visual Studio defaults to the oldest installed .NET SDK, which may not support .NET 8.
  • With a global.json file specifying .NET 8.0.407, VS tries to use it but fails because MSBuild 17.4.1 is too old.
  • dotnet build works because it does not depend on MSBuild in the same way VS does.

Solution

Recommended Fix: Update Visual Studio

  1. Open Visual Studio Installer

    • Press Windows + S → Search for Visual Studio Installer → Open it.
  2. Update Visual Studio to the Latest Version

    • Find Visual Studio 2022 in the list.
    • Click Update.
  3. Verify MSBuild Version

    • Open a command prompt and run:
      msbuild -version
      
    • Ensure it is 17.8.3 or later.
  4. Restart Visual Studio and Try Building Again


🔄 Alternative Workarounds

1. Use dotnet build Instead of Visual Studio

  • Open Terminal/Command Prompt inside the project folder and run:
    dotnet build
    

2. Temporarily Downgrade to .NET 7 (Not Recommended)

If you cannot update Visual Studio, you can use an older .NET version:

  1. Open global.json (in the solution root).
  2. Change the contents to:
    {
      "sdk": {
        "version": "7.0.102"
      }
    }
    
  3. Restart Visual Studio and build.

🚨 Warning: This prevents you from targeting .NET 8, which may not be acceptable.


References


Conclusion

The best solution is to update Visual Studio to get MSBuild 17.8.3+, ensuring full compatibility with .NET 8. 🚀