MSB4057: The target “package” does not exist in the project.

When MSBuild executes a project file, it might not find all imported targets files and ignore them silently. This makes it quite difficult to figure out which file is actually missing. In my case, the result was the error MSB4057: The target “package” does not exist in the project on web application projects.

MSB4057 On the Web

The error MSB4057 can have many reasons. When dealing with the “package” target, you may want to consider other solutions on the web as well. For example:

Another Approach

In a web application project file, the file Microsoft.WebApplication.targets is imported.

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />

That file, in turn, references the targets file Microsoft.Web.Publishing.targets that contains the package target. The task here is to figure out where MSBuild searches for this file. From the import directive as a starting point, these are the steps to resolve the error:

  1. Determine the path to imported Microsoft.WebApplication.targets

    Add a new target to your project file and make it output the path of to the imported file.

    <Target Name="Output">
        <Message Text="Path: $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets"/>
    </Target>
    

    Then execute the new target with MSBuild.

    > msbuild myproject.csproj /t:output
    

    Which prints out the path to Microsoft.WebApplication.targets.

    Build started 29.09.2017 09:14:01.
    Project "C:\projects\myproject.csproj" on node 1 (Output target(s)).
    Output:
      Path: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\WebApplications\Microsoft.WebApplication.targets
    Done Building Project "C:\projects\myproject.csproj" (Output target(s)).
    
  2. Determine the path to Microsoft.Web.Publishing.targets

    Open the targets file found above and find the import directive for Microsoft.Web.Publishing.targets. You’ll find something like this:

    <Import Project="$(AspNetTargetsPath)Microsoft.Web.Publishing.targets" Condition="Exists('$(AspNetTargetsPath)Microsoft.Web.Publishing.targets')" />

    Change the output target in your project file to print out the full path of the imported file.

    <Target Name="Output">
        <Message Text="Path: $(AspNetTargetsPath)Microsoft.Web.Publishing.targets"/>
    </Target>
    

    Then execute the new target with MSBuild.

    > msbuild myproject.csproj /t:output
    

    Which prints out the path to Microsoft.Web.Publishing.targets.

    Build started 29.09.2017 09:14:01.
    Project "C:\projects\myproject.csproj" on node 1 (Output target(s)).
    Output:
      Path: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications\..\Web\Microsoft.Web.Publishing.targets
    Done Building Project "C:\projects\myproject.csproj" (Output target(s)).
    
  3. Find Microsoft.Web.Publishing.targets

    Now that you know the full path under which MSBuild searches for Microsoft.Web.Publishing.targets, try to find it. If that file is not located under the path, find another copy of it and copy the whole folder. In my case, the file was not located under C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications\..\Web\Microsoft.Web.Publishing.targets, but I found it under C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.targets.
    I searched for files with the same name underneath the MSBuild programs directory.

    Found copies of the file Microsoft.Web.Publishing.targets
    I copied the whole directory C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web to the location MSBuild was looking for, i.e. to C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications\..\. Now, the file path determined in Step 2 exists.

  4. Try again

  5. Retry the build process that initially encountered the MSB4057 error. If everything went well, the build should run now.

Freelance full-stack .NET and JS developer and architect. Located near Cologne, Germany.

Leave a Reply

Your email address will not be published. Required fields are marked *