locked
T4 not compiling after VS 2010 SDK was installed RRS feed

  • Question

  • Hi,

    I have a Sample.tt in a C# class library project in a VS 2010 solution. A snippet from this:

    <#@ template language="C#" debug="false" hostspecific="true"#>
    <#@ include file="EF.Utility.CS.ttinclude"#><#@
     output extension=".cs"#>

    This was compiling fine. The trouble began when I installed VS 2010 SDK. I did this in order to add an MSBUILD directive to automate the T4 generation. I was following instructions from here http://msdn.microsoft.com/en-us/library/ee847423.aspx.

    Soon after installing the SDK the Sample.tt, which was compiling fine, now reports the following compilation errors:

    C:\Temp\Sample.tt(1,1): error CS0246: The type or namespace name 'EnvDTE' could not be found (are you missing a using directive or an assembly reference?)
    C:\Temp\Sample.tt(1,1): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

    The really bad news is that uninstalling the VS 2010 SDK and the VS Visualisation and Modelling SDK does not fix the problem!

    Is anyone able to help with this?

    Thanks!

    Tuesday, January 25, 2011 7:52 AM

Answers

  • Hello Noel.

    I did what you proposed, but cannot repro your problem: When I reset the build action of Model1.tt to None, this is accepted. Would you have anything specific to your project / VS Installation?

    In order to unblock you, a work-around would be to edit directly the .csproj project to change back the build action,and I'm aware this is not a great answer, but since I cannot repro your problem for the moment without more information, I don't undestand what happens to your VS. I suggest that you post a bug on http://connect.microsoft.com/visualstudio

    Just thinking of it, could you please tell me if you would be using a localized version of visual studio?

     

    Thanks!
    Best regards

     


    Jean-Marc
    Friday, January 28, 2011 9:27 AM

