11

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.

1
  • have you resolve the problom? Commented Jun 1, 2021 at 8:40

4 Answers 4

15

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! A tip: this could be more simply achieved with <Content Update="wwwroot\dist\**" Watch="false" />.
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.
2

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

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

Works nicely for me :)

Comments

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

2 Comments

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.
Note that you should only use Exclude when also specifying Include. Here, it would be more performant to use the Remove attribute. | learn.microsoft.com/en-us/visualstudio/msbuild/…
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.