How to exclude certain file types from dotnet watch run?
Asked Answered
S

4

8

Looking to get the following setup going:

I'm working on a Blazor app, and with the official css isolation bundler. I'm using Less though, and installed a Less transformer which creates the required css on build.

However, running my app via dotnet watch run, it often ends up in an endless loop now.

The reason for this is probably that dotnet watch run sees a change in the *.razor.css files, rebuilds, and the cycle just repeats on and on.

So here's my question:

How can I configure my csproj (new Net Standard format) to exclude **\*.razor.css from the watch process? It would also be fine it it just disappears from my VS solution as a whole.

Seena answered 20/12, 2020 at 2:11 Comment(1)
have you resolve the problom?Ditch
A
13

Had a play with both answers above, but neither worked for me. I generate some files during the build and that may be why the other 2 don't work for me. According to Microsoft docs shared above (https://learn.microsoft.com/en-us/aspnet/core/tutorials/dotnet-watch?view=aspnetcore-3.1), to remove an item from watch you can also set it through the definition. Doing this worked for my scenario.

<ItemGroup>
    <Content Remove="wwwroot\dist\**" />
    <Content Include="wwwroot\dist\**" Watch="false" />
</ItemGroup>

I did have to add the Content Remove as otherwise the item is declared twice and the build will fail.

Accusal answered 7/6, 2021 at 23:42 Comment(2)
Thanks! A tip: this could be more simply achieved with <Content Update="wwwroot\dist\**" Watch="false" />.Ku
Another tip for anyone struggling, dotnet watch --list in the project dir tells you exactly what files will be watched, so you can easily test these changes.Belch
K
1

Please edit your .csproj file and add the following to it

<ItemGroup>  
    <Watch Exclude="**\*.razor.css" />
</ItemGroup>

More info at

https://learn.microsoft.com/en-us/aspnet/core/tutorials/dotnet-watch?view=aspnetcore-3.1

Knot answered 20/12, 2020 at 19:32 Comment(1)
Hey, thanks, I already tried this - However, I got the following error: error MSB4232: Items that are outside Target elements must have one of the following operations: Include, Update, or Remove.Seena
K
1

I had the same issue after coming here, ended up with:

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

Works nicely for me :)

Kasandrakasevich answered 20/3, 2021 at 20:3 Comment(0)
C
0

I have a similar set-up except but I'm using SASS. I have a custom target to execute the command npm run sass, so the files are being generated as part of the dotnet build process, which made the watch re-trigger builds in an infinite loop.

In my case, the solution I found was like the following:

  1. Alter the DefaultItemExcludes:
<PropertyGroup>
   <DefaultItemExcludes>$(DefaultItemExcludes);Features/**/*.css</DefaultItemExcludes>
</PropertyGroup>
  1. Adjust sure the css compilation target to run before BeforeResGen instead of Compile (which I was using before):
  <Target Name="CompileScopedScss" BeforeTargets="BeforeResGen">
    <Exec Command="npm run sass -- %(ScopedScssFiles.FullPath) %(RootDir)%(Directory)%(FileName).css --no-source-map" />
  </Target>
  1. Include the css file and set Watch="False":
    <ItemGroup>
      <Content Include="Features/**/*.css" Watch="False" />
    </ItemGroup>

I do this in a target after the previous target, but it seems to work also outside it.

Just using the <Content Update... Watch="False" /> wasn't enough in my case. The <Watch Remove="..." /> didn't work either in my case and I believe it could have something to do with the fact that the files are generated during the build.

Calciferol answered 10/1, 2022 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.