Introducing the New T4 Command-Line Tool for .NET

Mike Corsaro

We’re happy to announce that Visual Studio 2022 v17.6 now includes an updated Text Template Transformation Toolkit (T4) command-line tool built with .NET 6.

For those unfamiliar, T4 is a powerful framework that allows you to automate the creation of text files. It’s perfect for automating the creation of HTML, XAML, or even code models from REST APIs. These template files can contain invokable .NET code and string literals, so it’s critical that our users are able to utilize the latest .NET 6+ features and libraries.

Try It

Using the new TextTransformCore.exe utility is simple: all arguments are the same as TextTransform.exe. The location of the new utility is also the same and can be found under {VS_INSTALL_PATH}\Common7\IDE\TextTransformCore.exe.

Feedback

We want to hear from you! Please file feedback or issues in our Developer Community.

Additionally, there are a few questions we have about how best to support our T4 users who wish to use .NET (Core) libraries, so we’ve prepared a short survey.

Limitations

We currently do not yet support in-IDE or MSBuild task file generators for .NET 6+. However, if your template does not rely on invoking in-IDE services you can work around this by using the new TextTransformCore.exe with the Exec command.

Here’s an example, as a PreBuild step:

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Command="'$(DevEnvDir)TextTransformCore.exe' '$(ProjectDir)MyFile.tt'" />
</Target>

Known issues

There’s currently a known issue where transforms will fail when setting the attribute hostSpecific to true. If you do not use this.Host we recommend that you set the attribute hostSpecific="false". This issue only applies to TextTransformCore.exe.

Thanks for your patience — we’re excited to see how you utilize these new T4 capabilities.

24 comments

Discussion is closed. Login to edit/delete existing comments.

  • Θοδωρής Τσιρπάνης 7

    Great work! I’ve been considering to use T4 in a project of mine. Any chance to release it as a .NET tool?

    • Marco GoertzMicrosoft employee 1

      I would encourage you to go to https://developercommunity.visualstudio.com/, request a new feature, and respond back here with the link to the ticket. That way others can upvote, we can get this officially tracked, and we can provide you with a better way for updates and further communication. Thanks!

  • Thomas Levesque 18

    Cool. Why not make it a dotnet CLI tool? Something like dotnet t4 mytemplate.tt.

    • Mike CorsaroMicrosoft employee 3

      Hello! Thanks for bringing that up — it’s something we’ll seriously consider for sure.

      • Howard van Rooijen 1

        In the process of modernising our toolchain from .NET Framework to .NET out CI/CD process (in GitHub Actions) is entirely based on pulling in .NET Global Tools. The days of having a build image with Visual Studio (in order to access all the tools) are thankfully long gone! It feels much more like the GO / Rust approaches.

    • Marco GoertzMicrosoft employee 1

      I would encourage you to go to https://developercommunity.visualstudio.com/, request a new feature, and respond back here with the link to the ticket. That way others can upvote, we can get this officially tracked, and we can provide you with a better way for updates and further communication. Thanks!

    • Mikayla HutchinsonMicrosoft employee 1

      FWIW there is a .NET CLI tool for T4, though it uses a different T4 implementation – https://www.nuget.org/packages/dotnet-t4/

  • Amadeusz Sadowski 8

    Why is it in /dotnet blog? It’s neither open-source, part of .NET SDK, or supported by dotnet tooling. It’s a Visual Studio exclusive feature. Move it to Visual Studio blog then. Why isn’t it open sourced? So much work to update, why not share with community to enable porting for cross-platform usage, be open for community enhancements, performance improvements, and so much other good stuff.

  • Jon Miller 1

    I gave up on T4, due to Visual Studio’s complete lack of editor support for the files. I switched to just using regular WriteLine() statements in normal code for generating code.

  • MgSam 0

    T4 rises from the dead! Super-exciting to see you guys resurrect T4 and start to modernize it.

    I hope you will be able to make it open source and figure out some of the portability issues (the biggest one in my mind was always how to figure out the project root without VS).

  • Alexey Gvozdikov 0

    Am I alone who doesn’t understand why you wrote “TextTransformCore.exe”, having WORKING “TextTransform.exe”???

    You have TONS of tasks, but instead you write hell knows what, wasting time and money. VS for 20 years(!!!) lacks syntax highlight for *.tt files – who and why still not implement it? In C# islands you have nothing – no highlight, no intellisense. It’s not more complex than ASP files (which HAVE support).

    MS disappoints me more and more with every single month. What is going on in your company? Why we see no any adequate progress?

    • Mike CorsaroMicrosoft employee 2

      Hello! There were a few reasons why we needed to create a separate .NET 6 version of the TextTransform utility beyond the reason of it being one of our highest voted issues:

      1. The existing T4 engine heavily uses the .NET Framework AppDomain technology, which no longer exists in .NET Core/6
      2. The existing T4 engine uses the Global Assembly Cache (GAC) which also doesn’t exist in .NET Core
      3. We didn’t want to break existing users of T4 who load and run .NET Framework assemblies in their T4 files

      Regarding syntax highlighting — this is something we’d like to implement, and I recommend that you vote on or create an issue in the community forums for visibility.

  • Mårten Rånge 5

    Hello. Great to see some updates to the old workhorse T4. I saw a few comments here about a dotnet tool and just FYI there’s a T4 clone developed for mono back in the days that is runnable as a tool. https://github.com/mono/t4

    Here’s a few things I wish T4 in the future.
    1. Support nuget references
    2. Package t4 templates as nuget packages.
    3. Basic syntax highlighting.

    For me I know people are saying source generators is the next big thing and it might be but for my use cases source generators does nothing and T4 does it excellently.

    Regards

  • Podbielski, Natan (External) 0

    So it is working only on Windows?

    • Marco GoertzMicrosoft employee 0

      At the moment, yes. This is a CLI tool bundled with Visual Studio.

  • Andrej Tozon 0

    While I’m porting all my T4 scripts to use the new Source Generators, I’m wondering if you’re resurrecting T4 or just supporting existing users… I loved T4, but lack of tooling and more output options made it quite difficult to use with time.

  • AVATAR Jake 0

    Yes, really need intellisense and syntax highlight.

Feedback usabilla icon