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.65k stars 1.05k forks source link

Hot Reload just doesn't work. #29163

Closed sake402 closed 3 months ago

sake402 commented 1 year ago

Describe the bug

While hot reload was announced to be the game changer for dotnet development, the fact remains that some of us has never gotten it to work. Enabling it in Visual studio brings productivity to a halt and have had it disabled since dotnet 5. Today, I felt to give it another try perhaps something would have changed.

In our large solution, from Visual studio, it still doesnt work. Even adding a simple field to a class ends up with

So I created a basic console app with just the Program .cs and added this to it

while(true){
Console.WriteLine("Hello, World! Guzdot");
Thread.Sleep(500);
}

Did dotnet watch --verbose

hotrelad

And made changes to Console.WriteLine("Hello, World! Guzdot"); string parameter. It says Hot reload of changes succeeded, but no change in the console message

dotnet watch ⌚ Project supports hot reload and was configured to run with the default run-command. Watching with hot-reload
dotnet watch 🔥 Hot reload enabled. For a list of supported edits, see https://aka.ms/dotnet/hot-reload.
  💡 Press "Ctrl + R" to restart.
dotnet watch 🔧 Building...
dotnet watch 🚀 Started 'D:\Program Files\dotnet\dotnet.exe' 'msbuild /t:Build /restore /nologo' with process id 208456
  Determining projects to restore...
  All projects are up-to-date for restore.
  csl -> D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0\csl.dll
dotnet watch ⌚ Process id 208456 ran for 2424ms
dotnet watch ⌚ Running MSBuild target 'GenerateWatchList' on 'D:\Users\ojesc\Desktop\csl\csl.csproj'
dotnet watch 🚀 Started 'D:\Program Files\dotnet\dotnet.exe' 'msbuild /nologo D:\Users\ojesc\Desktop\csl\csl.csproj /p:_DotNetWatchListFile=C:\Users\ojesc\AppData\Local\Temp\tmp32B5.tmp /nologo /v:n /t:GenerateWatchList /p:DotNetWatchBuild=true /p:DesignTimeBuild=true /p:CustomAfterMicrosoftCommonTargets=D:\Program Files\dotnet\sdk\7.0.100\DotnetTools\dotnet-watch\7.0.100-rtm.22521.7\tools\net7.0\any\DotNetWatch.targets /p:CustomAfterMicrosoftCommonCrossTargetingTargets=D:\Program Files\dotnet\sdk\7.0.100\DotnetTools\dotnet-watch\7.0.100-rtm.22521.7\tools\net7.0\any\DotNetWatch.targets' with process id 207412
dotnet watch ⌚ Process id 207412 ran for 1087ms
dotnet watch ⌚ Watching 2 file(s) for changes
dotnet watch ⌚ launchSettings does not allow launching browsers.
dotnet watch ⌚ Skipping configuring browser-refresh middleware since this is not a webapp.
dotnet watch 🔥 HotReloadProfile: Default.
dotnet watch ⌚ Connecting to the application.
dotnet watch 🚀 Started 'D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0\csl.exe' 'run' with process id 207928
dotnet watch ⌚ Running csl with the following arguments: run
dotnet watch 🚀 Started
Hello, World! Guzdot
Hello, World! Guzdot
Hello, World! Guzdot
Hello, World! Guzdot
Hello, World! Guzdot
dotnet watch 🔥 Hot reload capabilities: Baseline AddMethodToExistingType AddStaticFieldToExistingType AddInstanceFieldToExistingType NewTypeDefinition ChangeCustomAttributes UpdateParameters.
Hello, World! Guzdot
Hello, World! Guzdot
Hello, World! Guzdot
Hello, World! Guzdot
Hello, World! Guzdot
Hello, World! Guzdot
dotnet watch ⌚ File changed: .\Program.cs.
Hello, World! Guzdot
Hello, World! Guzdot
Hello, World! Guzdot
dotnet watch ⌚ Received successful apply from delta applier.
dotnet watch 🔥 Hot reload of changes succeeded.
dotnet watch 🔥 Hot reload change handled in 1637.4208ms.
Hello, World! Guzdot

