MSBuild error MSB3021: Unable to copy file. Could not find file 'obj\Release\myWebProject1.dll'
Asked Answered
D

16

31

When using TeamCity to compile my MSBuild XML task script, it fails with this:

[10:43:03]: myWebProject1\ myWebProject 1 .csproj (3s)
[10:43:07]: [ myWebProject1\ myWebProject1 .csproj] _CopyWebApplicationLegacy
[10:43:07]: [_CopyWebApplicationLegacy] Copy
[10:43:07]: [Copy] C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets(131, 5): error MSB3021: Unable to copy file "obj\Release\myWebProject1.dll" to "C:\MSBUILDRELEASE\myWebProject1\\bin\myWebProject1.dll". Could not find file 'obj\Release\myWebProject1.dll'.

When I run it locally, it works.

When I compare my local output to my build server output, there are files missing on my build server. Like the global.asax file is missing from my build server output directory (but not when I compile this locally). Why is that?

Here is my current MSBuildScript:

<?xml version="1.0" encoding="utf-8"?>
<Project
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
  ToolsVersion="4.0"
  DefaultTargets="Build">

  <PropertyGroup>
    <OutputDir>C:\MSBUILDRELEASE</OutputDir>
  </PropertyGroup>

  <ItemGroup>
    <ProjectToBuild Include="UtilityApp.sln" >
      <Properties>OutputPath=$(OutputDir);Configuration=MSBuildRelease;Platform=x86</Properties>
    </ProjectToBuild>
  </ItemGroup>

  <Target Name="Build">
    <MSBuild Projects="@(ProjectToBuild)"/>
            <CallTarget Targets="Publish WebProject1" />
            <CallTarget Targets="Publish WebProject2" />  
  </Target>

<Target Name="Publish WebProject1">
 <RemoveDir Directories="$(OutputFolder)"
       ContinueOnError="true" />
 <MSBuild Projects="WebProject1\WebProject1.csproj"
      Targets="ResolveReferences;_CopyWebApplication"
      Properties="WebProjectOutputDir=$(OutputDir)\WebProject1\;
      OutDir=$(OutputDir)\WebProject1\;Configuration=Release;Platform=AnyCPU" />
</Target>

<Target Name="Publish WebProject2">
 <RemoveDir Directories="$(OutputFolder)"
       ContinueOnError="true" />
 <MSBuild Projects="WebProject2\WebProject2.csproj"
      Targets="ResolveReferences;_CopyWebApplication"
      Properties="WebProjectOutputDir=$(OutputDir)\WebProject2\;
      OutDir=$(OutputDir)\WebProject2\;Configuration=Release;Platform=AnyCPU" />
</Target>

</Project>

I can run this script locally and it seems to work fine (no errors generated). When I run it on my build server, it fails with MSBuild error MSB3021.

Now when I compare my local build output files to my server build output files, the server output does not have as many files. For instance, the global.ASAX file is missing in the output on my buildserver. Why would it work local for me, but not on my TeamCity build server? What's the difference and how can I fix it?

I noticed the TeamCity build agent error message has a funny directory path: "C:\MSBUILDRELEASE\myWebProject1\bin\myWebProject1.dll"

^ There are two slashes before the bin folder. I do not specify that anywhere. What gives? I have a feeling I am not building my Web Projects correctly (maybe use a different task approach?). It seems to work locally but not on my build server.

Am I building my web projects correctly? These are simply web projects for Web Service (ASMX) deployment. Help?

Disquietude answered 1/3, 2011 at 17:49 Comment(3)
I'm having a similar issues. It seems to stem from a problem with where the dlls are compiled too. With Configuration=Release, files are output to obj\Release\, and if Configuration=Debug they're output to obj\Debug\. But if you have Configuration=SomethingElse, my files are ouput to obj\SomethingElse but MSBuild looks to obj\Release still. My configuration "SomethingElse" was copied from the Release config... Why won't it look to the right folder? It's a problem with the _CopyWebApplicationLegacy target for VS2010 I think.Dank
I suggested you delete all files and folders in your obj folder on your development machine, and try to run your msbuild script again. I imagine, if your issue is like mine, you'll get the same error on your local machine that you get on your build server.Dank
The other situation I have run into is that the file(s) in question are marked as ReadOnly. In my case a misc DLL originated in source control thus ReadOnly. When I removed ReadOnly from the DLL the issue cleared up. MSBuild won't copy files on top of themselves if they are ReadOnly.Dutiful
D
12

