Scott Hanselman

Tiny Happy Features #1 - T4 Template Debugging in Visual Studio 2012

August 10, '12 Comments [67] Posted in Tiny Happy Features | VS2012
Sponsored By

(UPDATE: See other Tiny Happy Features)

At some point soon lots of people are going to start writing these epic blog posts about Visual Studio 2012. They will include LOTS of screenshots (some good and some bad), some small code samples and minimal context. I can't speak for other teams; I can only talk about what we worked on. The <AngleBrackets/> folks in Azure Platform and Tools (ASP.NET, IIS, WCF, EF, Azure much and more) have been putting a lot of work into what I sometimes call "Death by a Thousand Tiny Cuts." It's the little irritants that are as frustrating (or more so) as the big missing features.

Rather than a giant super post (although I'll do that at some point, I thought I'd showcase some Tiny Happy Features* that I or another passionate member of the team worked on just because it made life better. Some of these features might not be obvious or easily noticed. Actually it'll be better that you notice the absence of a tiny cut than the inclusion of a new shiny drag and drop toy. It's all part of a larger goal to make the experience of making Web Applications in Visual Studio enjoyable.

T4 Template Debugging

I've long said that T4 is one of the best kept "secret" features of Visual Studio. T4 (the Text Template Transformation Toolkit) doesn't have a whole pile of people working on it, but developers like Gareth Jones really care about it and we really like it in the Web team. Lots of our projects use and have used T4 to generate code. In Visual Studio 2012 Gareth worked with Tim Malone so Tim could make a T4 debugger! Be sure to check out the T4 Team Blog.

Sure, T4 fans have always wanted built-in T4 Syntax Highlighting, but in this case, there were already a great T4 SyntaxHighlighting solution or three good options and there was no FREE debugger. Until now.

T4 in Visual Studio 2012 has Debugging!

You can now right-click on a T4 templates and Debug T4 template. I've got a breakpoint there on a line within the T4. My own T4 is syntax highlighted with the Tangible Editor.

There's the Debug T4 Template menu

When I start debugging I can set breakpoints and watches and step through just like any other language.

Debugging a T4 template

Notice here in the Immediate Window I'm looking at this.GenerationEnvironment. I can see the generation AS it happens in this variable.

Viewing T4 output

If you like T4 and you want them to know, leave a comment below and I'll use your comments to bludgeon convince management that we should continue to invest.

* Ya, some features aren't Tiny, but some are refinements and I like saying Tiny Happy Features. Sue me.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Friday, August 10, 2012 9:17:38 AM UTC
I absolutely love the T4 templates. I've created lots of template to generate a big amount of our layers automatically (our own DSL) and it saves us tons of time! I can only recommend this to every new (bigger) project to invest a little time into codegen using T4 templates, you'll be glad you did in the long run. Debugging them will be a great step forward when we migrate to Visual Studio 2012 (hopefully soon :) ).
Friday, August 10, 2012 9:22:32 AM UTC
Long time reader, first time commenter.

I love T4 Templates and use them extensively, however one of the "tiny paper cuts" that breaks my heart is that the autogenerated code autogenerates a base class for the Template class to inherit from.

This means my partial classes that add context and functionality to the template cannot inherit from a base class.

It is (IMHO) a questionable design decision. If you're generating the code, why not generate everything you need in one class?

I know it's not your area mate, but you did mention T4 Templates :)
Friday, August 10, 2012 9:23:52 AM UTC
Brilliant! Autogenerating certain types of classes using T4 saves us a lot of time, but developing and maintaining the T4 can be a bit of a pain point sometimes - this will make those things easier.
Friday, August 10, 2012 9:31:53 AM UTC
This is great news! T4 templating is a powerful tool I've used in many projects but it can be very hard to set up and understand why it doesn't work. Hopefully it's going to be much easier with a debugger!
Ni
Friday, August 10, 2012 9:37:51 AM UTC
T4 templates are an amazing feature. We use them to create multiple artifacts for our products and we also use them within our custom code generation framework, replacing our own templating system. The speed at which we can generate code with them is astounding!

