Debugging T4 Template in VS 2010 Crashes IDE - debugging

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.

Related

Code-Snippet not refreshed in Visual-Studio

I am creating Code Snippet in Visual-Studio 2010 using "Snippet Editor". All is working fine when i first save the snippet in the folder ... Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets.
The snippet is executed when i do CTRL-K CTRL-X and choose it in the list, and also by intellissence using the snippet Shortcut.
My problem comes when i only change the code of the snippet and save it again :
the new code is executed when i hit CTRL-K CTRL-X
the old code is executed when using intellisense ??
the problem is still here when i reopen VS
I don't think this is a "Snippet Editor" issue. Is there any "Intelissence Cache" i would have to reset ?
I have the same issue in visual studio 2019.
I have a custom ( My Code ) snippet file with multiple CodeSnippet elements added with the Code Snippets Manager and recognized and working in visual studio.
I make two changes.
The first I add a comment to an existing CodeSnippet just to demo that the change has been recognized.
I then change the Shortcut element of another CodeSnippet and completely change the Code.
Import again with Code Snippets Manager and Overwrite.
Use both snippets.
The first now has the additional comment.
The second does not have the code that is in the CDATA of the Code element but has what was there before !
Put the two CodeSnippet elements in separate snippet files and all ok.
Seems to be a bug in Visual Studio 2010, no more problem on the next version of VS

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 do I change the default list of exceptions shown in Visual Studio's Exceptions dialog?

There is a certain C++ exception which I never want to stop for while debugging. I know I can add it to the list of Visual Studio exceptions, as described here: how do I exclude user defined exceptions in the visual studio 2010 debugger? ... and then uncheck it. Once I have added a new exception type to the list, is there some way I can save it, so it will be listed whenever I am using Visual Studio with any project/solution -- not just the one where I initially added it?

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.

Setting a breakpoint in a T4 template

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.

Resources