19

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?

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • 1
    Wow, Skynet is now up to T4 model? – DVK Mar 23 '10 at 15:33
  • I have the same behavior with VS2008; I am not able to set a breakpoint in the .tt file. (I also have the Clarius highlighter installed, but I wouldn't think that is related to the breakpoint issue). However, I can set breakpoints in VS2010 in the .tt file. Not useful to you, but maybe of interest to someone. – Mark Wilkins Mar 23 '10 at 16:38
  • Adding these is necessary in VS2017 and VS2019 as well. You'd think VS would at least complain they're missing instead of just do nothing and report no error at all. – Chris Bordeman Oct 12 '19 at 11:04

4 Answers4

30

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

ShaneGray
  • 561
  • 4
  • 4
  • Worked perfect... launches second VS for debugging; steps through template and into any debug code you assembly-loaded. Nice! – Kirk B. Jul 29 '13 at 20:46
  • Just one more step for some... Make sure to install the Just-In-Time debugger first by modifying your VS installation! (add it as an individual component) – sw1337 Aug 26 '20 at 18:47
18

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.

Emond
  • 50,210
  • 11
  • 84
  • 115
9

OK- figured it out. Launching a new instance is not an option, regardless of what Oleg's article says. (No diss to Mr. Sych, his blog is gospel for T4 code generation!)

  1. Start a second instance of Visual Studio,
  2. Open a file (any file) so the Debug menu shows up.
  3. Select "Attach to Process..." and select the other VS.NET instance
  4. Save your template in the attached instance of VS.NET (or right-click and select Run Custom Tool)

Voila.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
6

Make sure that you turn on the debug option in the template directive:

<#@ template language="C#" debug="true" #>

This makes T4 save the source code and symbol files necessary for debugging in Visual Studio.

Oleg Sych
  • 6,548
  • 36
  • 34