Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 353
Raheel Avatar asked Apr 03 '17 12:04

Raheel


People also ask

How do I debug a T4 template?

To debug a design-time text template, save the text template file, and then choose Debug T4 Template on the shortcut menu of the file in Solution Explorer. To debug a run-time text template, simply debug the application to which it belongs.

How do I debug a TT file in Visual Studio?

Build the solution and start running it in the debugger. (On the Build menu, click Rebuild Solution, and then on the Debug menu, click Start Debugging.) A new instance of Visual Studio opens the Debugging project. Add a text file named DebugTest.tt to the Debugging project.


2 Answers

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.

like image 124
Reznoir Avatar answered Sep 19 '22 12:09

Reznoir


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"'.

like image 21
Nicholas J. Markkula Avatar answered Sep 17 '22 12:09

Nicholas J. Markkula