Alright, I figured it out. It's a "Configuration" mismatch. You have one project building with Configuration=MSBuildRelease and two other projects building with Configuration=Release. MSBuild then looks in the wrong place for the "intermediate" assemblies.

Change your code to this:

<?xml version="1.0" encoding="utf-8"?>
<Project
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
  ToolsVersion="4.0"
  DefaultTargets="Build">

  <PropertyGroup>
    <OutputDir>C:\MSBUILDRELEASE</OutputDir>
  </PropertyGroup>

  <ItemGroup>
    <ProjectToBuild Include="UtilityApp.sln" >
      <Properties>OutputPath=$(OutputDir);Configuration=MSBuildRelease;Platform=x86</Properties>
    </ProjectToBuild>
  </ItemGroup>

  <Target Name="Build">
    <MSBuild Projects="@(ProjectToBuild)"/>
            <CallTarget Targets="Publish WebProject1" />
            <CallTarget Targets="Publish WebProject2" />  
  </Target>

<Target Name="Publish WebProject1">
 <RemoveDir Directories="$(OutputFolder)"
       ContinueOnError="true" />
 <MSBuild Projects="WebProject1\WebProject1.csproj"
      Targets="ResolveReferences;_CopyWebApplication"
      Properties="WebProjectOutputDir=$(OutputDir)\WebProject1\;
      OutDir=$(OutputDir)\WebProject1\;Configuration=MSBuildRelease;Platform=AnyCPU" />
</Target>

<Target Name="Publish WebProject2">
 <RemoveDir Directories="$(OutputFolder)"
       ContinueOnError="true" />
 <MSBuild Projects="WebProject2\WebProject2.csproj"
      Targets="ResolveReferences;_CopyWebApplication"
      Properties="WebProjectOutputDir=$(OutputDir)\WebProject2\;
      OutDir=$(OutputDir)\WebProject2\;Configuration=MSBuildRelease;Platform=AnyCPU" />
</Target>

</Project>
Dank answered 26/3, 2011 at 17:47 Comment(4)
I actually have not been able to test this out. I ended up using CruiseControl.NET to build my .NET solution and it worked much easier. TeamCity's support was very slow, and kept putting blame on Microsoft. Microsoft support would put blame on them. So instead, CruiseControl.NET was up and running, building my app within a few hours. I may eventually migrate over to TeamCity, since I am using TeamCity to build my FLEX and FLASH projects. Who knows, but I am marking your answer as the correct solution.Disquietude
I am seeing a similar MSBuild error from time to time, though for me, it is always about copying from <full path>\bin\Debug\MyProject.dll to bin\Debug\MyProject.dll which suddenly starts failing for a few assemblies on some days. It does have something to do with configurations, as switching the affected projects to Release instead of Debug (which is otherwise set for all projects in the solution) will prevent the problem from happening. Not sure how to satisfactorily resolve the issue yet, but thank you for the hint about configurations.Corporate
Thank you. Solved my problem by changing my release output to where by debug output was pointing. Had this error once I tried to build using release, debug was always working.Bend
this answer at least got me to look at the configurations used by TC. Thank you.Stites
M
8

I experience this problem often with old projects, that I have not opened for long time

The solution that works for me is:

  1. Open the project folder and delete the bin folderenter image description here

  2. Make sure the project config is set to Debug not Release enter image description here

  3. Build the project - VS will make new bin folder

    enter image description here

I think that this problem is releated to the release in some kind of a way.

Merissameristem answered 21/5, 2022 at 12:19 Comment(0)
E
4

After spending 3 hours on this bug, I started a new project and I imported each file from the old project one by one. I've been able to compile between each file until I added the last file.

I thought the problem was related to that last file but I realized by removing other files that this issue was happening only when I had a specific number of files included in my project.

I can't tell why it worked, but I solved this issue by adding / removing empty classes with random names to my project.

After a couple of Add / Compile / Remove / Compile again, VS started to work correctly.

Empyrean answered 12/4, 2013 at 19:53 Comment(0)
C
2

Just a hunch, but I notice you are building the solution with Platform=x86, then calling the two WebProjects with Platform=AnyCPU. If those two projects are being built by the solution, the output location might be different for the build vs. the subsequent call to deploy.

Some other notes:

I generally avoid CallTarget, and would prefer this form in your case:

<Target Name="BuildProjects">
    <MSBuild Projects="@(ProjectToBuild)" />
</Target>
<Target Name="Build"
    DependsOnTargets="BuildProjects;Publish WebProject1;Publish WebProject2"
    />

The doubled slashes usually indicate one of two things:

$(OutDir)\$(Intervening)\bin

