maloo
maloo

Reputation: 825

.NET watch exclude files does not compile

I have a .NET blazor project where I want to use dotnet watch. The blazor project is in a library, not an executable. I run watch on the executable project. In this project I try to exclude all files in wwwroot from triggering a rebuild. Include was required according to error message, so I added an empty one. Here is the msbuild part.

  <ItemGroup>
    <Watch Include="" Exclude="wwwroot\**\*" />
  </ItemGroup>

And when building the project I get this error, as if Exclude is not supported. But documentation states this is how to exclude files from dotnet watch.

> dotnet build
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
<proj>.csproj(51,23): error MSB4066: The attribute "Exclude" in element <Watch> is unrecognized.

Build FAILED.

Are there some changes in .NET 5.0 regarding Watch? Do I need to add something else in the csproj to make this work?

Upvotes: 1

Views: 1187

Answers (2)

Bryan M Lenihan
Bryan M Lenihan

Reputation: 81

On MacOS, the accepted answer:

 <ItemGroup>
    <Watch Remove="wwwroot\**\*" />
 </ItemGroup>

Does not seem to work; however, I was able to ignore the wwwroot directory using:

  <ItemGroup>
    <Content Update="wwwroot/**/*" Watch="false" />
  </ItemGroup>

Source: Opt-out of files to be watched - Microsoft

Upvotes: 2

Mr Qian
Mr Qian

Reputation: 23828

The Watch include item cannot be empty and this is an xml syntax error.

Instead, you should use this:

<ItemGroup>
    <Watch Remove="wwwroot\**\*" />
 </ItemGroup>

And then use dotnet watch build command.

By default, the Watch item will not monitor any js files, css files and will only watch .cs,.csproj,.resx files.

Note: if you want to exclude .cs, .resx files, you cannot use Remove and you should add Watch="false" for file to exclude them. Check this tip.

enter image description here

And if you have extended the Watch Range by your need, basically, add your other custom files, you should pay attention to the below:

===============================================================

In fact, since the include cannot be empty and exclude should work with include at the same time. If you already include the files before, and then use include with exclude to remove some files, it is wrong because you have include some files twice. The exclude node acts on the current include node. And if the above xml node still have the same item include, the file always exists under that item.

Either, when you use a wide range of files for watch include, exclude some files that do not need to be monitored at the same time.

Like: <Watch Include="**\*.js;**\*.css" Exclude="wwwroot\**\*"> to remove the js and css files under wwwroot folder. When you first extend the files into Watch, you should remove the related files based on your need at the same time.

And I think you want to exclude some js files(not the default watch files) and other non-default monitored file, in this case, the best way is to use Remove.

In other word, Remove and Include works for other extended non-default files while Watch="false" works for default watched files.

Upvotes: 1

Related Questions