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?
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/
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With