In our large solution however, the behaviour is different

dotnet watch ⌚ Process id 129856 ran for 54759ms
dotnet watch ⌚ Running MSBuild target 'GenerateWatchList' on 'E:\Apps\LivingThing\Components\LivingThing.Community\Zoey.Server\Zoey.Server.csproj'
dotnet watch 🚀 Started 'D:\Program Files\dotnet\dotnet.exe' 'msbuild /nologo E:\Apps\LivingThing\Components\LivingThing.Community\Zoey.Server\Zoey.Server.csproj /p:_DotNetWatchListFile=C:\Users\ojesc\AppData\Local\Temp\tmp6A3A.tmp /nologo /v:n /t:GenerateWatchList /p:DotNetWatchBuild=true /p:DesignTimeBuild=true /p:CustomAfterMicrosoftCommonTargets=D:\Program Files\dotnet\sdk\7.0.100\DotnetTools\dotnet-watch\7.0.100-rtm.22521.7\tools\net7.0\any\DotNetWatch.targets /p:CustomAfterMicrosoftCommonCrossTargetingTargets=D:\Program Files\dotnet\sdk\7.0.100\DotnetTools\dotnet-watch\7.0.100-rtm.22521.7\tools\net7.0\any\DotNetWatch.targets' with process id 9416
dotnet watch ⌚ Process id 9416 ran for 29600ms
dotnet watch ⌚ Watching 368600 file(s) for changes
dotnet watch ⌚ launchSettings does not allow launching browsers.
dotnet watch ⌚ Configuring the app to use browser-refresh middleware.
dotnet watch ⌚ Refresh server running at wss://localhost:57643,ws://localhost:57644.
dotnet watch 🔥 HotReloadProfile: BlazorHosted. E:\Apps\LivingThing\Libraries\LivingThing.Frameworks\LivingThing.Web.Server.Frameworks\LivingThing.Web.Server.Frameworks.csproj references BlazorWebAssembly project E:\Apps\LivingThing\Components\LivingThing.Community\Zoey.Wasm\Zoey.Wasm.csproj.
dotnet watch ⌚ Connecting to the application.
dotnet watch 🚀 Started 'E:\Apps\LivingThing\Components\LivingThing.Community\Zoey.Server\bin\Debug\net7.0\Zoey.Server.exe' 'run' with process id 140424
dotnet watch ⌚ Connecting to the browser.
dotnet watch ⌚ Running Zoey.Server with the following arguments: run
dotnet watch 🚀 Started

Note the absence of this line

dotnet watch 🔥 Hot reload capabilities: Baseline AddMethodToExistingType AddStaticFieldToExistingType AddInstanceFieldToExistingType NewTypeDefinition ChangeCustomAttributes UpdateParameters.

When a file is changed, we get this

dotnet watch ⌚ File changed: E:\Apps\LivingThing\Libraries\LivingThing.Frameworks\LivingThing.Core.Frameworks.Client.PageBuilder.Blazor\ComponentBrowser.razor.cs.

Just notification that file changed and nothing else.

When we terminate the aplication with Ctrl+C on console, we finall have

dotnet watch 🛑 Shutdown requested. Press Ctrl+C again to force exit.
info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...
dotnet watch ⌚ Killing process 140424
dotnet watch ⌚ Failed to read Hot Reload capabilities: A task was canceled.
  ❔ Do you want to restart your app - Yes (y) / No (n) / Always (a) / Never (v)?
sake402 commented 1 year ago

Updated dotnet to 7.0.101 and still doesn't work. This time it says update applied but it wasn't

dotnet watch  -v
dotnet watch ⌚ Project supports hot reload and was configured to run with the default run-command. Watching with hot-reload
dotnet watch 🔥 Hot reload enabled. For a list of supported edits, see https://aka.ms/dotnet/hot-reload.
  💡 Press "Ctrl + R" to restart.
