1

I've set up a web application project with an additional post-build step to zip some resulting files for release builds. To do this, I've added the MSBuild Community Tasks NuGet package. This works fine when I build within Visual Studio, and I've confirmed that the zip file is created. The relevant part of the csproj file is:

<PropertyGroup>
    <MSBuildCommunityTasksPath>$(SolutionDir)\.build</MSBuildCommunityTasksPath>
    <ZipFile>$(SolutionName).zip</ZipFile>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.targets" />

<Target Name="Zip" AfterTargets="Build" Condition="'$(Configuration)' == 'Release'">
    <ItemGroup>
        <ZipFiles Include="$(ProjectDir)\*.js; $(ProjectDir)\manifest.json" Exclude="$(ProjectDir)\*.map.js" />
    </ItemGroup>
    <Delete Files="$(ZipFile)" />
    <Zip Files="@(ZipFiles)" WorkingDirectory="$(ProjectDir)" ZipFileName="$(ZipFile)" ZipLevel="9" />
</Target>

I have some code in another project that attempts to automatically build the above project. Here is the code:

var projectInstance = new ProjectInstance(project.File.AbsolutePath);
projectInstance.SetProperty("Configuration", "Release");
projectInstance.SetProperty("SolutionDir", project.Solution.Directory.AbsolutePath);

projectInstance.Build("Build", Enumerable.Empty<ILogger>());

The above code fails with the following error message:

The "Zip" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with in the project file, or in the *.tasks files located in the "C:\Windows\Microsoft.NET\Framework\v4.0.30319" directory.

If I remove the Zip task, it works flawlessly. Why does this fail when I build programmatically, but not when I build within VS? How can I fix it?

Sam
  • 40,644
  • 36
  • 176
  • 219
  • Have you tried the same from Visual Studio Command prompt, using `msbuild` command. If not, can you try that once and post the result so? – RinoTom Sep 24 '13 at 14:28
  • @RinoTom, I just tried that, and it produced the same result. It also included an error number: *error MSB4036*. – Sam Sep 24 '13 at 23:44

1 Answers1

-1

This will be resolved if you give the option for package restore from Visual Studio's TOOLS Menu >> Options >> Package Manager >> General as shown in image below:

Enable Package Restore

This will restore the Nuget packages even when you are building the project using MSBuild. Otherwise it will restore only in Visual Studio builds and not in MSBuild builds. By that reason MSBuild.Community.Tasks Nuget was not getting restored for you in MSBuild but happens fine in Visual Studio.

So, try this and hope this should fix the issue.

RinoTom
  • 2,278
  • 2
  • 26
  • 40
  • The package isn't missing, so I don't think it needs restoring; it's already in the file system. MSBuild isn't saying it can't find the file. – Sam Sep 26 '13 at 08:37
  • Do you mean the package files are present in the filesystem after a try on Visual Studio command prompt MSBuild and\or a programmatic build? I know it will be present even after a Visual Studio solution build even without the setting I mentioned in answer. – RinoTom Sep 26 '13 at 09:38
  • That's right; I already installed the package from NuGet before updating the project file. I haven't removed it from the file system. – Sam Sep 27 '13 at 09:29
  • Even if the packages are installed from Nuget manually, the MSBuild will try to restore package during each build. If the EnablePackageRestore environment variable is not set as I mentioned in my answer using Visual Studio setting, the package restoration will delete the package and it will not get restored and in turn it will fail the build. – RinoTom Sep 27 '13 at 12:17
  • I'm not sure exactly how that works. What I do know, though, is that the error it's giving is not related to the file not existing. If the path is incorrect or the file doesn't exist, it seems to produce an error to indicate this. – Sam Sep 27 '13 at 23:29
  • My assumption is that, file not existing error will only have a chance to happen during Import step of target. But, since package restoration happens after that `Import` step in MSBuild, may be the file is available during `Import`. But, `Zip` call happens after package restore and then as file is getting deleted and not getting restored by Nuget I suppose and it is saying `Zip` task is not found. But, it is purely my assumption. Don't go 100% on this. – RinoTom Sep 28 '13 at 07:49