NuGet restore enabled but I still get an error - visual-studio

Why do I get the following build output error when I already have NuGet package restore enabled?
Restoring NuGet packages...
To prevent NuGet from downloading packages during build, open the
Visual Studio Options dialog, click on the Package Manager node and
uncheck 'Allow NuGet to download missing packages'.
All packages listed in packages.config are already installed.
MyProject.csproj: error : This project references NuGet package(s)
that are missing on this computer. Enable NuGet Package Restore to
download them.
It only happens on one project.
I am using Visual Studio 2013 and NuGet 2.8.

Make sure to upgrade to the latest version of NuGet which does package restore automatically. See this post by David Ebbo for more information: http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html
You're going to want to delete the NuGet targets (delete the .nuget folder and then manually edit your .csproj files and remove the lines that import the NuGet.targets file). This is no longer needed. When you compile your solution, NuGet will restore it.

Related

URL for NuGet packages in Visual Studio 2019

Been working on another computer for several months. Recently, I did a git pull on another laptop which is not used very often. When I tried to build the solution, several projects failed due to missing NuGet packages.
I tried restoring the packages but this failed. Packages MSTest.TestFramework and MSTest.TestAdapter in particular were out-of-date, so I cleared the NuGet cache in Tools > Options > NuGet Package Manager > Clear All NuGet Cache(s).
But now when I right-click the project and Manage Nuget Packages..., no packages are displayed at all!
Back in Tools > Options > NuGet Package Manager > Package Sources only a single Machine-wide package source is listed, namely Microsoft Visual Studio Offline Packages. This points to a location on my location disk ("C:\Program Files (x86)\Microsoft SDKs\NuGetPackages", currently an empty folder).
New sources can be added, but what URL should be used to get the latest NuGet packages?
NuGet's documentation states that the endpoint is https://api.nuget.org/v3/index.json
source: https://learn.microsoft.com/en-us/nuget/nuget-org/overview-nuget-org#api-endpoint-for-nugetorg
There's also a similar question in the FAQ: https://learn.microsoft.com/en-us/nuget/resources/nuget-faq#i-don-t-see-nuget-org-in-my-list-of-repositories--how-do-i-get-it-back-

Nuget restore doesn't add reference to project

Nuget keeps on not doing what I expect:
Say I got a bunch of references in a packages.config and add that to a newly created project. Then nuget asks me to restore the packages, on answering yes, it only downloads the packages to the packages folder but forgets to add a reference the the project.
How to automatically also add a reference to my project once restored?
Lasse is correct about the NuGet restore. NuGet restore will only download the NuGet packages and extract them to the packages directory.
However with just the package information in the packages.config file there is a way to have NuGet add the references to the project.
First restore the NuGet packages.
Next open the NuGet Package Manager Console and reinstall the packages using:
Update-Package -reinstall
The above -reinstall command will uninstall and install the packages again and fix any missing references.
There are some limitations with the above command which are documented on the NuGet site.
Restoring packages will only download the packages and place them into the right folders on disk.
You get the references when you originally add the nuget packages to the project the correct way.
You should not add nuget packages to the projects by modifying the package configuration file, you should add them using the "Add Nuget reference" menu item on the References folder of the project.

Restore nuget packages defined in .nuget folder

There are some nuget packages (e.g. OpenCover or ReportGenerator) installed without changing packages.config in any of the project, but there is a [Solution Dir]\.nuget\packages.config created with the package reference information.
When VS builds the solution, the packages defined in that file are not downloaded at all (even I have auto restore nuget enabled).
How can I restore them automatically?
The MSBuild based package restore, that uses NuGet.targets, which is enabled in Visual Studio by selecting Enable NuGet Package Restore, does not seem to support restoring solution level packages, which are those that are defined in the [SolutionDir]\.nuget\packages.config file.
Looking at the NuGet.targets file on build it restores the packages for the project using the project's packages.config file but does not use the solution's packages.config file.
So your options are:
Stop using the MSBuild based package restore. Visual Studio, with recent versions of NuGet, will automatically restore all packages, including all solution level packages, when you build.
Run NuGet.exe restore YourSolution.sln from the command line, or PowerShell console, or as a pre-build step, to restore all packages. NuGet.exe can be used to restore all packages on a build server if you are using one.
The MSBuild based package restore has been deprecated by the NuGet team so I would use option 1) if this is possible.

