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

70,098

Solution 1

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>

Solution 2

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.

Solution 3

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!

Solution 4

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..

Share:
70,098
D3vtr0n
Author by

D3vtr0n

Wouldn't you prefer a nice game of chess? Everything is a hack. SO, stop changing my picture!! After 13 years, why?

Updated on June 16, 2021

Comments

  • D3vtr0n
    D3vtr0n almost 3 years

    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?