Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 Preprocessed Template Debugging Not Working - Visual Studio 2010 RTM

I am attempting to debug a preprocessed T4 template and I am not able to step into the class created by running the preprocessed template. I am able to create an instance of the class but as soon as I try to step into while debugging, a new window pops up that says

No source available. There is no source code available for the current location.

My understanding was that preprocessed templates could be debugged just like a normal c# class, is this not correct? Is there anything in particular that you need to do to be able to step into the class defined by a preprocessed template?

Here is a very simple template and the calling code that I am experience the problem with:

TestPreprocessedTemplate.tt:

<#@ template language="C#" debug="true" #>
Hello <# Write("World"); #>

Test Code:

var template = new TestPreprocessedTemplate();
string test = template.TransformText();

Edit - Added the debug="true" statement per the suggestion below, still have same problem.

Update - I also posted this question on the MSDN forums and received response from a MS employee that indicated yes what I described above should indeed work. Anyone else ever run into this problem?

Update - With some help from the MSDN forums, it looks like the problem is with the #line directives that get added to the generated c# class. Commenting them out allows me to step through the code as expected. Is there any way to prevent these directives from being added to the generated class? With an ASP.NET page you can add the LinePragmas="false" parameter but that does not appear to have any effect on a T4 template. Any ideas?

like image 415
JohnZoidbergMD Avatar asked Aug 17 '10 20:08

JohnZoidbergMD


3 Answers

in Visual Studio 2010 you need to call Debugger.Launch() before Debugger.Break().

oleg is the master I'd check ou http://www.olegsych.com/2008/09/t4-tutorial-debugging-code-generation-files/

like image 196
James Fleming Avatar answered Sep 19 '22 16:09

James Fleming


It looks like your missing the debug="true" item in the template header which is necessary for debugging.

Also I would take a quick look at the following blog article which goes over T4 template debugging in great detail.

  • http://blogs.msdn.com/b/garethj/archive/2006/01/09/t4debugging.aspx?wa=wsignin1.0
like image 29
JaredPar Avatar answered Sep 19 '22 16:09

JaredPar


Update - With some help from the MSDN forums, it looks like the problem is with the #line directives that get added to the generated c# class. Commenting them out allows me to step through the code as expected. Is there any way to prevent these directives from being added to the generated class? With an ASP.NET page you can add the LinePragmas="false" parameter but that does not appear to have any effect on a T4 template. Any ideas?

The #line directives actually produce problems when debugging Preprocessed T4 templates (the debugger always searches for the *.tt file instead of the generated *.cs file). I wasn't able to find any option to turn of the generation of the #line directives. So I'm using the following VisualStudio Macro to get rid of them

Sub RemoveLineDirectives()
   DTE.ActiveDocument.Selection.SelectAll()
   DTE.ActiveDocument.Selection.ReplaceText("#line", "//#line")
End Sub

I'm always assigning the Macro to some short command within the command window

alias rl Macros.MyMacros.Module1.RemoveLineDirectives

So I'm able to remove the #line directives by simply invoking rl in the command window while the generated *.cs file is active, when I need to debug the Preprocessed T4 Template. After removing the #line directives debugging the generated template class works as expected.

Not the ideal solution but it works :)

like image 23
paiden Avatar answered Sep 18 '22 16:09

paiden