简体   繁体   English

MSBuild - 尝试构建项目引用时的竞争条件

[英]MSBuild - race condition while trying to build project references

I'm facing a weird issue while trying to build using MSBuild. 在尝试使用MSBuild构建时,我遇到了一个奇怪的问题。

I'm using MSBuild to build a solution file with /m (parallel build) and BuildProjectReferences set to true. 我正在使用MSBuild构建一个/ m(并行构建)和BuildProjectReferences设置为true的解决方案文件。 Suppose I have A.vcxproj and B.vcxproj in the sln file with B having a project reference to A. What happens is A project starts to build first and while its 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. 假设我在sl文件中有A.vcxproj和B.vcxproj,B有一个项目引用A.发生的事情是项目首先开始构建,而在编译过程中,B项目开始在另一个进程中构建(因为并行构建)并且它将调用构建A.现在这会导致竞争条件,因为我们有两个进程试图构建相同的项目A,我会看到访问问题。

Ideally MSBuild should not invoke building B if A hasnt finished building or if it does invoke B then detect that A is still building and wait for it to finish. 理想情况下,如果A没有完成构建,或者如果它确实调用B然后检测到A仍在构建并等待它完成,则MSBuild不应该调用构建B. None of this happens. 这些都不会发生。 Also, this happens only with MSBuild - doesnt happen if I try to build the solution file from VS2015 IDE. 此外,这只发生在MSBuild上 - 如果我尝试从VS2015 IDE构建解决方案文件,则不会发生这种情况。

Any idea why MSBuild behaves this way? 知道为什么MSBuild会这样吗?

Finally found the solution to my problem 终于找到了我的问题的解决方案

MSBuild expects that the project dependencies be added in two ways MSBuild期望以两种方式添加项目依赖项
1. In the vcxproj itself, add all the dependent projectreference 1.在vcxproj本身中,添加所有依赖项目引用
2. In the sln file too, define the projectdependencies. 2.在sln文件中,定义项目依赖项。

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. 以下VS博客实际上反过来说 - 例如 - https://blogs.msdn.microsoft.com/vcblog/2010/02/16/project-settings-changes-with-vs2010/声明项目依赖项和项目引用是类似的,只使用一个专门的项目参考。

This may be true when you build using VS IDE but not for MSBuild. 使用VS IDE构建而不是MSBuild构建时可能会出现这种情况。 It needs the project dependencies to be defined on both ProjectReference and ProjectDependencies . 它需要在ProjectReferenceProjectDependencies上定义项目依赖项。

Hope this helps anyone who hit into the same issue as mine. 希望这可以帮助任何遇到与我相同问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM