T4 templates not generating output in new VS2017 csproj projects
Asked Answered
V

2

11

I migrated a project.json/.xproj project to the newer CS2017 .csproj format.

The project contains a T4 (.tt) template file.

It doesn't regenerate its output on save or build. The output .cs file isn't nested below the .tt file either.

Is there something I have to do to get this working?

Vickyvico answered 22/3, 2017 at 22:2 Comment(0)
C
16

.tt files are only auto-run by VS on save. You can install AutoT4 to have them run before/after build. (Be aware that at the moment there is a limitation with the new .csproj files - the options don't show up for them in the properties window.)

If you've converted from the old project.json/.xproj format, you may need to add the template to the project explicitly:

<ItemGroup>
  <None Update="Foo.tt">
    <Generator>TextTemplatingFileGenerator</Generator>
    <LastGenOutput>Foo.cs</LastGenOutput>
  </None>
  <Compile Update="Foo.cs">
    <DesignTime>True</DesignTime>
    <AutoGen>True</AutoGen>
    <DependentUpon>Foo.tt</DependentUpon>
  </Compile>
</ItemGroup>

Related GitHub issue

Edit

As mentioned in the comments below, you can do this quickly & easily by excluding, then including the template in your project.

Chalmer answered 22/3, 2017 at 23:32 Comment(6)
I find that you can right-click exclude the TT file and then include again if Visual Studio's conversion missed marking the TT file explicitly.Claudeclaudel
Thanks. That's a great tip. I've added it to my answer.Chalmer
This is weird. That's exactly what i have, but no txt file is generated - on both VS2016 and VS2017.Neace
The string TextTemplatingFileGenerator was exactly what I was looking for. VS2017's conversion missed setting the "Custom Tool" property on the TT file. I knew it needed to be set to something but didn't know what. For future visitors if you click on the TT file check the Properties window and make sure "Custom Tool" is set to TextTemplatingFileGenerator. Custom Tool Namespace should be blank.Parlando
Thanks for this tip. I have a problem in my solution. As far as I can see, everything is set up correctly. Generate on save works, but generate on build does not. What could be the cause of problem?Skidproof
In my case the issue was that <None Include="Foo.tt"> instead of <None Update="Foo.tt"> was in the csproj fileBracci
S
2

I realise this is 2+ years old but for those bumping into this issue years on like me, the method listed below works for me without installing anything. I had the exact same issue, after upgrading a project from Visual Studio 2010 to Visual Studio 2017. YMMV. Make a backup copy of your .csproj file before you start.

Forcing rebuild of all .tt files when you build your project can be achieved without installing anything, by editing the .csproj project file. Editing the .csproj file seems clunky, but is is the approved way https://learn.microsoft.com/en-gb/visualstudio/modeling/code-generation-in-a-build-process?view=vs-2015

Within your .csproj file, you will find lots of PropertyGroup nodes. At the end of the list of PropertyGroup nodes (position not critical), add another PropertyGroup node with this content:

<PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly> 
</PropertyGroup>

Now look near the end of the .proj file, and you will see a line like this:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

(For interest, on my computer with VS2017 on it that resolves to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.CSharp.targets)

Beneath that line, add a line like this:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets" />

(For interest, on my computer that resolves to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets)

YMMV. If yours is a web project, there is probably a line nearby that is similar but to do with Microsoft.WebApplication.targets, from which you can draw inspiration.

That, possibly with a restart of Visual Studio, should do it. If you delete the transformed file that your .tt file emits, and then do a rebuild of your project, you should see that the emitted file reappears.

Sandstone answered 9/8, 2019 at 13:25 Comment(1)
The linked article has significant additional value. MSBUILD generates the templates differently than the IDE does, so my $(ProductVersion) was coming out literally in the generated CS file. The article shows how to alter the CSPROJ file to make that work properly, too.Overset

© 2022 - 2024 — McMap. All rights reserved.