I was playing with Sitecore solution, project setup, and configuration, I faced a need to alter the code generation logic used in it. We use TDS for content synchronization as well as its code generation engine build on T4 text templates.

While T4 is great for code generation, it is not as straightforward to debug them as it might be expected. So let me describe how to do this.

How-To

I have a solution with a set of projects. Each TDS project is referencing to the templates in a central location.

2016-07-18_2343

To start debugging I would need the following:

  • Start two instances of Visual Studio (in my case 2015) with the project containing T$ templates.
  • Add debug=“true” to a template
1
2
3
4
5
6
<#@ template language="C#" debug="true" hostSpecific="true" #></pre>
<ul>
  <li>Add a following line of code to the template. <em>Note: that items template will not be triggered is debug added to a header template, so you would nee to add     line to one you trying to test</em></li>
</ul>
<pre>[code language="csharp"]
<# System.Diagnostics.Debugger.Launch(); #>
  • On the TDS project that you using run “Regenerate Code for all items”

  • Debugger windows will appear as soon as code would hit “Debugger.Launch()” where you would need to select another instance of Visual Studio which runs your templates.

2016-07-19_0858

Done: Once the debugger loads itself - you are ready to dive into your code. Here is what you would be able to see in glassv3header.tt for example.

2016-07-19_0902.png


Follow me on Twitter @true_shoorik. I would be glad to discuss the ideas above in the comments.