Thursday, April 25, 2013

Visual Studio 2010 - Debug T4 Templates

In the previous post we discussed about debugging T4 templates in Visual Studio based on the new option provided. This allows debugging T4 templates in the same Visual Studio Editor instance. As a matter of fact the options are new but we could still debug T4 templates by attaching external debugger.

Let's update the same project as we created in Visual Studio 2010. You can download the project and open it in Visual Studio 2010. In order to add an external debugger, we need to launch the debugger from the template code as follows:

In addition to launch external debugger we have also set debug attribute for template directive as true. Basically Visual Studio creates a temporary class to generate the text by the template. This template class is created on the fly. Setting the attribute as true would add it to a temporary directory. If there is a compilation error the we can open the temporary file from the Errors List debug window. After updating the template code, we can simply run the custom tool by manually running it. Saving the template would also run the same tool:



This should launch another instance of Visual Studio Editor. Click Yes on the following dialog asking for permission to open a new instance.



The debugger breaks the code execution at the line where we launched the debugger. We can now use all the debugging support in visual studio in order to step through the code.



We can also use other debug windows including Locals and Call stack windows in order to see the values of variables and call stack within the template code. It seems Autos window was broken even in Visual Studio 2010.



No comments: