7

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?

Jonathan
  • 6,939
  • 4
  • 44
  • 61

2 Answers2

5

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.

Jonathan
  • 6,939
  • 4
  • 44
  • 61
1

I found someone had added two project references (from the same project) and that apparently caused msbuild to build twice also.. something to watch out for

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30587303) – MD. RAKIB HASAN Dec 21 '21 at 11:01
  • Original asker here. This is actually a different and equally-valid answer, and is of value to future people encountering this issue. So upvote from me. – Jonathan Dec 26 '21 at 07:51