dotnet / project-system

The .NET Project System for Visual Studio
MIT License
967 stars 386 forks source link

Microsoft.TextTemplating.targets and FastUpToDate #9477

Open innominateAtWork opened 3 months ago

innominateAtWork commented 3 months ago

The Microsoft.TextTemplating.targets will run t4 transformations on any .tt files in your project on build. https://learn.microsoft.com/en-us/visualstudio/modeling/code-generation-in-a-build-process?view=vs-2022&tabs=csharp

However if a .tt file (and nothing else) is changed then FastUpToDate will prevent a rebuild (and thus prevent the t4 transform from rerunning).

Ideally Microsoft.TextTemplating.targets will add .tt files and their outputs to the appropriate UpToDate item groups.

I tried to work around this with

<ItemGroup>
  <T4InFiles Include="**\*.tt" />
  <T4OutFiles Include="@(T4InFiles->'%(FullPath)'->Replace('.tt', '.cs'))" />     

  <UpToDateCheckInput Include="@(T4InFiles)" Set="T4Files" />
  <UpToDateCheckOutput Include="@(T4OutFiles)" Set="T4Files" />
</ItemGroup>

However that doesn't work if you have more than one .tt file you end up with a warning like the below and the build runs everytime.

WARNING: Potential build performance issue in 'MyProject.csproj'. The project does not appear up-to-date after a successful build: Input UpToDateCheckInput item 'C:\MyProject\File1.tt' is newer (2024-06-05 20:27:00.678) than earliest output 'C:\MyProject\File2.cs' (2024-06-05 20:26:28.652), not up-to-date. See https://aka.ms/incremental-build-failure.
innominateAtWork commented 3 months ago

Actually the work around is just

<ItemGroup>
  <UpToDateCheckInput Include="**\*.tt" />
</ItemGroup>

the .tt files just need to be compared to the main set of outputs

It still would be nice if this was included in Microsoft.TextTemplating.targets

drewnoakes commented 3 months ago

I agree with your analysis here. Another option would be to use something like UpToDateCheckBuilt with Original metadata, so that the.tt file would be compared directly with its corresponding .cs file (given it's a 1:1 correspondence).

Microsoft.TextTemplating.targets is not defined in this repo, nor owned by this team. Could you file a feedback ticket with this suggestion? I will try and find the correct owner internally and route it to them.