When does dotnet watch hot reload a running .NET 10 app, and when does it rebuild and restart the app instead?
answer
- a file watcher around run
- rude edit: Yes / No / Always / Never
- --non-interactive restarts on rude edits
- --no-hot-reload, Ctrl+R
- run, build or test only since .NET 8 SDK
basics
~20 sdotnet watch reruns dotnet run on every saved change and applies supported edits to the live process with hot reload; an edit it cannot apply (a rude edit) prompts for a restart, and --no-hot-reload or Ctrl+R forces a full restart.
solid answer
~40 s`dotnet watch` is a file watcher that runs `dotnet run` by default (or `build` or `test`). When a change can be applied to the running process, it hot reloads: method bodies, Razor markup, CSS and JavaScript update without a restart, and web apps get a browser refresh. A change the runtime cannot apply in place is a **rude edit**; interactively you answer Yes, No, Always or Never to a restart prompt, while `--non-interactive` or `DOTNET_WATCH_RESTART_ON_RUDE_EDIT=1` restart without asking. `--no-hot-reload` turns every change into rebuild and restart, and Ctrl+R forces one. Hot reload changes code but does not re-run what already executed, so edits to startup code such as service registrations need a restart.
code
bash · 8 lines# Inner loop for the job-queue tool; app arguments after --
dotnet watch --project src/PressCtl -- --printer lab-1
# Rerun tests on every save
dotnet watch test --project tests/PressCtl.Tests
# In a container: restart on rude edits without prompting
dotnet watch --non-interactive --project src/PressCtlgo deeper
Recall that dotnet watch reruns the app when files change and that hot reload applies many edits without a restart.
Explain rude edits and the four prompt answers, --no-hot-reload, --non-interactive and Ctrl+R, and that watch drives only run, build or test.
Show you know where hot reload misleads: code that already ran at startup keeps old behaviour, and in-memory state after a partial update can hide bugs that a clean restart reveals.
Treat the watcher as a developer-experience investment: decide when a team should default to --no-hot-reload for stateful apps, and keep CI independent of it.
## What dotnet watch is `dotnet watch` wraps another `dotnet` command and repeats it whenever a watched file changes. With no child command it runs `dotnet run`; since the .NET 8 SDK the child command can only be `run`, `build` or `test` (`dotnet watch test` reruns the tests on every save). `dotnet watch --list` prints the files it watches, which is the first check when a change seems ignored. ## Hot reload vs restart When the child is `run`, a change goes down one of two paths: 1. **Hot reload**: the SDK compiles the edit and applies it to the running process. Method bodies, Razor and Blazor markup, and static assets such as CSS and JavaScript update in place. For web apps, the browser refresh script reloads the page. 2. **Restart**: the process is stopped, rebuilt and started again. State held in memory is lost. An edit that cannot be applied to code already loaded is a **rude edit**. In an interactive terminal `dotnet watch` asks: | Answer | Effect | |---|---| | Yes (y) | restart now | | No (n) | keep the app running without the change | | Always (a) | restart now and on every later rude edit | | Never (v) | never restart for rude edits in this session | ## The switches - `--no-hot-reload`: skip hot reload entirely; every change triggers a rebuild and restart. Useful when an app holds state that becomes inconsistent after a partial update. - `--non-interactive`: never read console input. With hot reload on, a rude edit restarts the app automatically; this is what you want in a container or a script. - `DOTNET_WATCH_RESTART_ON_RUDE_EDIT=1` (or `true`): the same automatic restart without giving up interactive input. - **Ctrl+R** forces a rebuild and restart at any time while the app is running. - `DOTNET_WATCH_SUPPRESS_LAUNCH_BROWSER` and `DOTNET_WATCH_SUPPRESS_BROWSER_REFRESH` turn off browser launching and refreshing. The watched process also sees `DOTNET_WATCH=1` and an incrementing `DOTNET_WATCH_ITERATION`, so code can tell it runs under the watcher. ## The trap: code that already ran Hot reload replaces code; it does not rewind execution. Top-level statements in `Program.cs` that registered services, read configuration or mapped endpoints already ran at startup. Editing them is applied to the method, but nothing calls it again, so the app keeps the old registrations. The fix is a restart (Ctrl+R), or `--no-hot-reload` while you are working on startup code. A related web-app trap: response compression can stop the watcher injecting its browser-refresh script, and it logs a warning saying so. ## The print shop's loop For the print shop's job-queue tool, a developer runs `dotnet watch --project src/PressCtl -- --printer lab-1`: arguments after `--` go to the app. Editing the page-splitting logic hot reloads; changing how printers are registered at startup needs Ctrl+R. The CI job never uses `dotnet watch`; it is an inner-loop tool, and `dotnet watch test` is the test-side equivalent. ## When to turn hot reload off - The app caches configuration or builds object graphs at startup, so a hot-reloaded method keeps working against stale data and tempts you to debug a state that production never has. - You are changing startup code, middleware order or dependency registrations, where every useful edit needs a restart anyway. - A test run under `dotnet watch test` should start from a clean process each time. In those cases `--no-hot-reload` trades a few seconds per change for a process whose state always matches the code on disk.
- Why can dotnet watch no longer run dotnet publish or a global tool?Since the .NET 8 SDK the watcher only drives `run`, `build` and `test`. Earlier versions could forward any command dispatched by `dotnet`; that generality was dropped, so publishing or running a tool on change needs another mechanism.
- How can code detect that it is running under dotnet watch?The watcher sets `DOTNET_WATCH` to `1` in the child process and increments `DOTNET_WATCH_ITERATION` on every restart or hot reload, so the app can read either variable.
Hot reload is like a theatre swapping a line in the script mid-performance: the next time an actor reaches that line they say the new words, but scenes already played are not performed again, so changing the opening scene means restarting the show.
- dotnet watch hot reloads every kind of change, so restarts are never needed.
- --non-interactive turns hot reload off.
- dotnet watch can wrap any CLI command, such as dotnet publish.
- Editing a service registration in Program.cs takes effect through hot reload.
- Answering No to the rude-edit prompt applies the change on the next request.