Error MSB4226 Microsoft.TextTemplating.targets not imported

Problem

You have a project that is using text templating (such as for auto-incrementing the version number) and you’re upgrading to a new version of Visual Studio. When you open the project, you get error messages about not being able to import the Microsoft.TextTemplating.targets project:

Project “…\v16.0\TextTemplating\Microsoft.TextTemplating.targets” was not imported by “…SomeProject.csproj” at (7,3), due to the file not existing.

The error might appear with slightly different wording:

MSB4226 The imported project “…\v16.0\TextTemplating\Microsoft.TextTemplating.targets” was not found

The import project path in the .csproj file is wrong.

Solution

Microsoft.TextTemplating.targets is located in the Visual Studio installation directory. If you hardcoded the Visual Studio version number in the import path, then when you try to open the project in a different version of Visual Studio, the import path will be pointing to a non-existent directory.

To fix this, edit your project .csproj file and look for the Microsoft.TextTemplating.targets import project line:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
    </PropertyGroup>
    <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v16.0\TextTemplating\Microsoft.TextTemplating.targets" />
    <!-- rest of file -->
</Project>
Code language: HTML, XML (xml)

Update the version in the path (ex: from v16.0 to v17.0):

"$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets"Code language: JSON / JSON with Comments (json)

Or use the VisualStudioVersion variable instead of hardcoding the version:

"$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets"Code language: JSON / JSON with Comments (json)

Note: This variable might not be populated in older versions of Visual Studio.

Comments are closed.