They are a superb way to generate domain specific code either with, or without, the excellent DSL tools!

I do have a few things that could be improved, but that's not for here :) But MS definitely need to continue to invest in development of T4 !
Simply Ged
Friday, August 10, 2012 10:07:56 AM UTC
Sweet! Whilst it has always been possible with two instances of visual studio in the past (one attaching to the the other which is running the template), this is a much needed improvement.

Would be great to see syntax highlighting be a native part of VS2012 - plugins are a OK, but to me you're signalling that T4 is on it's way to becoming a first class citizen, so would be great if you could give it full citizenship :)
Friday, August 10, 2012 10:18:55 AM UTC
Binary - I don't understand. My example doesn't have a base class. What part of T4 generates something you don't like. Please forgive my ignorance.
Friday, August 10, 2012 10:21:11 AM UTC
Great news! I'd like to see inbuilt syntax highlighting too. The 3rd party editors are ok, but the quality needs to be better.
Friday, August 10, 2012 11:21:57 AM UTC
Well of course we love T4 because it makes our lifes so much easier and prevents us from putting typos in repeated tasks and helps us code faster by using reusable templates. But I suppose that syntax highlightig (although provided by third party tools) would work better when provided within VS since VS supports this file format. This is like adding third party tools to syntax highlight C# or Javascript or CSS or any other code that VS does out of the box. T4 should be no exception.
Friday, August 10, 2012 11:24:00 AM UTC
I don't use T4 for a lot of development but T4MVC is a huge help when coding calls to routes, helpers, redirects etc Without it refactoring of controller/action names is very hit and miss.
Friday, August 10, 2012 11:27:15 AM UTC
@Binary: T4 doesn't enforce any base classes to your generated files. Nobody even said that T4 should spit out code. It could as well be content. What base class can content have? None of course. Content is content. One could easily generate a text file with lines each with a random number in it for whatever purpose of their application...

I think you're referring to particular T4 templates provided by either some library/framework or something completely unknown to us.
Friday, August 10, 2012 11:33:23 AM UTC
Like Raf, I've used T4 alot for our own app framework to autogenerate BLL and DAL layers, based on a DSL we created with the SDK. We've updated it a few times and have used it in pretty much every project we've worked on. So much time and code writing can be saved with T4, especially the kind of code that follows the same tried and tested pattern (CRUD ops, etc).

T4 is definately underestimated and not talked about enough, especially for such a powerfull tool.

Debugging T4 is always painful. Small mistakes go somewhat unnoticed and most of the error messages tend to be cryptic. After a while you get used to it (lol).

But please do continue to invest in T4!
Friday, August 10, 2012 1:27:16 PM UTC
Super! A reason T4 syntax highlighting has been so valuable is as a poor man's debugger. Now we get a rich man's debugger. Kudos to Gareth Jones and others for caring enough to do something. Long live scaffolding!
Friday, August 10, 2012 1:56:13 PM UTC
Yes! I love T4 - and that debugging is built into Visual Studio it will be even easier to use than [an unnamed templating tool, rhymes with "ModeSmith"].

:-)
Friday, August 10, 2012 1:56:27 PM UTC
Yes! I love T4 - and that debugging is built into Visual Studio it will be even easier to use than [an unnamed templating tool, rhymes with "ModeSmith"].

:-)
Friday, August 10, 2012 1:56:36 PM UTC
Yes! I love T4 - and that debugging is built into Visual Studio it will be even easier to use than [an unnamed templating tool, rhymes with "ModeSmith"].

:-)
Friday, August 10, 2012 2:02:09 PM UTC
I love T4! The only real barrier for adoption in our company (until now) has been in debugging the templates! This is an awesome leap forward and I really do hope that Microsoft keeps improving T4.
Friday, August 10, 2012 2:10:22 PM UTC
I would love to be able to watch it spit out code as it goes. That looks great.

