Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions docs/core/tools/dotnet-environment-variables.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: .NET environment variables
description: Learn about the environment variables that you can use to configure the .NET SDK, .NET CLI, and .NET runtime.
ms.date: 01/31/2022
ms.date: 05/11/2022
---

# .NET environment variables
Expand Down Expand Up @@ -297,26 +297,6 @@ The typical way to get detailed trace information about application startup is t

If set to `true`, invoking `dotnet` won't produce a warning when a preview SDK is being used.

### `DOTNET_WATCH_*`

The following .NET watch settings are available as environment variables:

- `DOTNET_WATCH`: The `dotnet watch` command sets this variable to `1` on all child processes launched.
- `DOTNET_WATCH_ITERATION`: The `dotnet watch` command sets this variable to `1` and increments by one each time
a file is changed and the command is restarted.
- `DOTNET_WATCH_SUPPRESS_STATIC_FILE_HANDLING`: If set to `1`, or `true`, `dotnet watch` will _not_ perform special handling for static content file.
- `DOTNET_WATCH_SUPPRESS_MSBUILD_INCREMENTALISM`: By default, `dotnet watch` optimizes the build by avoiding certain operations such as running `restore` or re-evaluating the set of watched files on every file change. If set to `1` or `true`, these optimizations are disabled.
- `DOTNET_WATCH_SUPPRESS_LAUNCH_BROWSER`: The `dotnet watch run` command will attempt to launch browsers for web apps with `launchBrowser` configured in the _launchSettings.json_ file. If set to `1` or `true`, this behavior is suppressed.
- `DOTNET_WATCH_SUPPRESS_BROWSER_REFRESH`
- `DOTNET_WATCH_AUTO_RELOAD_WS_HOSTNAME`: As part of `dotnet watch`, the browser refresh server mechanism reads this value to determine the WebSocket host environment. The value `127.0.0.1` is replaced by `localhost`, and the `http://` and `https://` schemes are replaced with `ws://` and `wss://` respectively.
- `DOTNET_HOTRELOAD_NAMEDPIPE_NAME`: This value is configured by `dotnet watch` when the app is to be launched, and it specifies the named pipe.

