.\" Automatically generated by Pandoc 2.18 .\" .\" Define V font for inline verbatim, using C font in formats .\" that render this, and otherwise B font. .ie "\f[CB]x\f[]"x" \{\ . ftr V B . ftr VI BI . ftr VB B . ftr VBI BI .\} .el \{\ . ftr V CR . ftr VI CI . ftr VB CB . ftr VBI CBI .\} .TH "dotnet-watch" "1" "2022-11-10" "" ".NET Documentation" .hy .SH dotnet watch .PP \f[B]This article applies to:\f[R] \[u2714]\[uFE0F] .NET Core 3.1 SDK and later versions .SH NAME .PP dotnet-watch - Restarts or hot reloads the specified application when changes in the source code are detected. .SH SYNOPSIS .IP .nf \f[C] dotnet watch [--list] [--no-hot-reload] [--non-interactive] [--project ] [-q|--quiet] [-v|--verbose] [--version] [--] dotnet watch -?|-h|--help \f[R] .fi .SH DESCRIPTION .PP The \f[V]dotnet watch\f[R] command is a file watcher. When it detects a change that is supported for hot reload, it hot reloads the specified application. When it detects an unsupported change, it restarts the application. This process enables fast iterative development from the command line. .PP While running \f[V]dotnet watch\f[R], you can force the app to rebuild and restart by pressing Ctrl+R in the command shell. This feature is available only while the app is running. For example, if you run \f[V]dotnet watch\f[R] on a console app that ends before you press Ctrl+R, pressing Ctrl+R has no effect. However, in that case \f[V]dotnet watch\f[R] is still watching files and will restart the app if a file is updated. .SS Response compression .PP If \f[V]dotnet watch\f[R] runs for an app that uses response compression, the tool can\[cq]t inject the browser refresh script. The .NET 7 and later version of the tool displays a warning message like the following: .RS .PP warn: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[4] .PP Unable to configure browser refresh script injection on the response. This may have been caused by the response\[cq]s Content-Encoding: `br'. Consider disabling response compression. .RE .PP As an alternative to disabling response compression, manually add the browser refresh JavaScript reference to the app\[cq]s pages: .IP .nf \f[C] \[at]if (Environment.GetEnvironmentVariable(\[dq]__ASPNETCORE_BROWSER_TOOLS\[dq]) is not null) { } \f[R] .fi .SH ARGUMENTS .IP \[bu] 2 \f[B]\f[VB]forwarded arguments\f[B]\f[R] .RS 2 .PP Arguments to pass to the child \f[V]dotnet\f[R] process. For example: \f[V]run\f[R] with options for dotnet run or \f[V]test\f[R] with options for dotnet test. If the child command isn\[cq]t specified, the default is \f[V]run\f[R] for \f[V]dotnet run\f[R]. .RE .SH OPTIONS .IP \[bu] 2 \f[B]\f[VB]--list\f[B]\f[R] .RS 2 .PP Lists all discovered files without starting the watcher. .RE .IP \[bu] 2 \f[B]\f[VB]--no-hot-reload\f[B]\f[R] .RS 2 .PP Suppress hot reload for supported apps. .RE .IP \[bu] 2 \f[B]\f[VB]--non-interactive\f[B]\f[R] .RS 2 .PP Runs \f[V]dotnet watch\f[R] 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. .RE .IP \[bu] 2 \f[B]\f[VB]--project \f[B]\f[R] .RS 2 .PP Specifies the path of the project file to run (folder only or including the project file name). If not specified, it defaults to the current directory. .RE .IP \[bu] 2 \f[B]\f[VB]-q|--quiet\f[B]\f[R] .RS 2 .PP Suppresses all output that is generated by the \f[V]dotnet watch\f[R] command except warnings and errors. The option is not passed on to child commands. For example, output from \f[V]dotnet restore\f[R] and \f[V]dotnet run\f[R] continues to be output. .RE .IP \[bu] 2 \f[B]\f[VB]-v|--verbose\f[B]\f[R] .RS 2 .PP Shows verbose output for debugging. .RE .IP \[bu] 2 \f[B]\f[VB]--version\f[B]\f[R] .RS 2 .PP Shows the version of \f[V]dotnet watch\f[R]. .RE .IP \[bu] 2 \f[B]\f[VB]--\f[B]\f[R] .RS 2 .PP The double-dash option (`\[en]') can be used to delimit \f[V]dotnet watch\f[R] options from arguments that will be passed to the child process. Its use is optional. When the double-dash option isn\[cq]t used, \f[V]dotnet watch\f[R] considers the first unrecognized argument to be the beginning of arguments that it should pass into the child \f[V]dotnet\f[R] process. .RE .SH ENVIRONMENT VARIABLES .PP \f[V]dotnet watch\f[R] uses the following environment variables: .IP \[bu] 2 \f[B]\f[VB]DOTNET_HOTRELOAD_NAMEDPIPE_NAME\f[B]\f[R] .RS 2 .PP This value is configured by \f[V]dotnet watch\f[R] when the app is to be launched, and it specifies the named pipe. .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_USE_POLLING_FILE_WATCHER\f[B]\f[R] .RS 2 .PP When set to \f[V]1\f[R] or \f[V]true\f[R], \f[V]dotnet watch\f[R] uses a polling file watcher instead of . Polling is required for some file systems, such as network shares, Docker mounted volumes, and other virtual file systems. The class uses \f[V]DOTNET_USE_POLLING_FILE_WATCHER\f[R] to determine whether the method will rely on the . .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_WATCH\f[B]\f[R] .RS 2 .PP \f[V]dotnet watch\f[R] sets this variable to \f[V]1\f[R] on all child processes that it launches. .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_WATCH_AUTO_RELOAD_WS_HOSTNAME\f[B]\f[R] .RS 2 .PP As part of \f[V]dotnet watch\f[R], the browser refresh server mechanism reads this value to determine the WebSocket host environment. The value \f[V]127.0.0.1\f[R] is replaced by \f[V]localhost\f[R], and the \f[V]http://\f[R] and \f[V]https://\f[R] schemes are replaced with \f[V]ws://\f[R] and \f[V]wss://\f[R] respectively. .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_WATCH_ITERATION\f[B]\f[R] .RS 2 .PP \f[V]dotnet watch\f[R] sets this variable to \f[V]1\f[R] and increments by one each time a file is changed and the command restarts or hot reloads the application. .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_WATCH_SUPPRESS_BROWSER_REFRESH\f[B]\f[R] .RS 2 .PP When set to \f[V]1\f[R] or \f[V]true\f[R], \f[V]dotnet watch\f[R] won\[cq]t refresh browsers when it detects file changes. .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_WATCH_SUPPRESS_EMOJIS\f[B]\f[R] .RS 2 .PP With the .NET SDK 6.0.300 and later, \f[V]dotnet watch\f[R] emits non-ASCII characters to the console, as shown in the following example: .IP .nf \f[C] dotnet watch \[u1F525] Hot reload enabled. For a list of supported edits, see https://aka.ms/dotnet/hot-reload. \[u1F4A1] Press \[dq]Ctrl + R\[dq] to restart. dotnet watch \[u1F527] Building... dotnet watch \[u1F680] Started dotnet watch \[u231A] Exited dotnet watch \[u23F3] Waiting for a file to change before restarting dotnet... \f[R] .fi .PP On certain console hosts, these characters may appear garbled. To avoid seeing garbled characters, set this variable to \f[V]1\f[R] or \f[V]true\f[R]. .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_WATCH_SUPPRESS_LAUNCH_BROWSER\f[B]\f[R] .RS 2 .PP When set to \f[V]1\f[R] or \f[V]true\f[R], \f[V]dotnet watch\f[R] won\[cq]t launch or refresh browsers for web apps that have \f[V]launchBrowser\f[R] configured in \f[I]launchSettings.json\f[R]. .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_WATCH_SUPPRESS_MSBUILD_INCREMENTALISM\f[B]\f[R] .RS 2 .PP By default, \f[V]dotnet watch\f[R] optimizes the build by avoiding certain operations, such as running restore or re-evaluating the set of watched files on every file change. If this variable is set to \f[V]1\f[R] or \f[V]true\f[R], these optimizations are disabled. .RE .IP \[bu] 2 \f[B]\f[VB]DOTNET_WATCH_SUPPRESS_STATIC_FILE_HANDLING\f[B]\f[R] .RS 2 .PP When set to \f[V]1\f[R] or \f[V]true\f[R], \f[V]dotnet watch\f[R] won\[cq]t do special handling for static content files. \f[V]dotnet watch\f[R] sets MSBuild property \f[V]DotNetWatchContentFiles\f[R] to \f[V]false\f[R]. .RE .SS Files watched by default .PP \f[V]dotnet watch\f[R] watches all items in the \f[V]Watch\f[R] item group in the project file. By default, this group includes all items in the \f[V]Compile\f[R] and \f[V]EmbeddedResource\f[R] groups. \f[V]dotnet watch\f[R] also scans the entire graph of project references and watches all files within those projects. .PP By default, the \f[V]Compile\f[R] and \f[V]EmbeddedResource\f[R] groups include all files matching the following glob patterns: .IP \[bu] 2 \f[V]**/*.cs\f[R] .IP \[bu] 2 \f[V]*.csproj\f[R] .IP \[bu] 2 \f[V]**/*.resx\f[R] .IP \[bu] 2 Content files in web apps: \f[V]wwwroot/**\f[R] .PP By default, \f[I].config\f[R], and \f[I].json\f[R] files don\[cq]t trigger a dotnet watch restart because the configuration system has its own mechanisms for handling configuration changes. .PP Files can be added to the watch list or removed from the list by editing the project file. Files can be specified individually or by using glob patterns. .SS Watch additional files .PP More files can be watched by adding items to the \f[V]Watch\f[R] group. For example, the following markup extends that group to include JavaScript files: .IP .nf \f[C] \f[R] .fi .SS Ignore specified files .PP \f[V]dotnet watch\f[R] will ignore \f[V]Compile\f[R] and \f[V]EmbeddedResource\f[R] items that have the \f[V]Watch=\[dq]false\[dq]\f[R] attribute, as shown in the following example: .IP .nf \f[C] \f[R] .fi .PP \f[V]dotnet watch\f[R] will ignore project references that have the \f[V]Watch=\[dq]false\[dq]\f[R] attribute, as shown in the following example: .IP .nf \f[C] \f[R] .fi .SS Advanced configuration .PP \f[V]dotnet watch\f[R] performs a design-time build to find items to watch. When this build is run, \f[V]dotnet watch\f[R] sets the property \f[V]DotNetWatchBuild=true\f[R]. This property can be used as shown in the following example: .IP .nf \f[C] \f[R] .fi .SS Hot Reload .PP Starting in .NET 6, \f[V]dotnet watch\f[R] includes support for \f[I]hot reload\f[R]. Hot reload is a feature that lets you apply changes to a running app without having to rebuild and restart it. The changes may be to code files or static assets, such as stylesheet files and JavaScript files. This feature streamlines the local development experience, as it gives immediate feedback when you modify your app. .PP For information about app types and .NET versions that support hot reload, see Supported .NET app frameworks and scenarios. .SS Rude edits .PP When a file is modified, \f[V]dotnet watch\f[R] determines if the app can be hot reloaded. If it can\[cq]t be hot reloaded, the change is called a \f[I]rude edit\f[R] and \f[V]dotnet watch\f[R] asks if you want to restart the app: .IP .nf \f[C] dotnet watch \[u231A] Unable to apply hot reload because of a rude edit. \[u2754] Do you want to restart your app - Yes (y) / No (n) / Always (a) / Never (v)? \f[R] .fi .IP \[bu] 2 \f[B]Yes\f[R]: Restarts the app. .IP \[bu] 2 \f[B]No\f[R]: Leaves the app running without the changes applied. .IP \[bu] 2 \f[B]Always\f[R]: Restarts the app and doesn\[cq]t prompt anymore for rude edits. .IP \[bu] 2 \f[B]Never\f[R]: Leaves the app running without the changes applied and doesn\[cq]t prompt anymore for rude edits. .PP For information about what kinds of changes are considered rude edits, see Edit code and continue debugging and Unsupported changes to code. .PP To disable hot reload when you run \f[V]dotnet watch\f[R], use the \f[V]--no-hot-reload\f[R] option, as shown in the following example: .PP \f[V].NET CLI dotnet watch --no-hot-reload\f[R] .SH EXAMPLES .IP \[bu] 2 Run \f[V]dotnet run\f[R] for the project in the current directory whenever source code changes: .RS 2 .IP .nf \f[C] dotnet watch \f[R] .fi .PP Or: .IP .nf \f[C] dotnet watch run \f[R] .fi .RE .IP \[bu] 2 Run \f[V]dotnet test\f[R] for the project in the current directory whenever source code changes: .RS 2 .IP .nf \f[C] dotnet watch test \f[R] .fi .RE .IP \[bu] 2 Run \f[V]dotnet run --project ./HelloWorld.csproj\f[R] whenever source code changes: .RS 2 .IP .nf \f[C] dotnet watch run --project ./HelloWorld.csproj \f[R] .fi .RE .IP \[bu] 2 Run \f[V]dotnet run -- arg0\f[R] for the project in the current directory whenever source code changes: .RS 2 .IP .nf \f[C] dotnet watch run -- arg0 \f[R] .fi .PP Or: .IP .nf \f[C] dotnet watch -- run arg0 \f[R] .fi .RE .SH SEE ALSO .IP \[bu] 2 Tutorial: Develop ASP.NET Core apps using a file watcher .IP \[bu] 2 Hot reload in Visual Studio .IP \[bu] 2 Hot reload supported apps .IP \[bu] 2 Hot reload supported code changes .IP \[bu] 2 Hot reload test execution .IP \[bu] 2 Hot reload support for ASP.NET Core