One more thing to look at would be the generation of multiple files from a single tt file. It can be done, but it doesn't feel right. It's not a huge pain, just a tiny little cut...
Friday, August 10, 2012 2:20:20 PM UTC
Love T4 templates - they are the backbone to most basic object mapping systems, and make life a little easier.
Friday, August 10, 2012 2:28:07 PM UTC
I discovered the T4 Templates when I found SubSonic. And it opened my eyes to the power of "writing code that writes code". This scratched the same itch that writing SQL that writes SQL does.

In my life, I used T4 to generate a simple tool that would parse a FreeMind MindMapper file (which is persisted as XML) and turn it into a decision tree. Using FreeMind as a UI, I was able to empower non techinical users to build dynamic text generation algorithms, complete with verbs like "CHOOSE", "APPEND", and "RANDOM" which determined whether to pick a single node, append all nodes, or randomly choose a node based on conditions respectively.

Once the non technical user was done, they would check in their changes, and then the automated build would check out the file, run my T4 utility to parse it and turn it into C# code. Then the code file was checked in, compiled and tested. The NON Tech users would actually have the ability to break the build and need to fix it themselves! How cool is that?

To this day, it's one of my greatest professional accomplishments.

Thanks T4 team!!!
Friday, August 10, 2012 2:58:26 PM UTC
Big fan of T4. Please continue to make productive improvements like this one.
tb
Friday, August 10, 2012 3:30:09 PM UTC
Great news! I love T4 templates and this will make them much easier to use.
Friday, August 10, 2012 3:47:08 PM UTC
We use T4 as an ORM of sorts. In our case, it replaced CodeSmith. It generates stored procedures, database layer, service layer and even some front-end stuff (as a starting point that we then hand edit).

So +1.

Friday, August 10, 2012 4:03:40 PM UTC
Great to hear that debugging T4 templates in VS2012 will be easier. The old method of debugging (launching a second VS instance to debug the first instance) was cumbersome and rarely ever worked correctly. I owe Gareth and Tim a beer for this one!

As for syntax highlighting... I've tried all three of the third-party T4 syntax highlighters, and haven't really been impressed with any of them. They're buggy, sluggish, and their intellisense is abysmal. I'd kill for VS.Net to have native support for T4 as a first-class citizen.
Friday, August 10, 2012 4:31:54 PM UTC
T4 Rocks. Debugging and more support is needed.
Friday, August 10, 2012 4:42:59 PM UTC
Hi Scott,

I am so glad that the <Anglebrackets/> team got around to tackle the "Death by a Thousand Tiny Cuts.". I always thought "great, now they implemented the next epic feature, but while working with VS you get poked by a thousand little things".
VS2010 was already a big step in the right direction. NuGet was an awesome feature making a lot of workflows a ton easier, and VS2012 will ease the whole process.

Also I am really "proud" of you guys for using awesome open source projects, contributing to them to get even better and not playing "big bad Microsoft" which would have tried to swallow a lot of those projects.
Even placing MVC as open source speaks volumes and just gives a "good feeling".

Keep on the good work at the <AngleBrackets/> team. :-)

T4 Templates Debugging is the last obstacle taken away that prevented me from extensive usage. This is an essential block you need to be able to cope with anything going wrong in the process of using T4 templates.

Thanks
Andreas
Friday, August 10, 2012 5:40:14 PM UTC
Never used it... BUT, I just got an awesome idea like 3 days ago that would only be possible with T4. So I'm happy with the update!
Mike
Friday, August 10, 2012 6:29:31 PM UTC
We use it in almost every web project such a great feature and best kept secret ever in VS!
It's not just about code generation, it makes our life easier and safer from typos and mistakes.
Friday, August 10, 2012 8:34:43 PM UTC
I use Code Generation in all my projects. Since I first met T4 this is my first choice for Code Generation.
Friday, August 10, 2012 9:32:06 PM UTC
Count me in as another fan of T4. :)

