2

I am working with a web application that was written using VS2015, and is being maintained using VS2017. I am trying to write another application to build the full web stack locally using the MSBuild API and other tools. In VS2015 or VS2017 the ASP.NET Web Application project will build successfully, but when running MSBuild programmatically, I keep getting this error:

The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

I have the following build packages installed in my app:

  • Microsoft.Build
  • Microsoft.Build.Framework
  • Microsoft.Build.Tasks.Core
  • Microsoft.Build.Utilities.Core

The standard advice I've seen in forums for this error is to install Visual Studio on the build server, but I am doing this locally and I do have Visual Studio installed. I've also read that MSBuild 15 does not come with the WebApplication.targets file. There is also a toolsVersion parameter on the constructor for Microsoft.Build.Execution.BuildRequestData that I've tried setting manually to 14.0 but it still seems like my app is trying to use MSBuild 15. (I do have MSBuild 14 installed.)

Questions:

  • Can I make this build run in MSBuild 14 programmatically without updating any csproj files?
  • Where can I get WebApplication.targets for MSBuild 15?

Solution:

Thanks in large part to @Leo-MSFT I was able to get this working. Here's how:

  1. Uninstalled the VS2017 ASP.NET and Web Application Development workload, then reinstalled with all of its optional components. This downloaded the missing .targets file.
  2. In my builder application, added this property to my instance of BuildRequestData to make MSBuild look in the folders used by v15, rather than using the folders used by v14.

    ["MSBuildExtensionsPath32"] = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild"

JamesFaix
  • 8,050
  • 9
  • 37
  • 73
  • Using vs_buildtools__XXX.exe GUI installer fixed it for me with above recommendations. I had the following error "error MSB4057: The target "Package" does not exist in the project." – ChristoD Nov 22 '18 at 12:10
  • @JamesFaix How's that go w/ #2? I'm using Jenkins to call MSBuild v15. Don't entirely understand where you're placing that `["MSBuildExtensionsPath32"]` property? – fusion27 May 10 '19 at 16:23

1 Answers1

1

Can I make this build run in MSBuild 14 programmatically without updating any csproj files?

MSBuildExtensionsPath32 is set internally by MSBuild. If you do not want update you .csproj file, you can try to override the value in your project file:

  <PropertyGroup>
    <MSBuildExtensionsPath32>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild</MSBuildExtensionsPath32>
  </PropertyGroup>

  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />

But I'm not sure if it will introduce other error(Not tested).

Where can I get WebApplication.targets for MSBuild 15?

The path of WebApplication.targets for MSBuild 15 is:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • I know that that is the path where the targets should be, but I do not have them. The general instructions I've seen are to install Visual Studio, but I think this does not apply to the 2017 version. – JamesFaix Nov 03 '17 at 14:37
  • 1
    @JamesFaix, since you have installed Visual Studio 2017 but not have that targets, you should also install the Workloads `ASP.NET and web development` by Installer. – Leo Liu Nov 03 '17 at 14:58
  • 1
    @JamesFaix, how about this issue after install the Workloads ASP.NET and web development by Installer? – Leo Liu Nov 06 '17 at 10:14
  • I just opened up the installer and I already have that workload. I will try removing it and re-adding. – JamesFaix Nov 06 '17 at 23:25
  • I uninstalled the "ASP.NET and web development" workload and then reinstalled with all of its optional components. The targets file is now where its supposed to be and my build application works properly. I think this may have been part of the .NET 4.7 SDK component that I did not have before. – JamesFaix Nov 07 '17 at 00:11
  • @JamesFaix, I have not installed the .NET 4.7 SDK component in my machine, so the .NET 4.7 SDK component is not root cause of this issue. https://stackoverflow.com/questions/44061932/ms-build-2017-microsoft-webapplication-targets-is-missing. Anyway, re-install "ASP.NET and web development" and its optional components can resolve this issue, you can convert you comment as answer, then mark it. Or you can mark above answer if it help you, so it could help other community members who get the same issues. Thanks. – Leo Liu Nov 07 '17 at 02:09
  • 1
    Actually I spoke too soon, the targets file is now in the directory mentioned above (`Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\15.0\WebApplications`) but MSBuild is looking for the file in `C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications` – JamesFaix Nov 07 '17 at 02:57
  • @JamesFaix, just like what I have said in the answer, if you do not want update you .csproj file, you can try to override the value in your project file: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild . Because the value of `$(MSBuildExtensionsPath)` was changed to `C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild` for Visual Studio 2017. It seems your projects still use that value for Visual Studio 2015. – Leo Liu Nov 07 '17 at 03:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158366/discussion-between-jamesfaix-and-leo-msft). – JamesFaix Nov 07 '17 at 03:09
  • Reinstalling the Web workload did not help me. I had to manually copy the `WebApplications` directory to `Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0`. – alekop Dec 13 '19 at 22:40