I have a c# project which includes several generated files (by T4) which are later included in compilation. The generation of code files itself works fine, but unfortunately incremental build sees generated files as a 'changes since last build' and re-builds the project each time. (For me it is a problem, since a large part of solution is rebuilt each time).
The warning from MSbuild looks like
The project does not appear up-to-date after a successful build: Input Compile item '...\DtoGenerator.generated.cs' has been modified since the last successful build started, not up-to-date. See https://aka.ms/incremental-build-failure.
Related part of the *csproj:
<ItemGroup>
<UpToDateCheckInput Remove="*.generated.cs" />
<UpToDateCheckOutput Remove="*.generated.cs" />
<UpToDateCheckBuilt Remove="*.generated.cs" />
</ItemGroup>
<Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet t4 "%(TextTemplate.FullPath)" -P="$(ProjectDir)..\TextTemplating" -I="$(ProjectDir)..\TextTemplating"" />
</Target>
I added ´UpToDateCheck*´ as per link proposed in warning, but it does not seem to work.
I have also seen the other proposed solution where "<Compile Remove" is used, and it does seem to help with up-to-date check, but this way the generated files are not included in compilation.
Have anyone encountered similar problem? How can I change the *.csproj to have the files generated before build and included during compilation, but excluding them from incremental build check?
I suspect the issue is that you are not supporting incremental generation. Your
TextTemplateTransformtarget doesn't have inputs and outputs for the T4 files and the generated files, respectively.Assuming that
@(TextTemplate)is a collection of.ttfiles and that there is a one-to-one relation of.ttto.generated.cs, then modify the target definition as follows:Is this a Windows specific build? You may want to consider using the Modeling SDK's text transformation targets file instead of writing and maintaining your own
TextTemplateTransformtarget. See "Invoke text transformation in the build process".