NuGet not restoring packages on build
Asked Answered
C

8

28

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.

Cornish answered 24/12, 2013 at 12:53 Comment(4)
Might seem silly, but did you check the packages.config file for the library project is present? Ans if you open the Package Manager by right-clicking the project in VS, does it show all packages for the project properly?Gherardo
Yes, the packages.config file is there and the Package Manager has the Library project ticked with the correct packages.Cornish
have you blown away the NuGet package cache for the machine and the local packages folder by the SLN?Seraphim
It looks we have the same issue with Rebuild Solution. It doesn't pull any packages from NuGet. Though, if right click a project and choose Build, then it pulls the packages from NuGet.Inarticulate
T
12

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.

Tamberg answered 3/1, 2014 at 21:38 Comment(7)
This link is valid for VS13 but, apparently, in VS15, there's no such thing anymore. I have a folder called packages and a file called packages.config. That's it. When I drop the folder or any of its contents, it's being restored upon build. Pure magic, hehe! But that's on my local machine. When I push the stuff to autobuild on Visual Studio Online, it doesn't restore the packages (although I selected VS2015 as the building agent setting).Nemeth
Visual Studio will still use the .nuget folder if you create one (you can put a nuget.config in there for example), but it's not created by default any more. And yes, as you've noticed Visual Studio will now try to restore packages before it builds. On your build server you'll need to run nuget restore in a build step before you build the solution.Tamberg
Actually, what I noticed after crying my butt off is that there's no need for the pre-step restoring the packages anymore! If you're using VS2015 in the build step (only step I had in my test), it actually restores the packages for you. This is potentially confusing but oh so nice. And obvious, in some sense! Yey VS15, hehe.Nemeth
Which build server are you using? On AppVeyor and TeamCity I have to restore the packages before building the solution.Tamberg
I'm using Visual Studio Online (which, despite the name, is anything but Visual Studio). Please note that it's not the build server that's the thing here. It's the agent. If I choose VS2013 as profile, I need to restore too (the same if I'd compile locally on VS13. However, when I compile from VS15 (or choose the profile VS15 on the build server), it's being handled automagically. So, my guess would be that TC will do that too - but only if you select target agent (environment or whatever JetBrains called it) to VS2015. Kind of awesome, in my view. Could you verify it and tell me?Nemeth
I guess Microsoft must have put some of that magic into the VSTS build agent. I just tried both AppVeyor and TeamCity on the Visual Studio 2015 profile and neither of them automatically restored the packages.Tamberg
I'm not enough savvy on the issue to offer an explanation but I might speculate that TC and AV didn't implement the agent correctly, perhaps? I'm thinking about my local VS15 - it restores too. On the other hand, MS might have put in some of that magic into VS15 specifically (and the online build server). In that case, they kind of locked TC out... Confusing to the developers, in my view...Nemeth
B
21

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.

Buskined answered 6/5, 2015 at 18:33 Comment(0)
T
12

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.

Tamberg answered 3/1, 2014 at 21:38 Comment(7)
This link is valid for VS13 but, apparently, in VS15, there's no such thing anymore. I have a folder called packages and a file called packages.config. That's it. When I drop the folder or any of its contents, it's being restored upon build. Pure magic, hehe! But that's on my local machine. When I push the stuff to autobuild on Visual Studio Online, it doesn't restore the packages (although I selected VS2015 as the building agent setting).Nemeth
Visual Studio will still use the .nuget folder if you create one (you can put a nuget.config in there for example), but it's not created by default any more. And yes, as you've noticed Visual Studio will now try to restore packages before it builds. On your build server you'll need to run nuget restore in a build step before you build the solution.Tamberg
Actually, what I noticed after crying my butt off is that there's no need for the pre-step restoring the packages anymore! If you're using VS2015 in the build step (only step I had in my test), it actually restores the packages for you. This is potentially confusing but oh so nice. And obvious, in some sense! Yey VS15, hehe.Nemeth
Which build server are you using? On AppVeyor and TeamCity I have to restore the packages before building the solution.Tamberg
I'm using Visual Studio Online (which, despite the name, is anything but Visual Studio). Please note that it's not the build server that's the thing here. It's the agent. If I choose VS2013 as profile, I need to restore too (the same if I'd compile locally on VS13. However, when I compile from VS15 (or choose the profile VS15 on the build server), it's being handled automagically. So, my guess would be that TC will do that too - but only if you select target agent (environment or whatever JetBrains called it) to VS2015. Kind of awesome, in my view. Could you verify it and tell me?Nemeth
I guess Microsoft must have put some of that magic into the VSTS build agent. I just tried both AppVeyor and TeamCity on the Visual Studio 2015 profile and neither of them automatically restored the packages.Tamberg
I'm not enough savvy on the issue to offer an explanation but I might speculate that TC and AV didn't implement the agent correctly, perhaps? I'm thinking about my local VS15 - it restores too. On the other hand, MS might have put in some of that magic into VS15 specifically (and the online build server). In that case, they kind of locked TC out... Confusing to the developers, in my view...Nemeth
C
10

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.

Corned answered 23/3, 2016 at 9:57 Comment(0)
S
6

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:

  1. In Visual Studio, right-click on the project with the missing NuGet references, and select "Manage NuGet Packages..."
  2. 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..."
  3. 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."

enter image description here

Although the "Restore" apparently automatically happened for me, clicking that button manually might also do the trick to resolve the missing packages issue.

Suanne answered 18/12, 2014 at 15:4 Comment(0)
H
4

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

Humour answered 3/10, 2016 at 13:47 Comment(0)
F
1

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.

Frydman answered 6/3, 2020 at 2:27 Comment(0)
C
0

On Mac; I commited the code to git and deleted everything (main folder), then downloaded it again. Worked afterwards.

Cirrocumulus answered 13/8, 2020 at 14:12 Comment(0)
N
0

This is probably very outdated and people may have similar issues. So, in 2024 what I did...

Close Visual Studio and go to (replace "YourUser" accordingly):

C:\Users\YourUser\AppData\Roaming\NuGet

Delete the file:

NuGet.Config

Once you reopen Visual Studio, it will recreate that file. I hope it helps if the accepted solution doesn't to somebody else!

Nahshu answered 14/6 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.