One minor annoyance I've run into with precompiled T4 in VS2010 SP1 is that when XML documentation is enabled for the project, there are two CS1591 warnings that appear to be unavoidable since they are caused by missing XML comments in the generated output of Microsoft.VisualStudio.TextTemplating:

MyTemplate.tt.cs(21726,39): warning CS1591: Missing XML comment for publicly visible type or member 'MyClassBase.ToStringHelper'
MyTemplate.tt(4,31): warning CS1591: Missing XML comment for publicly visible type or member 'MyClass.TransformText()'
Daniel Regner
Friday, August 10, 2012 9:32:11 PM UTC
I think this is what @Binary refers to...

"...When the template processor parses the template, it builds a class derived from the TextTransformation class in the Microsoft.VisualStudio.TextTemplating namespace. Once the engine finishes parsing the template it instantiates this class and invokes a TransformText method. The processor builds the TransformText method by taking all the code inside of standard control blocks and placing it inside the method. Since it is illegal in C# to have a named method inside of a method, we’ll create an error condition with the above code"

found on http://msdn.microsoft.com/en-us/data/gg558520.aspx

But I could be wrong :D

Mike
Saturday, August 11, 2012 3:46:27 PM UTC
I am developing a template library at work to easy the programmers work.
I also post on my blog a suite of .tt files that I have wrote in the past years.
So - debug the template is best!
Saturday, August 11, 2012 10:30:13 PM UTC
T4 has come in handy so many times and having a built in debugger is a big help! Would love to see T4 gain a little more prominence among the "big" features.
Monday, August 13, 2012 7:25:13 AM UTC
Just stopped by to let everyone know I could not do without T4. We use it to generate literaly hundreds of classes within our webservices. It saves a great deal of time and it provides us with great flexibility when it comes to adding or rethinking features in our framework.

It allowed us to build multiple cool API features like a fluint API for relations between EF entities.

If we would not have T4 we would have to build or buy tooling, which would cost us dearly. With T4 and the templates already provided by Microsoft we were able to do a lot of work for a fraction of the cost.
Monday, August 13, 2012 3:11:28 PM UTC
This is great news! However, how would it work with precompiled text templates? Since they are run during runtime like regular classes and objects, it it possible to step into the TransformText() method and go straight to the template? Or just the generated code from the template?

The third-party T4 syntax highlighters are okay at best. They've gotten better over the past 3 years I've been using T4, but never that great. I would love to see VS fully support a file type that is specific to .NET and Visual Studio.
It never really made sense to me that it .tt files can really only be used by Visual Studio and yet VS doesn't provide basic syntax highlighting or intellisense for them at all.