All replies

  • Really weird errors at line 1 are sometimes caused by mismatched {brackets} in the template - did you change the .ttinclude file?

    Try setting debug="true". Double-click the error in the window, and it will show the C# code.


    - Alan -MSFT
    Tuesday, January 25, 2011 11:28 AM
  • Hi, Alan, I did further experimentation and now I have reduced the code in the .tt file to the bare minimum:

    <#@ template language="C#" debug="true" hostspecific="true"#>
    <#@ output extension=".cs"#>

    Let me reiterate: there is nothing else in the .tt file. But I still get

    "C:\Temp\Sample.tt(1,1): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)"

    Furthermore, the following strange behaviours also apply to the issue:

    1. When the .tt file is not actually open in the VS solution and I try to build the project containing the .tt file I get different errors: "Preprocessor directives must appear as the first non-whitespace character on a line"

    2. The actual T4 code generation seems to work fine. If I right-click on the .tt file and select "Run Custom Tool" then the required classes are generated correctly by the T4 code. It's only the compilation of the project that is failing.

    3. Following on from that if I try to set the build action for the .tt file to "None" and then try to compile, it is resetting back to "Compile", i.e. I am not able to set build action = "None".

    What's going on?

    NB: Setting debug="true" and double-clicking on the error does not show me any C# code. I just keep getting directed back to the .tt file.

    Wednesday, January 26, 2011 8:35 AM
  • Sounds like there's some problem with the template file's properties in your .csproj file. It's trying to compile the .tt file as if it were a .cs file, by the sound of it. Build Action for a template should be None. Just to check - silly question perhaps - the template file definitely has the extension ".tt"?

    First thing I'd try is to Add>New Item>Text Template, and then copy the content of the template into the new file. If that doesn't improve matters, create a new project and copy the bits across.

    I'm assuming we're talking about a regular (not preprocessed) text template here, because you wouldn't have an output directive in a preprocessed one.

    For a regular (not preprocessed) text template, the properties should be:

    • Build Action: None
    • Copy to Output Dir: Do not copy
    • Custom Tool: TextTemplatingFileGenerator
    • Custom Tool Namespace:

    If you open the .csproj in notepad (or in VS, Unload the project and then Edit it). For a text template, you should see an entry like:

        <None Include="TextTemplate2.tt">
          <Generator>TextTemplatingFileGenerator</Generator>
          <LastGenOutput>TextTemplate2.cs</LastGenOutput>
        </None>

    There will also be an entry for its generated file:

        <Compile Include="TextTemplate2.cs">
          <AutoGen>True</AutoGen>
          <DesignTime>True</DesignTime>
          <DependentUpon>TextTemplate2.tt</DependentUpon>
        </Compile>

    (BTW, for future reference - with debug="true", you can find the compiled version of your regular text template in c:\Users\<you>\AppData\Local\Temp\


    - Alan -MSFT
    Wednesday, January 26, 2011 10:40 AM
  • Hi, Alan, the problem can be reduced to the following "Setting Build Action = None via the VS property dialogue does not work for .tt files".

    (I had already tried your other suggestions: new file, new project etc. and that did not work. And yes, the file does have the .tt extension. I assume this is a regular .tt because I just added it via "Add new item" and selecting "ADO.NET POCO Entity Generator".)

    While we were discussing this I had got this to work by manually editing the .csproj and adding Condition="1=0" to the ItemGroup containing the .tt file. I found that was the only way to get VS not to try compiling the .tt file.

    Now I also find that manually editing the .csproj and changing the .tt element from "Compile" to "None" also seems to work.

    But the strange thing is I again used the property editor in VS and set Build Action to "None" then opened up the .csproj and checked to see whether it was reflecting changes in the XML and it was! But when I ran the build and then checked the property box, the Build Action had changed back to "Compile".

    There is a bug somewhere, which seems to be related to installing the VS 2010 SDK.

    Thursday, January 27, 2011 8:49 AM
  • Hello Noel.

    My understanding is that you intend to used the T4 MsBuild feature.

    - You installed the VSSDK, but did you install the VsVmSdk? I've both installed on my machine and cannot repro your problem?

    - which Custom tool are you using for the T4 file?

    Thanks!
    Best regards


    Jean-Marc
    Thursday, January 27, 2011 10:40 AM
  • Hi, Jean-Marc, I did install both the VS SDK and the VS Visual and Modeling SDK. The custom tool I am using is "TextTemplatingFileGenerator".

    Although my intention was to use the T4 msbuild feature I didn't get around to making any changes to the .csproj file.

    Installing VSSDK and VsVmSDK just caused some strange behaviour is VS 2010 (described in my posts above).

    Thursday, January 27, 2011 1:41 PM
  • Let me reformulate what I understand:

    if you:

    - create a new console project

    - add a text template to it

    - save the .tt

    then you will have the error you describe?

     


    Jean-Marc
    Thursday, January 27, 2011 1:45 PM
  • Hi, Jean-Marc, I have been able to reproduce the problem as follows:

    • Create a console project.
    • Go Add >> New Item >> Visual C# Items >> Code >> ADO.Net Poco Entity Generator.
    • Open up Model1.Context.tt and Model1.tt, locate "string inputFile" and assign to the path of an existing .edmx file.
    • Compile the project and it should compile fine.
    • Right-click on Model1.tt, select "Properties", and set "Build Action" to "Compile".
    • Build the project and, as expected, you should get compile errors.
    • Right-click on Model1.tt again, select "Properties", and set "Build Action" to "None".
    • Compile the project, but this time you will still get the same errors.
    • Check the "Build Action" and it will have reset to "Compile".

    After this all attempts to set "Build Action" to "None" will fail.

    Friday, January 28, 2011 6:32 AM
  • Hello Noel.

    I did what you proposed, but cannot repro your problem: When I reset the build action of Model1.tt to None, this is accepted. Would you have anything specific to your project / VS Installation?

    In order to unblock you, a work-around would be to edit directly the .csproj project to change back the build action,and I'm aware this is not a great answer, but since I cannot repro your problem for the moment without more information, I don't undestand what happens to your VS. I suggest that you post a bug on http://connect.microsoft.com/visualstudio

    Just thinking of it, could you please tell me if you would be using a localized version of visual studio?

     

    Thanks!
    Best regards

     


    Jean-Marc
    Friday, January 28, 2011 9:27 AM
  • Hi Noel,

    I know this thread is old but I just came across it because I was having the exact same problem and I found the cause (for me at least). It seems the culprit had to do with specific VS extensions I had installed recently (or one in particular). The Build Action kept switching back to Compile. One of them was "Visual T4" by Clarius which gave me color syntax and intellisense and a couple other features. I just simply disabled the extension, restarted VS and the problem went away - the Build Action was set to None (automatically I think), and stayed there, and everything compiled fine.

    I'm not sure if it's a problem with "Visual T4" or another extension it was conflicting with - but I really like the editing features. It looks like I'll have to enable/disable this extension and restart VS whenever I want to use the extension or build.

     

    -Brad

     

     

    • Proposed as answer by BJ Shark Wednesday, March 30, 2011 5:05 PM
    Wednesday, March 30, 2011 5:05 PM
  • I can confirm it's Visual T4 that's the cause of the build action switching back to Compile, and that you have to edit the .csproj by hand to fix it.
    Thursday, June 23, 2011 9:08 PM
  • Clarius claim the latest update build has fixed this issue, so you might want to try that.
    Gareth Jones - Developer Architect - T4, UML Designer Extensibility [MSFT]
    Monday, June 27, 2011 2:40 PM
  • I just started using T4 templates during last week, which I think are just awsome, along with Clarius's Visual T4, which I also think it is a great product.

    Still i am having an intermitent "Preprocessor directives must appear as the first non-whitespace character on a line" compile error, which prevents my project from running. I have spend a very frustating weekend trying to understand what is, after all, a hideous bug!

    Beside changing template Build Action from Compile to Content, which solves my problem, I have found another workaround: just open "offending tample" and recompile it. Unfortunatelly, if you have X templates you will have to open and recompile all of them everytime you open your project.

    It seems pre-processed templates (.tt) validation only works fine after editing.

    I am currently using version 1.0.50629.0 of Visual T4. Is there a more recent version?


    Sunday, July 8, 2012 2:59 PM