1

Dotnet watch / hot reload doesn't seem to work at all. Any ideas? Or am I just doing something wrong?

Program.cs:

while(true)
{
    Thread.Sleep(1000);
    Console.WriteLine("a");
}

Then on the command line:

dotnet watch
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...
  Determining projects to restore...
  Restored xxx.csproj (in 91 ms).
  test -> xxx/bin/Debug/net7.0/xxx.dll
dotnet watch  Started
a
a
a
(etc)

As expected. Then change "a" to "b" in the above and get this output:

dotnet watch ⌚ File changed: ./Program.cs.
a
dotnet watch  Hot reload of changes succeeded.
a
a
a
(etc)

Notice it's still outputting "a" not "b".

I'm running macOS and from the standard command line. The csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

TIA.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Brian Birtle
  • 627
  • 7
  • 15

1 Answers1

3

Extract the changed code into a separate function:

while(true)
{
    Thread.Sleep(1000);
    F();
}

void F()
{
    Console.WriteLine("b");
}

As stated in the docs:

The following changes can't be applied to C# and Visual Basic code during a debugging session:

  • Changes to the current statement or any other active statement.
    Active statements include any statements, in functions on the call stack, that were called to get to the current statement.
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    Thanks, you're right! I had created this simplified example for purposes of posting. In my real world case, I'm making edits in a different (and supported way). I'll go back and try to figure out a better example - it's more trying to get it working in Visual Studio Code and with debugging enabled... – Brian Birtle Mar 08 '23 at 06:55
  • 1
    By the way, do you know how I can get more information from dotnet watch of why edits trigger a restart? While the code edits you suggested in my toy app do work, in my real app, every change I make prompts me to restart and recompile. My app is hundreds of thousands of lines of code so the compile and restart process are taking several MINUTES and making development almost impossible. TIA! – Brian Birtle Apr 01 '23 at 01:40
  • 1
    @BrianBirtle TBH I have not used `dotnet watch` that much. _"every change I make prompts me to restart and recompile"_ - you mean that you need to manually do that? – Guru Stron Apr 01 '23 at 18:46
  • I created a new question with more details - https://stackoverflow.com/questions/75905227/dotnet-watch-not-working-visual-studio-code-on-macos - basically, it does seem to work (after your suggestion!) in the simple example. But not in my real app. And I have no idea why or even how to start figuring out why... – Brian Birtle Apr 02 '23 at 01:56