Including custom sub templates fails in T4 Toolbox for VS2012

Jan 7, 2013 at 5:56 PM

I'm getting an error when using the new T4 Toolbox for VS2012 on an existing set of templates, some of which include other templates in the same project.

I've created a simple repro of the issue (please forgive the unavoidable Skydrive landing page... <sigh>):

http://sdrv.ms/VyjKqN

Note that TextTemplate2 tries to <#@ include file="TextTemplate1.tt" #> and this fails with:

An Exception was thrown while processing the template.  The following Exception was thrown:
T4Toolbox.TransformationException: T4 Toolbox transformation context was not properly initialized. Add <#@ include="T4Toolbox.tt" #> directive to your text template.

Server stack trace: 
   at T4Toolbox.TransformationContext.get_Current()
   at T4Toolbox.TransformationContext.Cleanup()
   at Microsoft.VisualStudio.TextTemplatingFCDB4ED959BACACD78225C7A7823D5822707A1B1CB6E24BC95E18F6903396276F44BD4CC8EF01EE80FC4B7F651DC8E8D0F148053C5F166CEE3EB34F8524BF09F.GeneratedTextTransformation.Dispose(Boolean disposing) in c:\_dev\T4HostSpecificIssue\TextTemplate2.tt:line 6
   at Microsoft.VisualStudio.TextTemplating.TextTransformation.Dispose()
   at Microsoft.VisualStudio.TextTemplating.TransformationRunner.PerformTransformation()
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

This is new behavior after upgrading from T4 Toolbox 10, where there was no failure.

I don't see any obvious sign of any underlying cause, and am unaware of any diagnostics I can use to get a handle on why the include is failing. Thanks for any help!

Coordinator
Jan 8, 2013 at 4:55 AM
Edited Jan 8, 2013 at 5:36 AM

Thanks for posting a repro. The problem is caused by having two <#@ include file="T4Toolbox.tt" #> directives - one in the TextTemplate1.tt, the other in TextTemplate2.tt. In the next update T4Toolbox will report a warning when it detects multiple includes and ignore the subsequent ones.

Jan 16, 2013 at 11:01 PM
Edited Jan 16, 2013 at 11:03 PM

Funny thing is I put that in there since it wasn't able to find it in the including template otherwise. I'll rework it and see if I can repro that issue.

Jan 17, 2013 at 2:32 AM
Edited Jan 17, 2013 at 4:08 AM

Ok, I got to another repro of what appears to be the issue I was trying to work around. I have a custom Generator and custom Template class which I include in a main Runner template. The reason I was "double-including" the T4Toolbox.tt file is that otherwise it appears that the custom classes don't resolve the base Generator or Template classes, and the entire transform fails.

http://sdrv.ms/VyjKqN

Has the design which used to support this approach changed so as to not support it? Is there simply a more mundane error which is causing problems?

Thanks...

Coordinator
Jan 18, 2013 at 2:24 AM

The .tt files that define reusable Template or Generator classes should not be transformed. Only .tt file that has to be transformed (GeneratorRunner.tt in your example) should be associated with the TextTemplatingFileGenerator custom tool. To fix the transformation errors coming from ClassTemplate.tt, ExtensionsTemplate.tt and GeneratorTemplate.tt simply clear their Custom Tool property using the Properties window. To avoid this problem in the future, you may want to choose the T4 Toolbox project item templates called Template or Generator from the New Project Item dialog. When these items are added to your project they are not associated with the TextTemplatingFileGenerator custom tool by default.

Jan 18, 2013 at 2:42 AM

Ha, yes, you are right I neglected that. My actual project has the custom tool cleared in the item properties, and I'm still getting an error, which I have been trying to repro in a simple and clear way. I'll give it a 3rd go, apparently.

Jan 18, 2013 at 7:35 PM

Ok, I managed to track down the root cause finally. It turns out I was creating a separate AppDomain in which to load and run the Generator runner. It would seem that the TransformationContext doesn't get initialized in a new AppDomain. Since the T4 transform now happens isolated from the VS host, I can now drop this functionality, and as such, the error regarding an uninitialized context no longer appears. Thanks for the support!