My thanks to the T4 team and all they've done with a great product!
Monday, August 13, 2012 7:08:38 PM UTC
Bludhound, Excellent.
Monday, August 13, 2012 7:35:37 PM UTC
Installing VPCSRVR@))*VRTL
Tuesday, August 14, 2012 6:15:45 AM UTC
I love T4; use it to generate my simple business layer methods based on EF's edmx model. Saves me a lot of boring coding.
Wednesday, August 15, 2012 8:47:16 AM UTC
We use T4 templates to make skeleton of our UI layer. I love it and hope it will keep improving. Thanks to the team!
Wednesday, August 15, 2012 7:38:00 PM UTC
Thanks for the post. I'd absolutely love template debugging feature. It'd be nice to have native syntax highlighting & intellisense and ability to generate multiple output files without third-party extensions. I currently use T4 Toolbox (https://t4toolbox.codeplex.com/) extension to generate multiple output files but it doesn't support VS2012.

Senthil
Wednesday, August 15, 2012 9:02:37 PM UTC
Scott, please let the Connect team review the bug #757600. It's a really insidious bug in ASP.NET 4.5 RTM.

In short, web sites with a lot of files (I don't known the threshold) does not compile right by aspnet_compiler when sources are read from a network share.

Check it.
Thanks
Glat
Wednesday, August 15, 2012 9:13:49 PM UTC
Glat - I can't find that bug. Can you email me?
Wednesday, August 15, 2012 10:19:43 PM UTC
I use a T4 template several times a year, and it saves my life each time.

As to futures, just keep in mind that T4 is used to generate all sorts of text artifacts. I most recently used it to generate SQL Server user-defined functions.
Thursday, August 16, 2012 6:06:33 PM UTC
Have been using T4 since long time, we use it regularly to generate code for business, data access and model/DTO layers of our projects along with stored procedures to select, update, delete and insert.
But the material available on the web for T4 is not much, last time we had a tough time converting the T4 projects from Visual Studio 2008 to 2010 due to breaking changes.

Hope MS puts more resources on this to take it further.

More than the debugger having a built in (not third party) syntax highlighter with intellisense would have been nice.
Friday, August 17, 2012 10:11:22 PM UTC
Instead of In addition to leaving a comment, to convince management that they should continue to invest, I suggest that all interested in T4 also vote on Visual Studio UserVoice - better support for T4 and Visual Studio UserVoice - T4 code generator to create multiple files

Scott: Since most people will not be reading all comments, it would help if you can include these two links on the article itself :)
Tuesday, September 25, 2012 2:25:00 AM UTC
It rocks T4 is a great tool.
Jason
Tuesday, October 02, 2012 3:05:17 PM UTC
Glad that the T4 features have been enhanced in VS 2012 and would like to see more progress in this direction for the future.

Will
Will
Saturday, October 06, 2012 3:19:05 AM UTC
I view code generation as a best practice. Similar to DRY.

I prefer to put common logic in base classes rather than in generated code. However, if using T4 templates with metadata will avoid repetitive work, then that is what I do. It started to pay big dividends very quickly. So I have been putting more and more effort into it. Ironically it has not been saving me time because I reinvest the time saved into building better templates, metadata, and code generation libraries. T4 makes writing templates is quick and easy. Not duplicating metadata is what takes the effort. Now I use it for everything from sql code, Dal, BLL scaffolding, controllers, web apis, javascript view model/ajax code, role based security, multi tenant security, and embarassingly I even generate many of my tests through code generation. I found it surprising how many patterns are repetitive, and how much meta data is reused in an application.

I believe my apps are more responsive to change, and of significantly higher quality because of code generation.

Please keep the T4 features coming. As great as it is, it seems to have only touched the surface of what could be done.
DarcyD
Wednesday, October 17, 2012 10:54:37 PM UTC
T4 Rocks it is a very productive tool
Jason Robertson
Tuesday, November 06, 2012 8:06:01 PM UTC
I'm extremely happy that T4 debugging is available now. I initially introduced this tool to my organization when I realized we didn't have enough Code Smith licenses for all our developers. Since then, we've been relying on it heavily in conjunction with T4 Toolbox for some massive amounts of code generation - all of it tightly integrated into our code solutions. Please continue to support this awesome tool.
Dan
Friday, November 09, 2012 10:08:55 AM UTC
Hi,

I wonder if there is a way to run multiple TT files at once.

We use tt files to generate managers, filterextensions, repositories and Viewmodels.

So when changing the model i want to run all the different tt files.

Frans
Frans van Ek
Tuesday, December 04, 2012 10:41:30 PM UTC
I love and use T4 extensively to generate proxies for UPnP services directly from the official service XML published by the UPnP forum. In actual fact, I generate control point and device side code in a range of different langues - c#, java, javascript, c, and c++.

UNFORTUNATELY, I cannot use the MS T4 executable directly because it does not seem to really support command line invocation. It seems to be overly focused on being invoked directly from visual studio. I have been forced into using a slightly modified version of the clone published as part of MonoDevelop. But as far as I am concerned, command line invocation with the ability to pass some template-aware arguments into the generation process would move this program from being vey good to truly great.

It would also be awesome if MS would open source this project - just feels exactly like the kind of project that MS, in its new and hugely appreciated more open spirit, might like to consider opening up. (PS please open source Powershell as well, pretty please)
Wednesday, December 12, 2012 1:40:20 PM UTC
Please, don't kill T4 templates... we need it!
Sunday, December 30, 2012 6:13:54 AM UTC
I am a big fan of templating. I have been writing custom templates for years to make my programming life easier. It's very cool that you guys are spending time developing such a useful feature. Thanks!
Chuck
Friday, January 04, 2013 8:43:49 PM UTC
Professionaly used it as a poor Man's Volta to generate Javascript code from .NET code. Been using T4 generated DAL layers and WCF services. Pleasant to use.

Privately used it tot torture the mono .NET compiler by generating all possible Curry function for 0-16 arguments functions. It generates 2*2^16 functions. When I digg up the code from backup(disk crash) I will try again on Windows and blog about it.
Friday, February 08, 2013 10:00:19 AM UTC
I've created an empty VS2012 MVC4 project but when I add a new T4 template to the project (from File -> New) the default TextTemplate1.tt doesn't have any syntax highlighting nor intellisense.

Any idea how to get it back?
David
Friday, February 08, 2013 10:47:53 AM UTC
Oh I didn't notice the syntax highlighting and intellisense isn't included in VS2012. It should be.
David
Sunday, March 10, 2013 1:20:42 PM UTC
T4 is awesome and your post as well, we are using it in our current projects. You are absolutely right its hidden secret of VS.
Mudassir
Monday, April 08, 2013 10:33:35 AM UTC
T4 is great, I just have a serious issue with VS2012: Whenever a T4 processing throws an exception, VS2012 crashes immediately, not giving any chance to debig. This even happens when Iadd a Debugger.Break(). Am I the only one experiencing this issue?
thomas
Monday, April 08, 2013 11:01:19 AM UTC
Ok, as always when crl crashes without any notice it was a stack overflow exception. However having VS surving this by running out of process would certainly be fine.
thomas
Tuesday, May 07, 2013 5:34:18 AM UTC
T4 is great!

I've been using to automate and add more rigour to a existing dev process that would otherwise involve the maintenance of 300 separate .css files.

Managed to do so by wrapping the .tt file in a data context and iterating to a list of records in a database table that has the file specific settings (client specific style guide) in it.

:)