Does "enable nuget package restore" realy necessary for package recovery?

I can successfully restore package even if the option "enable nuget package restore" is not switched on in the project. Plus it generates additional files within my solution.
With the latest version of NuGet (2.7 or above) you do not need to use the Enable NuGet Package Restore menu option if you do not want to.
The latest version of NuGet, when installed in Visual Studio, will automatically restore any missing packages when your build your project. It does this without needing to add any new files to your solution and does not use MSBuild. It is now the recommended way of restoring NuGet packages.
You can also restore from the command line using NuGet with a command line similar to:
nuget restore YourSolution.sln

NuGet not restoring packages on build

I have just created a workspace on a new machine, got latest of our project from TFS, enabled NuGet Restore, and I get the following (skimmed-down) output:
1>------ Rebuild All started: Project: Caching, Configuration: Debug Any CPU ------
1> Restoring NuGet packages...
1> To prevent NuGet from downloading packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages'.
1> All packages listed in packages.config are already installed.
1> Caching Framework -> C:\MyProjLocation\Caching\bin\Debug\Caching.dll
2>------ Rebuild All started: Project: Library, Configuration: Debug Any CPU ------
2>C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "LumenWorks.Framework.IO". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
2>C:\MyProjLocation\Library\SomeClass.cs(2,7,2,17): error CS0246: The type or namespace name 'LumenWorks' could not be found (are you missing a using directive or an assembly reference?)
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
None of the NuGet packages were restored (there are over 10 - I removed them from the above output for readability sake).
It looks like NuGet is not even trying to restore the packages for the second project (Library).
I have already tried moving the NuGet targets import below the CSharp targets import in the Library.csproj file, as mentioned here, but it's still not working:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
I am running the latest version of NuGet (2.7.41101.371) on Visual Studio 2013.
Edit: The packages.config exists and the NuGet Package Manager has the Library project ticked with the correct packages.
I had to go into Source Control and delete all of the files in the packages folder (except repositories.config) before NuGet would restore the missing packages. The idea is that you are using package restore rather than checking your packages in to source control. If it sees the packages in source control, it won't download them.
Have you deleted the NuGet.targets file from disk too?
If the NuGet.targets file is there, Visual Studio / NuGet.exe will try to do the MSBuild package restore.
See this doc for more info.
I have the same issue, also on the local machine. Although both Package Manager Console and nuget.exe restore MySolution.sln report that everything is installed, there is no packages folder to be found in the solution directory and no references to packages are being resolved.
I checked all project files and they expect packages to be placed in ..\packages folder, the same folder where the solution file itself is located.
The way I made it work is to run:
nuget.exe restore MySolution.sln -PackagesDirectory packages
This forced nuget.exe to download all packages to the specified folder and all references were restored.
Remember that from NuGet 2.7, the targets file is not supported, msbuild suppose to use some integrated way of restoring packages but it fails very often.
In fact, for my own work I prefer using Paket, which always work, when you get used to it. It also supports target files and nice way to create NuGet packages.
I'm not sure about the science behind this, but it worked for me just now after trying to build a freshly-downloaded Visual Studio project, and getting several MSB3245 warnings followed by a build failure due to missing references:
In Visual Studio, right-click on the project with the missing NuGet references, and select "Manage NuGet Packages..."
The Manage NuGet Packages dialog will open. I also saw a message quickly display and then auto-close, with text along the lines of "Restoring NuGet Packages..."
Close the Manage NuGet Packages dialog (without actually changing anything on the dialog), and retry the build.
Edit: Going back in my TimeSnapper auto-screenshot history (no affiliation with that tool -- I'm just a fan), it looks like there was also a message displayed at the top of the Manage NuGet Packages, along with a "Restore" button: "Some NuGet Packages are missing from this solution. Click to restore from your online package sources."
Although the "Restore" apparently automatically happened for me, clicking that button manually might also do the trick to resolve the missing packages issue.
Had to uninstall nuget packages and do a refresh install in order to make it work properly. This might help some of you facing the same issue
I was having the same problem. In my case, it was a NuGet.Configin the parent directory that was setting <add key="repositorypath" value="C:\CxCache" />. So the nuget restore was copying the packages to a folder I didn't know of. See NuGet Configuration Inheritance.
Deleting the NuGet.Config in the parent directory solved the problem.
On Mac; I commited the code to git and deleted everything (main folder), then downloaded it again. Worked afterwards.

Resources