Setting a breakpoint in a T4 template - debugging

I'm trying to debug the execution of a T4 template in Visual Studio 2008.
All the information I'm finding on debugging T4 templates in Visual Studio 2008 say that you can set a breakpoint (red dot) in the template as if it were a regular code file. I have the Clarius T4 code highlighter installed, so my T4 template is colored, but I can't set a breakpoint. When I click in the margin nothing happens.
I've tried Debugger.Break(), and it launches a new instance of VS.NET, but it can't load the code from my template. I get a dialog that says "There is no source code available for the current location." This happens if I have the same project loaded in the another instance of if I spin up a new instance.
What gives?

Set the following:
<## template debug="true" hostSpecific="true" #>
<## import namespace="System.Diagnostics" #>
Then in your template
Debugger.Launch();
VS will kick off the JIT debugger in a new instance of VS 2010

In Visual Studio 2013:
Set a breakpoint in the .tt file
Right-click the .tt file in the solution explorer
Select "Debug T4 Template"
Done!
No attaching a second instance of Visual Studio needed.

OK- figured it out. Launching a new instance is not an option, regardless of what Oleg's article says. (No diss to Mr. Sych, his blog is gospel for T4 code generation!)
Start a second instance of Visual Studio,
Open a file (any file) so the Debug menu shows up.
Select "Attach to Process..." and select the other VS.NET instance
Save your template in the attached instance of VS.NET (or right-click and select Run Custom Tool)
Voila.

Make sure that you turn on the debug option in the template directive:
<## template language="C#" debug="true" #>
This makes T4 save the source code and symbol files necessary for debugging in Visual Studio.

Related

Remove automatic addition of using statements on Visual Studio on using methods not in the current namespace [duplicate]

I am using Visual Studio with Unity. When I copy and paste a code in Visual Studio. It automatically adds an unnecessary namespace on the top. It is pretty annoying to see if there have many unused namespaces over time. May I ask if is this a bug or how can I turn off the auto import namespace when copy paste?
From the suggested name, I think Visual Studio confuses the "length" and "Length"
Here are the steps to reproduce the problem
On the top of the page in Visual Studio, go to Tools > Options > Text Editor > C# > Advanced > Uncheck the option for "Add missing using directives on paste" then click OK on the bottom of the page. And then restart Visual Studio.
Might be useful to some to note that tab completion will still auto-add missing using directives.
Here is an example with the option checked (the example specifics are not relevant but in this case an attribute is being pasted to an ASP.Net Core Razor Page):
Here is an example with the option unchecked:

How to execute T4 template files in Visual Studio For Mac?

I am unable to run text templates in Visual Studio for Mac or visual studio code. I tried:
VS > Right click project > Tools > Process T4 templates > (No Templates Found)
I also went to Visual Studio top menu > Extensions > (Search for T4) > IDE Extensions > Text Templating v8.10.6 is installed.
I am trying to use the *.tt template from linq2db.SqlServer.. But running any basic T4 template would let me know the TextTemplatingFileGenerator is at least working. There the documentation states:
Make sure that custom tool for your tt file set to TextTemplatingFileGenerator, otherwise it will not run
Though it doesn't explain how to actually accomplish this task?
I also installed TT-Processor into visual studio code. I was also unable to execute the *.tt file. In this case I did not see an execute/run command when I right clicked a *.tt file.
Microsoft answered the question for me. As I already mentioned, the part that nobody talks about is the answer to this:
Make sure that custom tool for your tt file set to TextTemplatingFileGenerator, otherwise it will not run
In visual studio right click the *.tt File > Properties > Custom Tool > Choose "TextTemplatingFileGenereator"
Now visual studio will find the *.tt file when you Right click the project > Tools > Process T4 templates
Arguably, this experience should be improved to default to this behavior to fit the philosophy of "fall into the pit of success".

Debugging T4 Template VS2013

I am trying to debug T4 template in Visual Studio 2013. But I don't see "Debug T4 Template" option when I right click on my .tt file in solution explorer.
What do I need to do to be able to see that option and to successfully debug the template?
Sometimes you have to wait for the Error List to Load till you can Debug T4.

How to debug Visual Studio extensions

