Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a breakpoint in a T4 template

I'm trying to debug the execution of a T4 template in Visual Studio 2008.

All the information I'm finding on debugging T4 templates in Visual Studio 2008 say that you can set a breakpoint (red dot) in the template as if it were a regular code file. I have the Clarius T4 code highlighter installed, so my T4 template is colored, but I can't set a breakpoint. When I click in the margin nothing happens.

I've tried Debugger.Break(), and it launches a new instance of VS.NET, but it can't load the code from my template. I get a dialog that says "There is no source code available for the current location." This happens if I have the same project loaded in the another instance of if I spin up a new instance.

What gives?

like image 776
Dave Swersky Avatar asked Mar 23 '10 15:03

Dave Swersky


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 you set a breakpoint in Visual Studio 2017?

To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

What is transform all T4 templates?

t4 is basically a tool built into VS for doing text transformation, typically for doing code generation. Transform All T4 Templates searches your solution for *. tt files and executes them to create other text, again typically source code, files.


2 Answers

Set the following:

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

Then in your template

Debugger.Launch();

VS will kick off the JIT debugger in a new instance of VS 2010

like image 74
ShaneGray Avatar answered Sep 22 '22 05:09

ShaneGray


In Visual Studio 2013:

  1. Set a breakpoint in the .tt file
  2. Right-click the .tt file in the solution explorer
  3. Select "Debug T4 Template"
  4. Done!

No attaching a second instance of Visual Studio needed.

like image 34
Emond Avatar answered Sep 18 '22 05:09

Emond