Using Text Transform (.tt) templates in GitHub Actions
Asked Answered
M

1

6

I am trying to use Text Transform templates (a feature of Visual Studio) with GitHub Actions. I am specifically using the Windows runner, which has Visual Studio 2022 Enterprise installed, so the dependency should be there.

In my .csproj file I have set up the configuration as follows:

<PropertyGroup>
    <CustomPathToTransforms>$(MSBuildExtensionsPath)</CustomPathToTransforms>
</PropertyGroup>
    
<Import Project="$(CustomPathToTransforms)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />

Then, I also make sure that the transform occurs during build:

<PropertyGroup>
    <TransformOnBuild>true</TransformOnBuild>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>

Locally, this works fine with Visual Studio 2022 Community edition. The reason that I added another property (CustomPathToTransforms) is that so in GitHub Actions I can redirect it to the Visual Studio installation folder instead of the .NET tooling folder, that would otherwise be picked up with MSBuildExtensionsPath.

In GitHub Actions, I am running the build like this:

dotnet build --configuration Release -p:CustomPathToTransforms="C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\"

In theory, this should work, and I can tell from the build output that the targets are found correctly, however the step fails rather spectacularly:

C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets(347,5): error MSB4018: The "TransformTemplates" task failed unexpectedly. [D:\a\grunt\grunt\Grunt\Grunt\Grunt.csproj]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets(347,5): error MSB4018: System.TypeLoadException: Could not load type 'System.Windows.DependencyObject' from assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. [D:\a\grunt\grunt\Grunt\Grunt\Grunt.csproj]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets(347,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Sdk.Host.GenericTextTemplatingHost.InternalConstruct() [D:\a\grunt\grunt\Grunt\Grunt\Grunt.csproj]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets(347,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Sdk.Host.GenericTextTemplatingHost..ctor(IServiceProvider serviceProvider, ITelemetryService telemetryService) [D:\a\grunt\grunt\Grunt\Grunt\Grunt.csproj]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets(347,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Build.Tasks.TransformTemplatesBase.GetConfiguredTextTemplatingHost() [D:\a\grunt\grunt\Grunt\Grunt\Grunt.csproj]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets(347,5): error MSB4018:    at Microsoft.VisualStudio.TextTemplating.Build.Tasks.TransformTemplatesBase.Execute() [D:\a\grunt\grunt\Grunt\Grunt\Grunt.csproj]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets(347,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [D:\a\grunt\grunt\Grunt\Grunt\Grunt.csproj]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Msbuild\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets(347,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [D:\a\grunt\grunt\Grunt\Grunt\Grunt.csproj]
Build FAILED.

This is a .NET 6.0 project that I want to make cross-platform (compiled through VS, though).

I am curious - what can I do to resolve the issue? Is there a way to make the process identify dependencies which I assume are already in the GAC?

Misfit answered 17/6, 2022 at 23:20 Comment(1)
Did you ever get this working?Tager
T
0

Might not be the answer you are looking for, but I would advice adding a Condition to both the Import and the PropertyGroup to only execute this on debug mode (locally) and thus not when Releasing on a build server.

Not only does it fix the build on the build server, it also makes sure no wierd file-adjustments are made after the code has been reviewed via a PR, I wouldn't want to have code generated on the fly when pushing something to Staging/Production. Let's not empower AI of this...yet :)

Example:

<Import Condition="'$(Configuration)' == 'Debug'" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TransformOnBuild>true</TransformOnBuild>
    <TransformOutOfDateOnly>true</TransformOutOfDateOnly>
</PropertyGroup>
Tosspot answered 25/8, 2023 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.