Raheel
Raheel

Reputation: 431

Cannot debug T4 template in VS2017

In VS2017 Community, I cannot debug T4 Templates, which works in 2015.

I have a very basic template, such as this...

<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
<#
var a = "Hello";
var b = "World";
#>
<#=($"{a} {b}!")#>

Run Custom Tool and Transform All T4 Templates both options work, and text file contains expected output

Hello World!

If I put breakpoint somewhere and use Debug T4 Template from the context menu of .tt, it throws this error

Unable to start transformation run creation process.

However it works fine in VS 2015, and I'm able to debug there.

What I could be missing? how to debug T4 Templates in VS 2017? Note that I don't have any Tool/ Extension installed in VS2015 to debug T4

Upvotes: 11

Views: 5246

Answers (3)

UnreachableCode
UnreachableCode

Reputation: 1791

I had this issue in Visual Studio 2022. As it turned out, I needed to install Text Templating Transformations in the individual components section of the Visual Studio installer. I figured this out from this note in the docs

The Text Template Transformation component is automatically installed as part of the Visual Studio extension development workload. You can also install it from the Individual components tab of Visual Studio Installer, under the SDKs, libraries, and frameworks category. Install the Modeling SDK component from the Individual components tab.

Visual Studio Installer - Individual components tab

Upvotes: 0

Nicholas J. Markkula
Nicholas J. Markkula

Reputation: 1562

The easiest solution is to just add these two lines to the top of your T4 template.

<#@ template debug="true" hostspecific="false" language="C#" #>
<# System.Diagnostics.Debugger.Launch(); #>

Then just run the template by saving the file and visual studio will prompt you to debug in a new instance.

If you use Host in your template and you get the error The name 'Host' does not exist in the current context then set `hostspecific="true"'.

Upvotes: 4

Reznoir
Reznoir

Reputation: 939

I have had the same issue, I don't know why it doesn't work this way but I have a work around.

Set debug to true, and add the diagnostic namespace

<#@ template language="C#" debug="true" #>
<#@ import namespace="System.Diagnostics" #>

In your T4 template write

Debugger.Launch();

Then run your template (easiest way it just to save it) and it will ask if you would like to debug in a new instance of visual studio.

Upvotes: 9

Related Questions