11

When using Hot Reload in the .NET CLI...

$ dotnet watch
watch : Hot reload enabled. For a list of supported edits, see https://aka.ms/dotnet/hot-reload. Press "Ctrl + R" to restart.
watch : Building...
  ...
watch : Started

... any rude edit leads to the following prompt:

watch : Unable to apply hot reload because of a rude edit.
watch : Do you want to restart your app - Yes (y) / No (n) / Always (a) / Never (v)?

Is there any way to tell the CLI to default to "Always"? Something like dotnet watch --always-restart?

(asking because I'd like to run dotnet watch in a container)

Max
  • 9,220
  • 10
  • 51
  • 83

1 Answers1

12

As it turns out, yes!

$ export DOTNET_WATCH_RESTART_ON_RUDE_EDIT=1
$ dotnet watch

(.NET 6.0.2+)

Max
  • 9,220
  • 10
  • 51
  • 83
  • 3
    Huh, this doesn't appear to work if I `set DOTNET_WATCH_RESTART_ON_RUDE_EDIT=1` in windows. I at least have option to say always-reload, but I can't get the default behavior of my `dotnet watch` to be always-reload. Anyone know what I might be doing wrong? My machine doesn't have an `export` cmd (win11 cmder/cmd). – JohnnyFun Feb 17 '22 at 17:45
  • What SDK version are you using? – Max Feb 18 '22 at 02:28
  • `dotnet --version` gives me `6.0.200` – JohnnyFun Feb 18 '22 at 03:48
  • Try [7.0.100-preview.1](https://dotnet.microsoft.com/en-us/download/dotnet/7.0)? Maybe it didn't actually get backported to 6.0.2 for some reason. – Max Feb 19 '22 at 00:47
  • 1
    Ah, I found a clue. I was running as npm script that set the env var and then ran dotnet watch. If I do those commands directly, it works as expected. Will see if I can get it to work as one tidy little npm script somehow... – JohnnyFun Feb 19 '22 at 16:52
  • Yay, using `cross-env` npm package worked like so: `cross-env DOTNET_WATCH_RESTART_ON_RUDE_EDIT=1 dotnet watch`. Note it's important to not have `&&` between setting the var and starting dotnet watch. Seems like dotnet could maybe account for that though, since it seems webpack is able to find it if I put an && between and use normal `set` command. – JohnnyFun Feb 19 '22 at 17:17
  • Oh, and I confirmed same issue on preview 7, fwiw. – JohnnyFun Feb 19 '22 at 17:17
  • Ok cool, so it worked with the 6.0.200 SDK, correct? – Max Feb 19 '22 at 21:34
  • 1
    Correct, I downgraded back to 6.0.200 after figuring that out. – JohnnyFun Feb 20 '22 at 16:34