Either An intervening property was not evaluated, if $(Intervening) is empty, or one of the parts of the path already ends in a trailing slash, if the $(OutDir) property already has a trailing slash.

I never knew you could have spaces in a target name, I had to check it just to be sure and it worked!

Copilot answered 1/3, 2011 at 18:9 Comment(6)
ok the double backslash problem I figured out, it should be more like this: "Properties="WebProjectOutputDir=$(OutputDir)\WebProject1;" with no trailing slash. OutDir property requires a trailing slash. Go figure?Disquietude
It just dawned on me that the full path is not specified in the error message. It is trying to use "obj\Release\myWebProject1.dll". How do I change that to use full path? I only specify two paths (WebProjectOutputDir & OutDir). I dont see where this would be wrong??Disquietude
Generally $(OutDir) has the value "bin\Debug" but you seem to be passing the project folder in your calls to the MSBuild task in your two Publish targets. Is it possible you are mixing up the standard $(OutDir) property with your own custom $(OutputDir) property?Copilot
I installed Visual Studio 2010 (professional) on my build server today. I ran the build script through the VS Command Prompt. I receive the same error "unable to copy file" blah blah. This is weird because my development machine is just fine when it builds this. No errors. Only on my build server is this happening. Any ideas? What about the .target files for _CopyWebApplication stuff?Disquietude
whats weird now is that I get it to compile on my build server from the command line. but when I use TeamCity to run it, it produces this error. It should be the exact same script and compiler. I dont know why its doing this. Microsoft keeps sending me to TeamCity support. We will see.Disquietude
attach a diagnostic logger "/fl /flp:v=diag;logfile=log.txt" to the command line for both builds, the command line build that works and to the TeamCity build that fails, then diff the two log files. Pay attention to the section for each project that lists the property and item values. Ideally they should be the same, so any significant differences may yield a clue.Copilot
S
1

I Faced the same issue when connecting to MDF via entity framework. Tackled it by simply cleaning the solution and rebuilding it. Hope it helps..

Straightway answered 27/5, 2019 at 16:35 Comment(0)
G
1

My project build again after delete the 'obj' and 'bin' folder.

Gravity answered 3/3, 2020 at 9:14 Comment(0)
O
1

If your Build is being ruined by MSB3021, start with checking your antivirus settings. Mine has a feature called Real-time file system protection. Whenever a file is being created or modified, it grabs it for analysis, which results in the file being locked. As soon as I disabled this feature, MSB3021 disappeared.

Orometer answered 16/5, 2022 at 21:24 Comment(1)
thank you! I had to go into Avast settings, by right clicking the icon in the taskbar > Quarantine... > Select the ... next to my file > Restore and add exception.Wheezy
D
0

I had this issue when trying to deploy to appharbor, for me, excluding the file then re including it fixed the issue

Datary answered 11/7, 2015 at 18:44 Comment(0)
W
0

While I had the exact same error; in my case I had a Wix (v3.9) Deployment project (MSI - for a desktop application) that ran Heat.exe to harvest the file from the output folder. Turned out that VS and Heat dont play well if you dont have the

RunAsSeparateProcess="true"

attribute set on the Heat Directory pre-build task. Found this after many hours of frustration. see Wix HeatFile Task Locks Dll for details. HTH someone.

Wuhan answered 22/1, 2016 at 5:6 Comment(0)
S
0

Step 1: Restart your Visual Studio.

Step 2: Build >> Reconfigure app.

I got it right this way.

Statutory answered 13/11, 2017 at 20:50 Comment(0)
S
0

I had the same error. Look the Target framework. For mistake i've changed the netcore from 2.1 to 2.0

Sustenance answered 5/12, 2019 at 19:1 Comment(0)
S
0

My solution for this is open TaskManager/win or Monitor/mac And kill all visual studio and related processes.

Sever answered 10/1, 2020 at 9:35 Comment(0)
A
0

Quick solution for those, who are using Github - simply clone your last project commited edit to different empty folder. I did this right now and it was working.

Agitate answered 15/6, 2021 at 16:15 Comment(0)
B
0

the problem was error MSB3021 it work with me like this :-

  1. i delete the bin and obj in the root of my project
  2. i run dotnet restore and bulid it tell me that some processes are already in use so i restart the laptop to close all the processes
  3. dontnet run and every thing worked
Bistro answered 1/3, 2022 at 15:43 Comment(0)
A
0

Upgrade .net version works for me

Africanist answered 3/1, 2023 at 14:10 Comment(0)
C
0

simply end task your project application in task manager and run the project again. it works for me.

Cafard answered 29/6, 2024 at 1:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.