Debugging T4 Text Templates

People often ask me about the best way to debug T4 templates.  This topic is covered in our new downloadable help file CTP, but I thought I'd get it up here in searchable form.
 
The first thing you need to do is to set the debug flag on the template directive of your template.
 
<#@ template debug="true" inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" #>
 
If you're debugging a system-provided template rather than one of your own, this typically means that you'll need to copy the included portion of the template directly into the template file that's in your project.
 
Next you'll need to bring in the System.Diagnostics namespace (this isn't strictly necessary but it means you won't have to fully-qualify types).
 
<#@ import namespace="System.Diagnostics" #>
 
Finally, call Debugger.Break wherever you want to stop in your template.
 
<# Debugger.Break(); #>
 
When this gets hit, you should be asked whether you want to start a new instance of Visual Studio to debug or use an existing one.  You'll then be debugging within the generated-on-the-fly transformation class that implements your template.
 
If you're going to be doing a lot of debugging, there's also another technique available.
 
Open a second instance of Visual Studio.
Open your template files in the new instance.
Set breakpoints on the control code in the template just as though it was regular code.
Choose Tools/Attach to Process… and pick the original devenv.exe Visual Studio process.
Run your template in the original Visual Studio.
Your breakpoint should be hit.
 
I haven't actually tried it, but I believe these steps should also work for T4 templates used within GAT.
 
 
Published 09 January 06 05:16 by GarethJ
Filed under: , ,

Comments

# Sonnet said on June 6, 2006 1:01 PM:
Hi,
 I follwed all the steps you have described for debugging T4 template, but I get the following exception when I try to generate code off of the T4 template with debug statements:

Failed to load base class 'Microsoft.VisualStudio.TextTemplating. ... ModelingTextTransformation' from which Transformation class inherits

Any help will be greatly appreciated.

Thanks.
# Bharath said on July 28, 2006 7:50 AM:
Same issue here. Is there any other way to debug T4 templates in GAT packages.
# José Escrich Blog said on June 7, 2007 10:40 AM:

Durante este mes de mayo estuve trabajando en un editor de T4 templates como parte de SFT , así que ahora

# jescrich said on September 6, 2007 11:02 PM:

During this past month I've been working in a new T4 template editor as a part of the SFT . If you're

# Nathan Allen-Wagner said on October 20, 2008 12:38 PM:

Great info on T4 Templates for VS

New Comments to this post are disabled

Search

This Blog

Disclaimer
The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion.
All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Tags

Archives

Architects who Model

DSL Tools Team

Links

Syndication

Page view tracker