I'm just writing a VSIX extension for Visual Studio 2010 and can't figure out how to debug it.
One obvious method is to output messages. Extension template uses Trace.WriteLine(). But where to find it's output?
Visual Studio Extensions can be debugged like any other application. You just need to setup the debug experience to launch devenv with the loaded extension. Try the following
Right click on the project and select Properties
Go to the Debug Tab
Click on the radio button for Start External Program. Point it to the devenv.exe binary. On my machine it's located at
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe
On a non x64 machine though you can remove the " (x86)" portion.
Then set the command line arguments to /rootsuffix Exp. This tells Visual Studio to use the experimental hive instead of the normal configuration hive. By default VSIX extensions when built will register themselves in the experimental hive.
Now you can F5 and it will start Visual Studio with your VSIX as an available extension.
The accepted answer by #JaredPar is technically correct, but suffers from the fact that you need to redo it for every developer, every time you get a fresh copy of the code, and any time the csproj.user file is deleted. When you do it that way, the settings are saved in the csproj.user file.
A better option is to put the settings in the csproj file so they are not lost. Unfortunately, Visual Studio does not allow you to do this automatically, so you need to manually add the settings. Luckily, the settings are the same for any project.
Right-click and unload the project, then right click again and edit the csproj project file file. In the XML, add the following to the first PropertyGroup, for example right after TargetFramework.
<StartAction>Program</StartAction>
<StartProgram>$(DevEnvDir)\devenv.exe</StartProgram>
<StartArguments>/rootsuffix Exp</StartArguments>
This has the following advantages;
It sets it up for debug and release
It runs whatever version of Visual Studio you are currently running
It is checked into source control, so every developer doesn't have to remember how to do it :)
As #MBulli states in the comments, if you have made the changes in the accepted answer, delete your *.csproj.user file because the settings in it will override the ones you added to the main csproj file.
The OutputWindowHelper.OutputString method writes to the 'General' output window pane (Ctrl Alt o). I added this line in my .csproj references to get this in VS 2013
<Reference Include="Microsoft.VisualStudio.Services.Integration, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
Also see this answer.
If you try to debug a UnitTestExtension, you should also attach the debugger to the vstest.*.exe processes like descibed here. Otherwise you might see the activate breakpoint but the debugger will never hit it.

Debugging T4 Template in VS 2010 Crashes IDE

I'm trying to debug a slightly-modified version of the ADO.NET POCO Entity Generator template using the directions Oleg Sych published a few years back. I modified the DbgJITDebugLaunchSetting key as recommended.
I get a dialog indicating that a user-defined breakpoint has been hit. However, rather than being presented with the option to debug with a new instance of VS 2010, the original instance of VS 2010 just crashes and auto-restarts.
Is it possible to debug T4 templates with VS 2010?
in Visual Studio 2010 you need to call Debugger.Launch() before Debugger.Break().
Instead of using System.Diagnostics.Debugger.Launch(); or Break(), attach the debugger manually.
In a second instance of vs2010,
open the T4 template you want to
debug (just the .tt file is fine)
Go to Debug -> Attach to Process and find the original devenv.exe
process
Add a regular breakpoint
(red ball) to the place you want to
start the debug (in the second
vs2010 still)
Go to the original
vs2010, save the .tt file and...
bingo! The second instance of vs2010
will debug your template.
Final solution which works for me:
regedit:
Key (x86 systems): HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework
Key (x64 systems): HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework
value: DbgJITDebugLaunchSetting
data: 0x2
tt template:
<## template debug="true" hostSpecific="true" #>
<# System.Diagnostics.Debugger.Launch(); System.Diagnostics.Debugger.Break(); #>
You also need debug=true:
<##template debug="true" #>
System.Diagnostics.Debugger.Launch();
Debugger.Break();
http://msdn.microsoft.com/en-us/library/bb126338.aspx
To add to andrecarlucci's solution, if you save the file, you will be prompted to reload it in the second instance of Visual Studio before you can debug it. If you don't need to make further changes but need to debug it multiple times, you don't have to save every time in order to break into the code. You can simply click the Transform All Templates button on the Solution Explorer toolbar in the original instance of Visual Studio.

Resources