Search code examples
msbuildmsbuild-4.0

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


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?


Solution

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