msbuildrace-conditionproject-referenceparallel-buildsmsbuild-projectreference

MSBuild - race condition while trying to build project references


I'm facing a weird issue while trying to build using MSBuild.

I'm using MSBuild to build a solution file with /m (parallel build) and BuildProjectReferences set to true.

Suppose I have A.vcxproj and B.vcxproj in the .sln file with B having a project reference to A.

What happens is: the A project starts to build first and while it's in the middle of compiling, B project starts to build in another process (since parallel builds) and it would invoke building A.

Now this causes a race condition because we have two processes trying to build the same project A and I would see access issues.

Ideally, MSBuild should not invoke building B if A hasn't finished building, or if it does invoke B then detect that A is still building and wait for it to finish.

None of this happens. Also, this happens only with MSBuild - doesnt happen if I try to build the solution file from VS2015 IDE.

Any idea why MSBuild behaves this way?


Solution

  • Finally found the solution to my problem

    MSBuild expects that the project dependencies be added in two ways
    1. In the vcxproj itself, add all the dependent projectreference
    2. In the sln file too, define the projectdependencies.

    The following VS blog actually states the opposite- For example - https://blogs.msdn.microsoft.com/vcblog/2010/02/16/project-settings-changes-with-vs2010/ states that projectdependencies and projectreference are analogous and use only one specifically projectreferences.

    This may be true when you build using VS IDE but not for MSBuild. It needs the project dependencies to be defined on both ProjectReference and ProjectDependencies.

    Hope this helps anyone who hit into the same issue as mine.