C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets (3797): Could not copy file “<file>” to “<bin directory>”. Exceeded retry count of 10. Failed.
C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets (3797): Unable to copy file "<file>" to "<file>". The process cannot access the file “<bin directory>” because it is being used by another process.
occurs due to parallel msbuild processes running in a build trying to replace a single assembly referred more than one project parallel.
This error can cause random failures in builds which is not good. The teams may loose trust of the build system. Even if the cause of error due to code team will not pay attention since CI build failing randomly.
One way to solve this is to detect and prevent duplicate files from being copy to build output. To detect duplicate files tool explained here can be used.
How to resolve?
1. The easy fix for this is setting the build to run in a single process. Build definitions using template prior to TFS 2013 build templates, this can be set easily by going to build definition Process set “MSBuild Multi-Proc” to false.
2. However this is not possible in TFS 2013 build templates. To bring “MSBuild Multi-Proc” back to the TFS 2013 build templates follow the instructions explained in article here.
3. The other option is passing the process count to the build using “MSBuild Arguments”. using /m:1 (/maxcpucount:1) will run the build as a single process.