In VS2017 Community, I cannot debug T4 Templates, which works in 2015.
I have a very basic template, such as this...
<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
<#
var a = "Hello";
var b = "World";
#>
<#=($"{a} {b}!")#>
Run Custom Tool
and Transform All T4 Templates
both options work, and text file contains expected output
Hello World!
If I put breakpoint somewhere and use Debug T4 Template
from the context menu of .tt, it throws this error
Unable to start transformation run creation process.
However it works fine in VS 2015, and I'm able to debug there.
What I could be missing? how to debug T4 Templates in VS 2017? Note that I don't have any Tool/ Extension installed in VS2015 to debug T4
To debug a design-time text template, save the text template file, and then choose Debug T4 Template on the shortcut menu of the file in Solution Explorer. To debug a run-time text template, simply debug the application to which it belongs.
Build the solution and start running it in the debugger. (On the Build menu, click Rebuild Solution, and then on the Debug menu, click Start Debugging.) A new instance of Visual Studio opens the Debugging project. Add a text file named DebugTest.tt to the Debugging project.
I have had the same issue, I don't know why it doesn't work this way but I have a work around.
Set debug to true, and add the diagnostic namespace
<#@ template language="C#" debug="true" #>
<#@ import namespace="System.Diagnostics" #>
In your T4 template write
Debugger.Launch();
Then run your template (easiest way it just to save it) and it will ask if you would like to debug in a new instance of visual studio.
The easiest solution is to just add these two lines to the top of your T4 template.
<#@ template debug="true" hostspecific="false" language="C#" #>
<# System.Diagnostics.Debugger.Launch(); #>
Then just run the template by saving the file and visual studio will prompt you to debug in a new instance.
If you use Host in your template and you get the error The name 'Host' does not exist in the current context
then set `hostspecific="true"'.
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