7

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.

Kit
  • 20,354
  • 4
  • 60
  • 103
Sossenbinder
  • 4,852
  • 5
  • 35
  • 78

4 Answers4

11

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Herman Vos
  • 121
  • 2
  • 5
  • 2
    Thanks! A tip: this could be more simply achieved with ``. – bart Nov 29 '21 at 17:02
  • 1
    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. – René Sackers Mar 31 '23 at 13:14
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

Kit
  • 20,354
  • 4
  • 60
  • 103
ademg
  • 197
  • 2
  • 4
  • 17
  • 5
    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. – Sossenbinder Dec 21 '20 at 01:02
1

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

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

Works nicely for me :)

McKnight
  • 121
  • 2
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.

dacucar
  • 206
  • 3
  • 10