FileNotFoundException when preprocessing T4 templates from build server

I'm trying to follow the instructions in Microsoft's document Code Generation in a Build Process to rebuild T4 templates on our build server. When I build, template generation is failing with the error (the full MSBuild output is later in this question):

error MSB4018: The "TransformTemplates" task failed unexpectedly.

error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I created a minimal project that reproduces the problem. The script build.cmd simply executes MSBuild. When I run build.cmd from my workstation, the build succeeds. It only fails on the build server.

Things I've tried:

  • Copied the MSBuild and Visual Studio assemblies to the build server, as specified by the Code Generation document.

  • Added the Visual Studio assemblies to the GAC.

  • Set the VSSDK140Install environment variable to "C:Program Files (x86)Microsoft Visual Studio 14.0VSSDK" (because that's how it's set on my workstation).

  • Set HKLMSOFTWAREWOW6432NodeMicrosoftVisualStudio14.0!InstallDir to C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDE

  • I'm trying to avoid installing Visual Studio 2015, because that's a pretty large dependency to add to our build server, and the Microsoft documentation explicitly says this should be possible.

    Copied assemblies to C:Program Files (x86)

    I've copied the following files to the build server, as specified in the Code Generation document (these are all in the Program Files (x86) folder, since that's where they are on my machine):

  • $(ProgramFiles)MSBuildMicrosoftVisualStudiov*.0TextTemplating
  • Microsoft.VisualStudio.TextTemplating.Sdk.Host.14.0.dll
  • Microsoft.TextTemplating.Build.Tasks.dll
  • Microsoft.TextTemplating.targets
  • $(ProgramFiles)Microsoft Visual Studio 14.0VSSDKVisualStudioIntegrationCommonAssembliesv4.0
  • Microsoft.VisualStudio.TextTemplating.14.0.dll
  • Microsoft.VisualStudio.TextTemplating.Interfaces.14.0.dll (several files)
  • Microsoft.VisualStudio.TextTemplating.VSHost.14.0.dll
  • $(ProgramFiles)Microsoft Visual Studio 14.0Common7IDEPublicAssemblies
  • Microsoft.VisualStudio.TextTemplating.Modeling.14.0.dll
  • Added to the GAC

    I saw it's failing trying to load Microsoft.VisualStudio.TextTemplating.14.0.dll , and since that assembly is in the GAC on my workstation I guessed maybe that was required, so I tried using GAC Manager to add Microsoft.VisualStudio.TextTemplating.14.0.dll to the GAC, but the error is the same.

    The full MSBuild output

    When I run build.cmd , I get the following output:

    D:TTTest>"C:Program Files (x86)MSBuild14.0BinMSBuild.exe" ClassLibrary1.sln /t:Rebuild
    Microsoft (R) Build Engine version 14.0.25420.1
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
    Build started 10/20/2017 11:05:16 AM.
    Project "D:TTTestClassLibrary1.sln" on node 1 (Rebuild target(s)).
    ValidateSolutionConfiguration:
      Building solution configuration "Debug|Any CPU".
    Project "D:TTTestClassLibrary1.sln" (1) is building "D:TTTestClassLibrary1ClassLibrary1.csproj" (2) on node 1 (Rebuild target(s)).
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: The "TransformTemplates" task failed unexpectedly. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: File name: 'Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Sdk.Host.GenericTextTemplatingHost..ctor(IServiceProvider serviceProvider) [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Build.Tasks.TransformTemplatesBase.GetConfiguredTextTemplatingHost() [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Build.Tasks.TransformTemplatesBase.Execute() [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018:  [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: === Pre-bind state information === [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: DisplayName = Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018:  (Fully-specified) [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Appbase = file:///C:/Program Files (x86)/MSBuild/14.0/Bin/ [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Initial PrivatePath = NULL [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: Calling assembly : Microsoft.VisualStudio.TextTemplating.Sdk.Host.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: === [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: This bind starts in LoadFrom load context. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load(). [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Using application configuration file: C:Program Files (x86)MSBuild14.0BinMSBuild.exe.Config [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Using host configuration file:  [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.config. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Post-policy reference: Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/14.0/Bin/Microsoft.VisualStudio.TextTemplating.14.0.DLL. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/14.0/Bin/Microsoft.VisualStudio.TextTemplating.14.0/Microsoft.VisualStudio.TextTemplating.14.0.DLL. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/14.0/Bin/Microsoft.VisualStudio.TextTemplating.14.0.EXE. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/14.0/Bin/Microsoft.VisualStudio.TextTemplating.14.0/Microsoft.VisualStudio.TextTemplating.14.0.EXE. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v14.0/TextTemplating/Microsoft.VisualStudio.TextTemplating.14.0.DLL. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v14.0/TextTemplating/Microsoft.VisualStudio.TextTemplating.14.0/Microsoft.VisualStudio.TextTemplating.14.0.DLL. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v14.0/TextTemplating/Microsoft.VisualStudio.TextTemplating.14.0.EXE. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018: LOG: Attempting download of new URL file:///C:/Program Files (x86)/MSBuild/Microsoft/VisualStudio/v14.0/TextTemplating/Microsoft.VisualStudio.TextTemplating.14.0/Microsoft.VisualStudio.TextTemplating.14.0.EXE. [D:TTTestClassLibrary1ClassLibrary1.csproj]
    C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplatingMicrosoft.TextTemplating.targets(396,5): error MSB4018:  [D:TTTestClassLibrary1ClassLibrary1.csproj]
    Done Building Project "D:TTTestClassLibrary1ClassLibrary1.csproj" (Rebuild target(s)) -- FAILED.
    
    Done Building Project "D:TTTestClassLibrary1.sln" (Rebuild target(s)) -- FAILED.
    
    
    Build FAILED.
    

    When I looked in file Microsoft.TextTemplating.targets , found out it uses registry keys to find folders who contain dlls :

     <PropertyGroup Condition=" $(IncludeDslT4Settings)=='true' ">
    <!-- Path to VSCommon7IDE -->
    <!-- Check the 32bit location first ; if that is empty, use the 64bit location. -->
    <VsIdePath>$(Registry:HKEY_LOCAL_MACHINESoftwareMicrosoftVisualStudio14.0@InstallDir)</VsIdePath>
    <VsIdePath Condition=" $(VsIdePath) == ''" >$(Registry:HKEY_LOCAL_MACHINESoftwareWow6432NodeMicrosoftVisualStudio14.0@InstallDir)</VsIdePath>
    
    <!-- Set the default location for the Dsl Designer install. -->
    <DslDesignerInstallPath Condition=" $(DslDesignerInstallPath)=='' ">$(VsIdePath)ExtensionsMicrosoftDSL SDKDsl Designer$(VisualStudioVersion)</DslDesignerInstallPath>
    
    <!-- Add the standard DSL templates folder -->
    <IncludeFolders>$(IncludeFolders);$(DslDesignerInstallPath)TextTemplates</IncludeFolders>
    

    So I have to install Microsoft Visual Studio 2015 SDK and Microsoft Visual Studio 2015 Visualization & Modeling SDK on build server to registry DLLs.

    However, Starting in Visual Studio 2015 , you do not install the Visual Studio SDK from the download center. It is included as an optional feature in Visual Studio setup . So we could not install Visual Studio SDK without installing Visual Studio. see Visual Studio SDK for detail info.

    Besides, Daniel's has a blog about How to transform T4 templates on build without installing a Visual Studio SDK, you can check if that give any helps.


    The problem seemed to be that .NET could not find the assembly Microsoft.VisualStudio.TextTemplating.14.0.dll .

    So instead of keeping everything in separate folders as Visual Studio installs them, I took the following files:

  • C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplating
  • Microsoft.VisualStudio.TextTemplating.Sdk.Host.14.0.dll
  • Microsoft.TextTemplating.Build.Tasks.dll
  • Microsoft.TextTemplating.targets
  • C:Program Files (x86)Microsoft Visual Studio 14.0VSSDKVisualStudioIntegrationCommonAssembliesv4.0
  • Microsoft.VisualStudio.TextTemplating.14.0.dll
  • Microsoft.VisualStudio.TextTemplating.Interfaces.14.0.dll (several files)
  • Microsoft.VisualStudio.TextTemplating.VSHost.14.0.dll
  • C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDEPublicAssemblies
  • Microsoft.VisualStudio.TextTemplating.Modeling.14.0.dll
  • And copied them all into the MSBuild task directory:

  • C:Program Files (x86)MSBuildMicrosoftVisualStudiov14.0TextTemplating
  • After doing this, .NET was able to find the assemblies it needed and transformed the templates.

    链接地址: http://www.djcxy.com/p/92432.html

    上一篇: 引用的DLL不复制到输出bin

    下一篇: 从生成服务器预处理T4模板时发生FileNotFoundException