Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing file NuGet.targets on existing project

The scenario is:
1. Created a NEW solution
2. ENABLED download missing packages
3. Add projects to solution that already exists and depends on nuget packages
4. Building the solution generates an error:

... The missing file is <solution folder>\.nuget\NuGet.targets.

Why? Have missing something?

I'm using Visual Studio 2017 Pro on Windows 10

All my searches answer about the scenario where create a new solution and adds a new project that depends on nuget package, that's ok, but when have an existing project, nothing.

like image 877
JPassos Avatar asked Jan 17 '19 11:01

JPassos


People also ask

How do I fix a missing NuGet package?

Restore NuGet packagesNavigate to Tools > Options > NuGet Package Manager > General, and then select the Allow NuGet to download missing packages check box under Package Restore. Enabling Restore NuGet Packages. In Solution Explorer, right-click the solution, and then select Restore NuGet Packages.

How do I install NuGet packages missing?

Right-click the project, select Manage NuGet Packages, and use the NuGet Package Manager to uninstall and reinstall the affected packages. For more information, see Reinstall and update packages.

How do I reference an existing NuGet package from a new project?

NET Core project. After you install a NuGet package, you can then make a reference to it in your code with the using <namespace> statement, where <namespace> is the name of package you're using. After you've made a reference, you can then call the package through its API.

Where are NuGet targets?

config and a nuget. targets file are created within a . nuget folder, and all projects that have NuGet package references will be modified to import the NuGet.


1 Answers

<solution folder>\.nuget\NuGet.targets is a file that NuGet's Visual Studio extension used to add in Visual Studio 2010, 2012 and 2013 when you right click on the solution and select "Enable NuGet Package Restore". It would then add an import in all of your project files to import the targets file. I was recently investigating a customer issue and as part of that investigation I found it made the following changes. Near the top of the csproj, it adds something like this:

<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>

and near the end of the csproj it adds somthing like this:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<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>

So, the problem is that you're creating a new solution, but using existing project files that have been modified in this way. One option is to edit your csrpoj files and remove these changes. Another option is to create new projects, in addition to the new solution, and then copy all your code and content files.

The only disadvantage is if you build on a CI server and use packages.config, your build script needs to run nuget.exe restore, whereas projects that use NuGet.targets could just build the solution and msbuild would execute nuget restore as needed. One advantage of no longer using NuGet.targets is that restoring the whole solution is faster than restoring project by project. Visual Studio automatically restores packages on build, even in VS2010, VS2012 and VS2013, so personally I discourage the use of using this feature, even if you use those old versions of Visual Studio. The benefit of reducing your build script by one step is not worth the issues it brings, in my opinion.

like image 120
zivkan Avatar answered Oct 11 '22 05:10

zivkan