Dotnet CLI Hot Reload: always restart app on rude edits?
Asked Answered
B

2

18

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)

Bootlace answered 12/2, 2022 at 1:16 Comment(0)
B
15

As it turns out, yes!

$ export DOTNET_WATCH_RESTART_ON_RUDE_EDIT=1
$ dotnet watch

(.NET 6.0.2+)

Bootlace answered 12/2, 2022 at 2:48 Comment(10)
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).Chil
What SDK version are you using?Bootlace
dotnet --version gives me 6.0.200Chil
Try 7.0.100-preview.1? Maybe it didn't actually get backported to 6.0.2 for some reason.Bootlace
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...Chil
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.Chil
Oh, and I confirmed same issue on preview 7, fwiw.Chil
Ok cool, so it worked with the 6.0.200 SDK, correct?Bootlace
Correct, I downgraded back to 6.0.200 after figuring that out.Chil
https://mcmap.net/q/677550/-dotnet-cli-hot-reload-always-restart-app-on-rude-edits is much simplerSlothful
S
9

It is also possible to pass the non interactive flag:

dotnet watch run --non-interactive

--non-interactive

Runs dotnet watch in non-interactive mode. Use this option to prevent console input from being requested. When hot reload is enabled and a rude edit is detected, dotnet watch restarts the app. Available since .NET 7 SDK.

Slothful answered 21/11, 2023 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.