For more information, see [GitHub: .NET SDK dotnet-watch](https://github.com/dotnet/sdk/blob/main/src/BuiltInTools/dotnet-watch/README.md).

#### `DOTNET_USE_POLLING_FILE_WATCHER`

When set to `1` or `true`, `dotnet watch` will poll the file system for changes. This is required for some file systems, such as network shares, Docker mounted volumes, and other virtual file systems. The <xref:Microsoft.Extensions.FileProviders.PhysicalFileProvider> class uses `DOTNET_USE_POLLING_FILE_WATCHER` to determine whether the <xref:Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch%2A?displayProperty=nameWithType> method will rely on the <xref:Microsoft.Extensions.FileProviders.Physical.PollingFileChangeToken>.

### Configure MSBuild in the .NET CLI

To execute MSBuild out-of-process, set the `DOTNET_CLI_RUN_MSBUILD_OUTOFPROC` environment variable to either `1`, `true`, or `yes`. By default, MSBuild will execute in-proc. To force MSBuild to use an external working node long-living process for building projects, set `DOTNET_CLI_USE_MSBUILDNOINPROCNODE` to `1`, `true`, or `yes`. This will set the `MSBUILDNOINPROCNODE` environment variable to `1`, which is referred to as _MSBuild Server V1_, as the entry process forwards most of the work to it.
Expand All @@ -333,6 +313,10 @@ These are overrides that are used to force the resolved SDK tasks and targets to

Configures the default programming language for the `dotnet new` command when the `-lang|--language` switch is omitted. The default value is `C#`. Valid values are `C#`, `F#`, or `VB`. For more information, see [dotnet new](dotnet-new.md).

### `dotnet watch` environment variables

For information about `dotnet watch` settings that are available as environment variables, see [dotnet watch environment variables](dotnet-watch.md#environment-variables).

## See also

- [dotnet command](dotnet.md)
Expand Down
257 changes: 257 additions & 0 deletions docs/core/tools/dotnet-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
---
title: dotnet watch command
description: The dotnet watch command is a file watcher that restarts or hot reloads the specified application when changes in the source code are detected.
ms.date: 05/11/2022
---
# dotnet watch

**This article applies to:** ✔️ .NET Core 3.1 SDK and later versions

## Name

`dotnet watch` - Restarts or [hot reloads](#hot-reload) the specified application when changes in the source code are detected.

## Synopsis

```dotnetcli
dotnet watch [--list]
[--no-hot-reload] [--non-interactive]
[--project <PROJECT>]
[-q|--quiet] [-v|--verbose]
[--version]
[--] <forwarded arguments>

dotnet watch -?|-h|--help
```

## Description

The `dotnet watch` command is a file watcher. When it detects a change that is supported for [hot reload](#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.

While running `dotnet watch`, 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 `dotnet watch` on a console app that ends before you press Ctrl+R, pressing Ctrl+R has no effect. However, in that case `dotnet watch` is still watching files and will restart the app if a file is updated.

## Arguments

<!-- markdownlint-disable MD012 -->

- **`forwarded arguments`**

Arguments to pass to the child `dotnet` process. For example: `run` with options for [dotnet run](dotnet-run.md) or `test` with options for [dotnet test](dotnet-test.md). If the child command isn't specified, the default is `run` for `dotnet run`.

## Options

- **`--list`**

Lists all discovered files without starting the watcher.

- **`--no-hot-reload`**

Suppress [hot reload](#hot-reload) for [supported apps](/visualstudio/debugger/hot-reload#supported-net-app-frameworks-and-scenarios).

- **`--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](#rude-edits) is detected, dotnet watch restarts the app.

- **`--project <PATH>`**

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.

- **`-q|--quiet`**

Suppresses all output that is generated by the `dotnet watch` command except warnings and errors. The option is not passed on to child commands. For example, output from `dotnet restore` and `dotnet run` continues to be output.

- **`-v|--verbose`**

Shows verbose output for debugging.

- **`--version`**

Shows the version of `dotnet watch`.

- **`--`**

The [double-dash option ('--')](../../standard/commandline/syntax.md#the----token) can be used to delimit `dotnet watch` options from arguments that will be passed to the child process. Its use is optional. When the double-dash option isn't used, `dotnet watch` considers the first unrecognized argument to be the beginning of arguments that it should pass into the child `dotnet` process.

## Environment variables

`dotnet watch` uses the following environment variables:

- **`DOTNET_HOTRELOAD_NAMEDPIPE_NAME`**

This value is configured by `dotnet watch` when the app is to be launched, and it specifies the named pipe.

- **`DOTNET_USE_POLLING_FILE_WATCHER`**

When set to `1` or `true`, `dotnet watch` uses a polling file watcher instead of <xref:System.IO.FileSystemWatcher?displayProperty=nameWithType>. Polling is required for some file systems, such as network shares, Docker mounted volumes, and other virtual file systems. The <xref:Microsoft.Extensions.FileProviders.PhysicalFileProvider> class uses `DOTNET_USE_POLLING_FILE_WATCHER` to determine whether the <xref:Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch%2A?displayProperty=nameWithType> method will rely on the <xref:Microsoft.Extensions.FileProviders.Physical.PollingFileChangeToken>.

- **`DOTNET_WATCH`**

`dotnet watch` sets this variable to `1` on all child processes that it launches.

- **`DOTNET_WATCH_AUTO_RELOAD_WS_HOSTNAME`**

As part of `dotnet watch`, the browser refresh server mechanism reads this value to determine the WebSocket host environment. The value `127.0.0.1` is replaced by `localhost`, and the `http://` and `https://` schemes are replaced with `ws://` and `wss://` respectively.

- **`DOTNET_WATCH_ITERATION`**

`dotnet watch` sets this variable to `1` and increments by one each time a file is changed and the command restarts or hot reloads the application.

- **`DOTNET_WATCH_SUPPRESS_BROWSER_REFRESH`**

When set to `1` or `true`, `dotnet watch` won't refresh browsers when it detects file changes.

- **`DOTNET_WATCH_SUPPRESS_EMOJIS`**

With the .NET SDK 6.0.300 and later, `dotnet watch` emits non-ASCII characters to the console, as shown in the following example:

```output
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...
dotnet watch 🚀 Started
dotnet watch ⌚ Exited
dotnet watch ⏳ Waiting for a file to change before restarting dotnet...
```

On certain console hosts, these characters may appear garbled. To avoid seeing garbled characters, set this variable to `1` or `true`.

- **`DOTNET_WATCH_SUPPRESS_LAUNCH_BROWSER`**

When set to `1` or `true`, `dotnet watch` won't launch or refresh browsers for web apps that have `launchBrowser` configured in *launchSettings.json*.

- **`DOTNET_WATCH_SUPPRESS_MSBUILD_INCREMENTALISM`**

By default, `dotnet watch` 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 `1` or `true`, these optimizations are disabled.

- **`DOTNET_WATCH_SUPPRESS_STATIC_FILE_HANDLING`**

When set to `1` or `true`, `dotnet watch` won't do special handling for static content files. `dotnet watch` sets MSBuild property `DotNetWatchContentFiles` to `false`.

## Files watched by default

`dotnet watch` watches all items in the `Watch` item group in the project file. By default, this group includes all items in the `Compile` and `EmbeddedResource` groups. `dotnet watch` also scans the entire graph of project references and watches all files within those projects.

By default, the `Compile` and `EmbeddedResource` groups include all files matching the following glob patterns:

* `**/*.cs`
* `*.csproj`
* `**/*.resx`
* Content files in web apps: `wwwroot/**`

By default, *.config*, and *.json* files don't trigger a dotnet watch restart because the configuration system has its own mechanisms for handling configuration changes.

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.

## Watch additional files

More files can be watched by adding items to the `Watch` group. For example, the following markup extends that group to include JavaScript files:

```xml
<ItemGroup>
<Watch Include="**\*.js" Exclude="node_modules\**\*;**\*.js.map;obj\**\*;bin\**\*" />
</ItemGroup>
```

## Ignore specified files

`dotnet watch` will ignore `Compile` and `EmbeddedResource` items that have the `Watch="false"` attribute, as shown in the following example:

```xml
<ItemGroup>
<Compile Update="Generated.cs" Watch="false" />
<EmbeddedResource Update="Strings.resx" Watch="false" />
</ItemGroup>
```

`dotnet watch` will ignore project references that have the `Watch="false"` attribute, as shown in the following example:

```xml
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" Watch="false" />
</ItemGroup>
```

## Advanced configuration

`dotnet watch` performs a design-time build to find items to watch. When this build is run, `dotnet watch` sets the property `DotNetWatchBuild=true`. This property can be used as shown in the following example:

```xml
<ItemGroup Condition="'$(DotNetWatchBuild)'=='true'">
<!-- only included in the project when dotnet-watch is running -->
</ItemGroup>
```

## Hot Reload

Starting in .NET 6, `dotnet watch` includes support for *hot reload*. 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.

For information about app types and .NET versions that support hot reload, see [Supported .NET app frameworks and scenarios](/visualstudio/debugger/hot-reload#supported-net-app-frameworks-and-scenarios).

### Rude edits

When a file is modified, `dotnet watch` determines if the app can be hot reloaded. If it can't be hot reloaded, the change is called a *rude edit* and `dotnet watch` asks if you want to restart the app:

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

* **Yes**: Restarts the app.
* **No**: Leaves the app running without the changes applied.
* **Always**: Restarts the app and doesn't prompt anymore for rude edits.
* **Never**: Leaves the app running without the changes applied and doesn't prompt anymore for rude edits.

For information about what kinds of changes are considered rude edits, see [Edit code and continue debugging](/visualstudio/debugger/edit-and-continue) and [Unsupported changes to code](/visualstudio/debugger/supported-code-changes-csharp#unsupported-changes-to-code).

To disable hot reload when you run `dotnet watch`, use the `--no-hot-reload` option, as shown in the following example:

```.NET CLI
dotnet watch --no-hot-reload
```

## Examples

- Run `dotnet run` for the project in the current directory whenever source code changes:

```dotnetcli
dotnet watch
```

Or:

```dotnetcli
dotnet watch run
```

- Run `dotnet test` for the project in the current directory whenever source code changes:

```dotnetcli
dotnet watch test
```

- Run `dotnet run --project ./HelloWorld.csproj` whenever source code changes:

```dotnetcli
dotnet watch run --project ./HelloWorld.csproj
```

- Run `dotnet run -- arg0` for the project in the current directory whenever source code changes:

```dotnetcli
dotnet watch run -- arg0
```

Or:

```dotnetcli
dotnet watch -- run arg0
```

## See also

* [Tutorial: Develop ASP.NET Core apps using a file watcher](/aspnet/core/tutorials/dotnet-watch)
* [Hot reload in Visual Studio](/visualstudio/debugger/hot-reload)
* [Hot reload supported apps](/visualstudio/debugger/hot-reload#supported-net-app-frameworks-and-scenarios)
* [Hot reload supported code changes](/visualstudio/debugger/supported-code-changes-csharp)
* [Hot reload test execution](/visualstudio/test/test-execution-with-hot-reload)
* [Hot reload support for ASP.NET Core](/aspnet/core/test/hot-reload)
4 changes: 2 additions & 2 deletions docs/core/tools/dotnet.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: dotnet command
description: Learn about the dotnet command (the generic driver for the .NET CLI) and its usage.
ms.date: 07/19/2021
ms.date: 05/06/2022
---
# dotnet command

Expand Down Expand Up @@ -240,7 +240,7 @@ Starting with .NET Core SDK 2.1.300, a number of tools that were available only
| dev-certs | Creates and manages development certificates. |
| [ef](/ef/core/miscellaneous/cli/dotnet) | Entity Framework Core command-line tools. |
| [user-secrets](/aspnet/core/security/app-secrets) | Manages development user secrets. |
| [watch](/aspnet/core/tutorials/dotnet-watch) | Starts a file watcher that runs a command when files change. |
| [watch](dotnet-watch.md) | A file watcher that restarts or hot reloads an application when it detects changes in the source code. |

For more information about each tool, type `dotnet <tool-name> --help`.

Expand Down
3 changes: 3 additions & 0 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ items:
href: ../core/tools/dotnet-tool-update.md
- name: dotnet vstest
href: ../core/tools/dotnet-vstest.md
- name: dotnet watch
displayName: hot reload
href: ../core/tools/dotnet-watch.md
- name: dotnet workload
items:
- name: dotnet workload install
Expand Down