MSBuild /m:4 fails because it builds the same project twice

前提是你 提交于 2020-01-03 08:55:28

问题


My team has a large solution (~500 csproj's). We use VS2012, and build using TFS Build, which uses MSBuild 4. Currently we build serially, but we want to build in parallel (using msbuild /maxcpucount:4). However, when I try it on my 4-proc machine, I get a weird failure:

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]

Looking at the log, 2 msbuild nodes were trying to build that same csproj, and thus colliding on writing some output:

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).

Why would MSBuild try to build the same project twice?


回答1:


Cause: Someone was calling <MSBuild Projects="Common.csproj" Properties="..." />. Then, MSBuild thinks that it should build Common.csproj again with those different properties, and it happened to occur at the same time with the regular compilation of Common.csproj.

Fix: Call <MSBuild ... /> without those unneeded properties.

Test:

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>

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:

> 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

And indeed, removing Properties="myprop=myvalue" solves the issue.



来源:https://stackoverflow.com/questions/17399862/msbuild-m4-fails-because-it-builds-the-same-project-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!