Msbuild reports MSB3021 from time to time

Yu Lan 1 Reputation point
2021-06-15T07:08:27.323+00:00

I have a larget project and when I run msbuild, it reports below errors from time to time, like 5 out of 10 times it reports errors.
When it fails, I have to msbuild multiple times to get it built successfully.

I feel like it is due to some kind of parellal building which causing issues that when a project tries to access dll A, dll A might still be building so it reports the dll is locked by msbuild.exe.

Any idea?

Could not copy "D:\repo\MyRepo\target\dev\MyDir\debug\amd64\myTest.dll" to "d:\repo\MyRepo\Target\Distrib\MyDirOther\all\debug\amd64\myTest.dll". Exceeded retry count of 10. Failed. The file is locked by: "System (4), MSBuild.exe (11396), MSBuild.exe (11296)"
error MSB3021: Unable to copy file "D:\repo\MyRepo\target\dev\MyDir\debug\amd64\myTest.dll" to "d:\repo\MyRepo\Target\Distrib\MyDirOther\all\debug\amd64\myTest.dll". The process cannot access the file 'd:\repo\MyRepo\Target\Distrib\MyDirOther\all\debug\amd64\myTest.dll' because it is being used by another process.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,146 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Anna Xiu-MSFT 25,801 Reputation points Microsoft Vendor
    2021-06-15T11:00:35.867+00:00

    Hi @Yu Lan ,

    Welcome to Microsoft Q&A!

    Please try the following steps:

    1. Close all instances of Visual Studio
    2. Delete the hidden .vs, bin and obj folders in the project folder
    3. Relaunch your Visual Studio
    4. Clean the solution and rebuild your project

    Sincerely,
    Anna

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. Rajiv Kumar 1 Reputation point
    2021-09-15T05:38:39.107+00:00

    Tried following and works for me:

    1. close VS code
    2. delete bin and obj folder in solution (for all projects)
    3. dotnet run --configuration Release (we need to run in Release not debug)
      Its works
      try above steps and let me know
      thanks
      Rajiv Kumar
      rjkchaurasia@Stuff .com