MSBuild/m:4失败,因为它构建了两次相同的项目

Jon*_*han 7 msbuild msbuild-4.0

我的团队有一个很大的解决方案(~500 csproj's).我们使用VS2012,并使用TFS Build构建,它使用MSBuild 4.目前我们按顺序构建,但我们想并行构建(使用msbuild /maxcpucount:4).但是,当我在我的4-proc机器上试用它时,我得到了一个奇怪的失败:

11:2>CSC : fatal error CS0042: Unexpected error creating debug information file 'C:\Common\obj\Debug\Common.PDB' -- 'C:\Common\obj\Debug\Common.pdb: The process cannot access the file because it is being used by another process. [C:\Common\Common.csproj]

查看日志,2个msbuild节点正在尝试构建相同的csproj,从而在写入一些输出时发生冲突:

10>Project "C:\Utils\Utils.csproj" (10) is building "C:\Common\Common.csproj" (11) on node 4 (default targets).
46:2>Project "C:\Objects\Objects.csproj" (46:2) is building "C:\Common\Common.csproj" (11:2) on node 1 (default targets).

为什么MSBuild会尝试两次构建同一个项目?

Jon*_*han 5

原因:有人在打电话<MSBuild Projects="Common.csproj" Properties="..." />.然后,MSBuild认为它应该使用这些不同的属性再次构建Common.csproj,并且它恰好与Common.csproj的常规编译同时发生.

修复:调用<MSBuild ... />没有那些不需要的属性.

测试:

Common.targets

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Build">
    <Message Importance="high" Text="Build in $(MSBuildThisFile)" />
  </Target>
  <Target Name="After" DependsOnTargets="Build">
    <Message Importance="high" Text="After in $(MSBuildThisFile)" />
  </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)

Other.targets

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Build">
    <Message Importance="high" Text="Build in $(MSBuildThisFile)" />
    <MSBuild Projects="common.targets" Targets="Build" />   <!-- regular builds -->
    <MSBuild Projects="common.targets"                      <!-- custom invocation with properties -->
             Targets="After"
             Properties="myprop=myvalue"
             />
  </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)

跑:

> msbuild other.targets /clp:verbosity=minimal
  Build in other.targets
  Build in common.targets
  Build in common.targets    <<<< Common.targets Build is invoked again
  After in common.targets
Run Code Online (Sandbox Code Playgroud)

事实上,删除Properties="myprop=myvalue"解决了这个问题.