无法使用VS 2019构建Web应用程序

时间:2019-09-09 15:03:23

标签: visual-studio msbuild visual-studio-2019

我安装了VS 2019 Pro并试图编译我们的解决方案以获取此信息:

error MSB4226: The imported project "C:\Program Files (x86)\Microsoft
 Visual Studio\2019\Professional\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found. Also, tried
 to find "WebApplications\Microsoft.WebApplication.targets" in the fallback search path(s) for $(VSToolsPath) - "C:\Program Files (x86)\MSBuild\Mi
crosoft\VisualStudio\v15.0" . These search paths are defined in "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\
Bin\MSBuild.exe.Config". Confirm that the path in the <Import> declaration is correct, and that the file exists on disk in one of the search paths

这就是它发生的原因:

  1. 我们的所有项目均包含以下导入语句:<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props" Condition="'$(VSToolsPath)' == ''" />
  2. MSBuildExtensionsPath = C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild
  3. MSBuildToolsVersion = Current
  4. 文件夹$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)确实存在,请参见下文。

$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)

C:\> dir "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current"


    Directory: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        6/13/2019   9:11 PM                Bin
d-----        5/27/2019   4:42 PM                Imports
d-----        5/27/2019   4:41 PM                Microsoft.Common.targets
d-----        5/27/2019   4:42 PM                SolutionFile
-a----        5/27/2019   4:38 PM          13606 Microsoft.Common.props
-a----        5/27/2019   4:38 PM            789 Microsoft.VisualStudioVersion.v15.Common.props
-a----        5/27/2019   4:38 PM           2029 THIRDPARTYNOTICES.txt


C:\>

因此,VS 2019实际上会加载Microsoft.VisualStudioVersion.v15.Common.props。那里面是什么?观察:

C:\> cat "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Microsoft.VisualStudioVersion.v15.Common.props"
<!--
***********************************************************************************************
Microsoft.VisualStudio.v15.Common.props

WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
          created a backup copy.  Incorrect changes to this file will make it
          impossible to load or build your projects from the command-line or the IDE.

Copyright (C) Microsoft Corporation. All rights reserved.
***********************************************************************************************
-->

<Project>

  <PropertyGroup>
    <VisualStudioVersion>15.0</VisualStudioVersion>
    <VSToolsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>

</Project>
C:\>

因此,最后VSToolsPathC:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VisualStudio\v15.0,这显然是错误的,因为不存在这样的目录。确实存在的是v16.0。

无论如何,似乎所有这一切的根本原因是c:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild向全世界传达了要使用MSBuild 15而不是MSBuild 16的版本。

我想念什么?

2 个答案:

答案 0 :(得分:0)

  

我们的所有项目均包含以下导入语句:<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props" Condition="'$(VSToolsPath)' == ''" />

我认为问题的根本原因是您将语句放在VSToolsPath属性的定义上方

在VS2019中创建一个简单的asp.net Web应用程序(.net框架),其结构如下所示:

<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
  </PropertyGroup>

  <ItemGroup>
    ...
  </ItemGroup>

  <!--Position 1-->
  <!--<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props" Condition="'$(VSToolsPath)' == ''" />--> <!--Position 1-->
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <!--This is where the VSToolsPath is defined.-->
  </PropertyGroup>

  <!--Position 2-->
  <!--<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props" Condition="'$(VSToolsPath)' == ''" />--> <!--Position 2-->
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" /> <!--This is where the error finally occurs.-->
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
  <ProjectExtensions>
    ...
  </ProjectExtensions>
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. ...-->
</Project>

我假设您可以将语句添加到xx.csproj文件的顶部或

的上方
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

如果是这样,从现在开始,尚未定义$(VSToolsPath)。它的值为空,然后构建系统将在您的语句中执行Import元素以导入$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.VisualStudioVersion.v*.Common.props文件,然后将VisualStudioVersion属性设置为15.0而不是16.0。

并且由于它从Microsoft.VisualStudioVersion.v15.Common.props导入属性,因此VSToolsPath现在被设置为$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0,这将导致由于到达{{1 }}文件为$(VSToolsPath)。参见:

Microsoft.WebApplication.targets

因此,如果将语句添加到$(VSToolsPath)的第一个定义(如位置1 )上方,则会出现此错误。如果将语句添加到$(VSToolsPath)的定义下方和<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" /> <!--This is where the error finally occurs.--> 上方,例如位置2 ,则一切正常。

更新1:

关于Import元素的信息,请参见this document 导入的项目按照导入的顺序进行检查。

Update2:

标记的提示,我在msbuild中发布了一个问题。 The link

答案 1 :(得分:0)

对我来说,以下是有效的。

在 csproj 文件中,

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" />

被替换为

<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" />