23

In some of my csproj files I have this but some dont:

  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
  </Target>

What exactly is this and why do I need it? My other projects are restoring their packages just fine without it.

I am using VS2013 and NuGet 2.8.

Dave New
  • 38,496
  • 59
  • 215
  • 394

1 Answers1

8

It ensures that the imported .props and .targets files are indeed imported. NuGet has supported NuGet MSBuild support since 2.5. EnsureNuGetPackageBuildImports code was added in September, but I think it is a 2.8 change. Unfortunately, it looks like a non-backward compatible change. We have some developers on 2.7 who will now need to upgrade to 2.8. I found EnsureNuGetPackageBuildImports in Common/MsBuildProjectUtility.cs on line 11.

Cameron Taggart
  • 5,771
  • 4
  • 45
  • 70
  • I wonder if "ctaggart/nuget" repository (the one you linked) is really accurate. Maybe you link to "NuGet/NuGet.Client" instead. It also has the [MSBuildProjectSystem.cs file](https://github.com/NuGet/NuGet.Client/blob/fee9da08b687e9d18073243d46f6d46ad660dd4c/src/NuGet.Clients/NuGet.CommandLine/Common/MSBuildProjectSystem.cs) with that EnsureNuGetPackageBuildImports and it seem to be the official nuget source. – Mariano Desanze Sep 04 '17 at 17:49
  • It seems that `EnsureNuGetPackageBuildImports` do not care the existence of the nuget package? – code4j Mar 03 '20 at 07:12
  • 1
    What does "NuGet has supported NuGet MSBuild support" mean? – StayOnTarget Jan 13 '21 at 17:46
  • What causes it to get added to the csproj? It just showed up in mine in a commit and I don't know what I did that added it. It's referencing Microsoft.Web.WEbJobs.Publish.2.0.0 so I'm guessing publishing from VS did it, but I'm pretty sure that wasn't the first time anyone has done that. – xr280xr Aug 31 '23 at 18:06