本文介绍了MSBuild/m:4失败,因为它两次构建同一项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队有一个很大的解决方案(约500个csproj).我们使用VS2012,并使用TFS Build(其使用MSBuild 4)进行构建.当前我们以串行方式进行构建,但我们希望以并行方式(使用msbuild /maxcpucount:4)进行构建.但是,当我在4-proc机器上尝试时,出现了奇怪的错误:

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]

看看日志,有2个msbuild节点试图构建相同的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).

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会尝试两次构建同一项目?

Why would MSBuild try to build the same project twice?

推荐答案

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

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.

固定::不带那些不需要的属性的呼叫<MSBuild ... />.

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

测试:

通用目标

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

运行:

> 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

确实,删除Properties="myprop=myvalue"可以解决问题.

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

这篇关于MSBuild/m:4失败,因为它两次构建同一项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 19:08