NuGet Package Restore does not fetch Build Target

2019-03-30 21:54发布

问题:

I added Fody ProperyChanged to two projects in my solution. Package Restore is enabled on the solution. However, the TFS Build Service fails building with the following error:

WindowsUI.csproj (443): The imported project "SolutionDir\Tools\Fody\Fody.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

The folder is indeed not there. I could check it into source control, obviously. However, should it not be populated by the NuGet Package Restore? Or am I misunderstanding what NuGet Package Restore does?

回答1:

As of version 1.13.0.0 (released March 23, 2013) Fody is a 100% nuget deployed tool and as such it will work with package restore.

https://nuget.org/packages/Fody/

This will appear when you install the Fody Nuget https://github.com/Fody/Fody/blob/master/NuGet/readme.txt



回答2:

I ran into a similar problem trying to get a solution to build on Visual Studio Online. Problem is that packages are restored before a project build, but before that the project files and target inclusions from packages (still to be restored) have already been interpreted.

Use the before build hook as described here:

http://sedodream.com/2010/10/22/MSBuildExtendingTheSolutionBuild.aspx

In your before.solutionname.sln.targets file put something like this to force all packages to be restored before even the first project is built:

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

<Target Name="BeforeBuild" BeforeTargets="Build">
 <Message Text="Restoring all nuget packages before build" Importance="high">
 </Message>
<Exec Command=".\.nuget\NuGet.exe restore YourSolution.sln" />
</Target>
</Project>

If you have external package sources configure them in your nuget.config file which should also be in the .nuget folder. For example:

<configuration>
  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>
  <packageSources>
    <add key="NuGet official package source" value="https://nuget.org/api/v2/" />
    <add key="YourSource" value="http://yoursource.somewhere.net/nuget" />
  </packageSources>
  <packageRestore>
    <!-- Allow NuGet to download missing packages -->
    <add key="enabled" value="True" />

    <!-- Automatically check for missing packages during build in Visual Studio -->
    <add key="automatic" value="True" />
  </packageRestore>
</configuration>


回答3:

UPDATE: This answer now only applies to versions prior to 1.13.0.0.

The files in SolutionDir\Tools\Fody cannot be deployed through nuget and needs to be checked into source control



回答4:

You are running into the same issue that I did when I tried to ship a build update in NuGet package. The issue is that NuGet package restore is invoked during the build process. Because of this if NuGet package restore restores a .targets file that is imported, it is restored too late. By the time the file is written to disk the <Import element has already been evaluated and skipped due to the file not being on disk.

The best thing that I have found is to build another project to invoke the package restore for you. In order to smooth this out for my own SlowCheetah NuGet package when the NuGet package is installed I create a packageRestore.proj file in the same director as the .csproj/.vbproj. Then users can build this project file and then the .sln/.csproj/.vbproj. By doing this the NuGet packages are restored and then the build process is kicked off.

If you are interested in using my packageRestore.proj I can re-factor that part of SlowCheetah NuGet package into its own and your NuGet package can depend on that one. Let me know if you are interested in that.