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

Updating patch version does not cleanly remove old SDKs #34764

Open benmcmorran opened 1 year ago

benmcmorran commented 1 year ago

Describe the bug

I have a project using .NET SDK 7.0.200 via a global.json file which specifies "rollForward": "patch". Windows Update periodically updates the installed version of the SDK on my machine to 7.0.203, presumably to address security vulnerabilities. However, when this update happens, it deletes C:\Program Files\dotnet\sdk\7.0.200\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props but does not delete the containing directory C:\Program Files\dotnet\sdk\7.0.200\Sdks\Microsoft.NET.Sdk\Sdk. This confuses MSBuild into thinking that the 7.0.200 SDK is still available, which breaks my build.

To Reproduce

  1. Create a basic project with this global.json file:
    {
    "sdk": {
      "version": "7.0.200",
      "rollForward": "patch",
      "allowPrerelease": false
    }
    }
  2. Install .NET SDK 7.0.200.
  3. Run msbuild on the command line to build the project.
  4. Update to .NET SDK 7.0.203.
  5. Attempt to build the project again. It fails with this message:
error MSB4019: The imported project "C:\Program Files\dotnet\sdk\7.0.200\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\7.0.200\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props" is correct, and that the file exists on disk.

Exceptions (if any)

None.

Further technical details

Found .NET SDK, but did not find dotnet.dll at [C:\Program Files\dotnet\sdk\7.0.200\dotnet.dll]

A ProcMon trace shows MSBuild discovering the partially removed 7.0.200 installation:

image
vitek-karas commented 1 year ago

This will not be a problem in .NET 8 anymore: https://github.com/dotnet/runtime/pull/89333

You could workaround it even now - install a preview version of .NET 8 (or ideally RC1 when it comes out soon). It will add a newer version of hostfxr with the change and should work regardless of the version of the SDK your project is meant to use.

I think this has been discussed before (Can't find the issue), it happens because the windows update tried to remove the old patch while it was in use, so instead it scheduled file removal upon reboot. But that will only remove files and not directories. I'm no expect on this - so maybe somebody who knows the specific details can chime in here.