When you build a project / solution in Visual Studio, the Up-to-date Check feature will determine if the project needs a full rebuild or if it’s already up-to-date. There are many factors it checks – mainly, did the source code change since last time?
Surprisingly, one thing that triggers a full rebuild is text template generated output (for example, when you are auto-incrementing your project build numbers). This is a problem if you have TransformOnBuild=true in your project file. It’s a bit of a vicious cycle. The Up-to-date Check sees your text template generated output as a change, so it triggers a full rebuild, which then causes the text transformation to run again (because TransformOnBuild=true).
Fortunately, you can make the Up-to-Date Check ignore your text template generated output, and hence stop the vicious rebuild cycle. I’ll show how to do that.
Note: This only seems to be a problem in SDK-style projects (the default in new versions of .NET). I tested the problem and fix in VS2022.
1 – Turn on Up-to-date Check logging
It’s a good idea to turn on Up-to-date Check logging so you can see what is causing it to do a full rebuild.
- Open the Visual Studio options (Tools > Options…).
- Go to Projects and Solutions > SDK-Style Projects.
- Change the Logging Level to Verbose
- Click OK.
This logs to the build output window.
Here’s what it looks like when a text template (ex: VersionAutoIncrement.tt) generates output (ex: VersionAutoIncrement.cs), which causes subsequent builds to be out of date, and therefore requires a rebuild every time:
2 – Ignore the text template generated output
To make the Up-to-Date Check ignore your generated file, add two properties to your .csproj file: UpToDateCheckInput and FastUpToDateCheckIgnoresKinds. Specify a value for the Kind attribute in the UpToDateCheckInput property (ex: GeneratedDuringBuild), and put this value in the FastUpToDateCheckIgnoresKinds property, like this:
Code language: HTML, XML (xml)
<Project Sdk="Microsoft.NET.Sdk"> <!-- rest of .csproj --> <ItemGroup> <UpToDateCheckInput Include="VersionAutoIncrement.cs" Kind="GeneratedDuringBuild" /> </ItemGroup> <PropertyGroup> <FastUpToDateCheckIgnoresKinds>GeneratedDuringBuild</FastUpToDateCheckIgnoresKinds> </PropertyGroup> </Project>
Now right-click the project and Unload Project, then Reload Project. For some reason, this is necessary to make the Up-to-date Check pay attention to the FastUpToDateCheckIgnoresKinds property (thanks to reader Brad V. for pointing this out!).
Now build once. It’ll do a rebuild, because you changed the .csproj file. Now do another build without doing any other changes. Notice that it correctly sees it as up-to-date, so it doesn’t do a rebuild.
FastUpToDateCheckIgnoresKinds feature availability
The FastUpToDateCheckIgnoresKinds feature was added recently to the project system repository. It appears this is available starting in VS2022. I tested on VS2019 and it didn’t work.