Visual Studio 2017 扩展 - VSToolsPath 不工作
Visual Studio 2017 extension - VSToolsPath not working
我正在为 VS 2017 更新一个旧的 Visual Studio 扩展。它从 Visual Studio 和 msbuild 编译良好,在我的本地计算机上调试和发布。
这是我正在使用的 msbuild 命令行:
msbuild VxCop.sln /p:ToolsHome=C:\ProgramData\chocolatey\bin /p:Configuration=Release /p:Platform="Any CPU"
但是,在构建机器 (TFS Build 2010) 上使用相同的命令行调用 msbuild.exe 它失败并显示 this error
为了解决这个问题,我尝试指定 VSToolsPath。我尝试了各种方法,例如更改 .csproj 中的 VSToolsPath 条目(由于这样做没有效果,因此似乎没有考虑在内)并在命令行上传递它:
msbuild VxCop.sln /p:ToolsHome=C:\ProgramData\chocolatey\bin /p:Configuration=Release /p:Platform="Any CPU" /p:VSToolsPath=Packages\Microsoft.VSSDK.BuildTools.15.1.192\tools\
这会导致一个非常奇怪的错误:
CopyFilesToOutputDirectory:
Copying file from "obj\Release\SymCop.dll" to "bin\Release\SymCop.dll".
SymCop -> H:\src\tools\VisualStudioExtensions\Main\VxCop\source\SymCop\bin\Release\SymCop.dll
Copying file from "obj\Release\SymCop.pdb" to "bin\Release\SymCop.pdb".
Done Building Project "H:\src\tools\VisualStudioExtensions\Main\VxCop\source\SymCop\SymCop.csproj" (default targets).
Done Building Project "H:\src\tools\VisualStudioExtensions\Main\VxCop\VxCop.sln" (Build target(s)) -- FAILED.
Done Building Project "H:\src\tools\VisualStudioExtensions\Main\VxCop\build.proj" (default targets) -- FAILED.
Build FAILED.
0 Warning(s)
0 Error(s)
实际的扩展项目根本没有出现在日志中,而且没有,您知道,错误。但是构建 returns 失败了,return 代码是非零的,而且 vsix 项目似乎没有构建(它的输出丢失)
希望大家提点建议
谢谢
编辑:
对于以后阅读本文的人来说,问题似乎是同一文件中的 <Import>
不关心我对 $(VSToolsPath)
的更新。
更改导入修复它:
<Import Project="$(SolutionDir)\packages\Microsoft.VSSDK.BuildTools.15.1.192\tools\VSSDK\Microsoft.VsSDK.targets"
/>
Visual Studio 2017 extension - VSToolsPath not working
根据您的脚本,我得到了与您相同的结果。将NuGet包Microsoft.VSSDK.BuildTools
安装到项目后,Microsoft.VSSDK.BuildTools.props
会被导入到项目文件中,打开项目文件,可以在下面找到Import
:
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.props" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.props')" />
然后打开这个道具文件,你可以看到下面的脚本片段:
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="VSSDK_NuGet_Configuration">
<ThisPackageDirectory>$(MSBuildThisFileDirectory)..\</ThisPackageDirectory>
<VSToolsPath>$(ThisPackageDirectory)\tools</VSToolsPath>
<VsSDKInstall>$(VSToolsPath)\VSSDK</VsSDKInstall>
<VsSDKIncludes>$(VsSDKInstall)\inc</VsSDKIncludes>
<VsSDKToolsPath>$(VsSDKInstall)\bin</VsSDKToolsPath>
</PropertyGroup>
</Project>
在这种情况下,NuGet 包将值 VSToolsPath
覆盖为 $(ThisPackageDirectory)\tools
。所以MSBuild会跳过在项目文件中设置下一步的值设置:
<PropertyGroup>
<MinimumVisualStudioVersion>15.0</MinimumVisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
因为 NuGet 已经设置了值 $(VSToolsPath)
,Condition="'$(VSToolsPath)' == ''"
的值将是 False。此外,您可以添加一个目标来检查该值是否已设置,例如:
<Target Name="CheckVSToolsPath" BeforeTargets="Build">
<Message Text="$(VSToolsPath)"></Message>
</Target>
您会发现此值设置为:
C:\Users\Admin\Documents\Visual Studio 2017\Projects\VSIXProject2\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\..\tools
上面的总结,VSToolsPath
的值被正确导入,我们不需要在命令行上传递它。
经过深入排查,我找到了之前错误"MSB4226: The imported project "(...)\VSSDK\Microsoft.VsSDK.targets"找不到的原因。"是 "VisualStudioVersion" 的 MSBuild 属性 没有在构建服务器上设置 。
有关详细信息,请参阅下文 link Building a VSIX extension with the Visual Studio 2017 Build Tools:
something that a machine with the full Visual Studio 2017 does and that a machine with the Build Tools 2017 does if you open a developer command prompt. Since I was not using it, I passed it as a parameter to the MSBuild script. It can be defined too inside the .csproj file, something that previous Visual Studio versions did automatically but recent versions don’t.
所以要解决错误 "MSBuild4226",您应该在命令行上传递 visual studio 版本:
msbuild VxCop.sln /p:ToolsHome=C:\ProgramData\chocolatey\bin /p:Configuration=Release /p:Platform="Any CPU" /p:VisualStudioVersion=15.0
使用此命令行后,错误 MSBuild 4226 已解决。
希望对您有所帮助。
在 VS 2019 中解决了这个问题
<ItemGroup>
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="16.10.1055">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(PkgMicrosoft_VSSDK_BuildTools)\tools\vssdk\Microsoft.VsSDK.targets" />
我正在为 VS 2017 更新一个旧的 Visual Studio 扩展。它从 Visual Studio 和 msbuild 编译良好,在我的本地计算机上调试和发布。
这是我正在使用的 msbuild 命令行:
msbuild VxCop.sln /p:ToolsHome=C:\ProgramData\chocolatey\bin /p:Configuration=Release /p:Platform="Any CPU"
但是,在构建机器 (TFS Build 2010) 上使用相同的命令行调用 msbuild.exe 它失败并显示 this error
为了解决这个问题,我尝试指定 VSToolsPath。我尝试了各种方法,例如更改 .csproj 中的 VSToolsPath 条目(由于这样做没有效果,因此似乎没有考虑在内)并在命令行上传递它:
msbuild VxCop.sln /p:ToolsHome=C:\ProgramData\chocolatey\bin /p:Configuration=Release /p:Platform="Any CPU" /p:VSToolsPath=Packages\Microsoft.VSSDK.BuildTools.15.1.192\tools\
这会导致一个非常奇怪的错误:
CopyFilesToOutputDirectory:
Copying file from "obj\Release\SymCop.dll" to "bin\Release\SymCop.dll".
SymCop -> H:\src\tools\VisualStudioExtensions\Main\VxCop\source\SymCop\bin\Release\SymCop.dll
Copying file from "obj\Release\SymCop.pdb" to "bin\Release\SymCop.pdb".
Done Building Project "H:\src\tools\VisualStudioExtensions\Main\VxCop\source\SymCop\SymCop.csproj" (default targets).
Done Building Project "H:\src\tools\VisualStudioExtensions\Main\VxCop\VxCop.sln" (Build target(s)) -- FAILED.
Done Building Project "H:\src\tools\VisualStudioExtensions\Main\VxCop\build.proj" (default targets) -- FAILED.
Build FAILED.
0 Warning(s)
0 Error(s)
实际的扩展项目根本没有出现在日志中,而且没有,您知道,错误。但是构建 returns 失败了,return 代码是非零的,而且 vsix 项目似乎没有构建(它的输出丢失)
希望大家提点建议
谢谢
编辑:
对于以后阅读本文的人来说,问题似乎是同一文件中的 <Import>
不关心我对 $(VSToolsPath)
的更新。
更改导入修复它:
<Import Project="$(SolutionDir)\packages\Microsoft.VSSDK.BuildTools.15.1.192\tools\VSSDK\Microsoft.VsSDK.targets"
/>
Visual Studio 2017 extension - VSToolsPath not working
根据您的脚本,我得到了与您相同的结果。将NuGet包Microsoft.VSSDK.BuildTools
安装到项目后,Microsoft.VSSDK.BuildTools.props
会被导入到项目文件中,打开项目文件,可以在下面找到Import
:
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.props" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.props')" />
然后打开这个道具文件,你可以看到下面的脚本片段:
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="VSSDK_NuGet_Configuration">
<ThisPackageDirectory>$(MSBuildThisFileDirectory)..\</ThisPackageDirectory>
<VSToolsPath>$(ThisPackageDirectory)\tools</VSToolsPath>
<VsSDKInstall>$(VSToolsPath)\VSSDK</VsSDKInstall>
<VsSDKIncludes>$(VsSDKInstall)\inc</VsSDKIncludes>
<VsSDKToolsPath>$(VsSDKInstall)\bin</VsSDKToolsPath>
</PropertyGroup>
</Project>
在这种情况下,NuGet 包将值 VSToolsPath
覆盖为 $(ThisPackageDirectory)\tools
。所以MSBuild会跳过在项目文件中设置下一步的值设置:
<PropertyGroup>
<MinimumVisualStudioVersion>15.0</MinimumVisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
因为 NuGet 已经设置了值 $(VSToolsPath)
,Condition="'$(VSToolsPath)' == ''"
的值将是 False。此外,您可以添加一个目标来检查该值是否已设置,例如:
<Target Name="CheckVSToolsPath" BeforeTargets="Build">
<Message Text="$(VSToolsPath)"></Message>
</Target>
您会发现此值设置为:
C:\Users\Admin\Documents\Visual Studio 2017\Projects\VSIXProject2\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\..\tools
上面的总结,VSToolsPath
的值被正确导入,我们不需要在命令行上传递它。
经过深入排查,我找到了之前错误"MSB4226: The imported project "(...)\VSSDK\Microsoft.VsSDK.targets"找不到的原因。"是 "VisualStudioVersion" 的 MSBuild 属性 没有在构建服务器上设置 。
有关详细信息,请参阅下文 link Building a VSIX extension with the Visual Studio 2017 Build Tools:
something that a machine with the full Visual Studio 2017 does and that a machine with the Build Tools 2017 does if you open a developer command prompt. Since I was not using it, I passed it as a parameter to the MSBuild script. It can be defined too inside the .csproj file, something that previous Visual Studio versions did automatically but recent versions don’t.
所以要解决错误 "MSBuild4226",您应该在命令行上传递 visual studio 版本:
msbuild VxCop.sln /p:ToolsHome=C:\ProgramData\chocolatey\bin /p:Configuration=Release /p:Platform="Any CPU" /p:VisualStudioVersion=15.0
使用此命令行后,错误 MSBuild 4226 已解决。
希望对您有所帮助。
<ItemGroup>
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="16.10.1055">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(PkgMicrosoft_VSSDK_BuildTools)\tools\vssdk\Microsoft.VsSDK.targets" />