What I would love for .tt to do is to be able to generate multiple files off the one .tt - To be honest, I think it already does (Entity Framework being an example of this - just not sure how - any pointers?)
Pradeep Shanmugham
Tuesday, May 14, 2013 5:14:03 PM UTC
I'm glad you linked us to the T4 blog, but it seems abandoned. Eleven months since the last post? Not a good sign.
Thursday, June 06, 2013 1:36:14 PM UTC
Scott,
So, I've been scouring the web, and haven't found an answer. Maybe you can help. I am working on three websites that all use my custom t4 templates (the "CodeTemplates" directory copied from the ItemTemplates in the visual studio directory). Right now I'm copying them to each project manually as I change them so that they stay in sync. Is there a way to have a shared library of these? I can't change the ones in the VS directory, as I have other projects that shouldn't use my customizations (oh, the joy of being a consultant).

Any help would be appreciated. Thx.
Monday, September 02, 2013 5:20:07 PM UTC
Please, Visual Studio needs to support native T4!!
Need better integration with modeling projects.
Thursday, September 19, 2013 8:37:31 PM UTC
T4 Templates are extremely useful. I have only used them a few times, but they have made some difficult problems easy to solve. Please keep investing in them.
Donovan Rogers
Tuesday, October 15, 2013 1:19:06 PM UTC
If we are able see integrated debugger with it, it would be awesome.
Friday, November 01, 2013 11:15:39 AM UTC
Does anyone know how can one create a custom data annotation attribute, apply it to a model class and then read that attribute values in the T$ template. I am talking about the MVC View and Controller scaffolding .tt files.
Cheers
bran
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.