dotnet watch 🔧 Building...
dotnet watch 🚀 Started 'D:\Program Files\dotnet\dotnet.exe' 'msbuild /t:Build /restore /nologo' with process id 28704
  Determining projects to restore...
  All projects are up-to-date for restore.
  csl -> D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0\csl.dll
dotnet watch ⌚ Process id 28704 ran for 3477ms
dotnet watch ⌚ Running MSBuild target 'GenerateWatchList' on 'D:\Users\ojesc\Desktop\csl\csl.csproj'
dotnet watch 🚀 Started 'D:\Program Files\dotnet\dotnet.exe' 'msbuild /nologo D:\Users\ojesc\Desktop\csl\csl.csproj /p:_DotNetWatchListFile=C:\Users\ojesc\AppData\Local\Temp\tmp3B0D.tmp /nologo /v:n /t:GenerateWatchList /p:DotNetWatchBuild=true /p:DesignTimeBuild=true /p:CustomAfterMicrosoftCommonTargets=D:\Program Files\dotnet\sdk\7.0.101\DotnetTools\dotnet-watch\7.0.101-servicing.22579.2\tools\net7.0\any\DotNetWatch.targets /p:CustomAfterMicrosoftCommonCrossTargetingTargets=D:\Program Files\dotnet\sdk\7.0.101\DotnetTools\dotnet-watch\7.0.101-servicing.22579.2\tools\net7.0\any\DotNetWatch.targets' with process id 29448
dotnet watch ⌚ Process id 29448 ran for 1331ms
dotnet watch ⌚ Watching 2 file(s) for changes
dotnet watch ⌚ launchSettings does not allow launching browsers.
dotnet watch ⌚ Skipping configuring browser-refresh middleware since this is not a webapp.
dotnet watch 🔥 HotReloadProfile: Default.
dotnet watch ⌚ Connecting to the application.
dotnet watch 🚀 Started 'D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0\csl.exe' 'run' with process id 27968
dotnet watch ⌚ Running csl with the following arguments: run
dotnet watch 🚀 Started
Hello, World!
Hello, World!
Hello, World!
dotnet watch 🔥 Hot reload capabilities: Baseline AddMethodToExistingType AddStaticFieldToExistingType AddInstanceFieldToExistingType NewTypeDefinition ChangeCustomAttributes UpdateParameters.
Hello, World!
Hello, World!
dotnet watch ⌚ File changed: .\Program.cs.
Hello, World!
Hello, World!
Hello, World!
dotnet watch ⌚ Received successful apply from delta applier.
dotnet watch 🔥 Hot reload of changes succeeded.
dotnet watch 🔥 Hot reload change handled in 2605.1667ms.
Hello, World!
Hello, World!

The console message still doesn't change.

wocar commented 1 year ago

/bump same here

mfsbo commented 1 year ago

Looking for this to work as well. Never understood why Visual Studio cant work with other frameworks and allow hot reload to work with for example typescript, cshtml files for MVC views and anything Blazer. I just use VS Code to achieve some of these and some help from tsc --watch or nodejs to watch and reload part of things that I know are setup in certain way. Would be good to at least see this working for one kind of setup as example. To be honest the gif on top of this page https://learn.microsoft.com/en-us/visualstudio/debugger/hot-reload?view=vs-2022#visual-studio-2022-with-a-net-app-but-not-using-the-debugger does not make much useful content to developers than having a code repo in github that shows scenarios that you have on same page for .net 6 app and .net core scenarios.

wocar commented 1 year ago

Please try the latest SDK + workload, seems that they did some patches, however, in my case is still not working!

image

image
tmat commented 3 months ago

It says Hot reload of changes succeeded, but no change in the console message

This is expected as the changes only take effect in a new invocation of the updated code. The loop however is never invoked again, it continues running the old version. If you make the same change in the following code it will work:

while(true){
Print();
Thread.Sleep(500);
}

void Print()
{
Console.WriteLine("Hello, World! Guzdot");
}