How do I enable NuGet Package Restore in Visual Studio?
There's a similar post on stack but it doesn't help with my issue possibly because I am using Visual Studio 2015.
How do I get the "Enable NuGet Package Restore" option to appear in VS2015?
I chose File > New Project and created an empty ASP.NET Web Application. I'm looking for this menu option.
I should mention that I have looked for any pre-existing nuGet files in my project folder and there are none.
It took far too long but I finally found this document on Migrating MSBuild-Integrated solutions to Automatic Package Restore and I was able to resolve the issue using the methods described here.
- Remove the
'.nuget'
solution directory along from the solution - Remove all references to
nuget.targets
from your.csproj
or.vbproj
files. Though not officially supported, the document links to a PowerShell script if you have a lot of projects which need to be cleaned up. I manually edited mine by hand so I can't give any feedback regarding my experience with it.
When editing your files by hand, here's what you'll be looking for:
Solution File (.sln)
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{F4AEBB8B-A367-424E-8B14-F611C9667A85}"
ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project File (.csproj / .vbproj)
<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>
Microsoft has dropped support for the 'Enable NuGet Package Restore' in VS2015 and you need to do some manual changes to either migrate old solutions or add the feature to new solutions. The new feature is described pretty well in NuGet Package Restore.
There is also a migration guide for existing projects (as previously mentioned) here: NuGet Migration Guide
When upgrading:
- do not delete the .nuget directory.
- Delete the nuget.exe and nuget.targets files.
- Leave the nuget.config.
- Purge each of the project files of any reference to the NuGet targets by hand. The Powershell script mentioned seemed to do more damage than good.
When creating a new project:
-
In your Visual Studio 2015 solution, create a Solution Directory called .nuget.
-
Create an actual directory of the solution directory (where the .sln file lives) and call it .nuget (note that the solution directory is not the same as the actual file system directory even though they have the same name).
-
Create a file in the .nuget directory called nuget.config.
-
Add the 'nuget.config' to the solution directory created in step #2.
-
Place the following text in the nuget.config file:
<?xml version="1.0" encoding="utf-8"?> <configuration> <config> <add key="repositorypath" value="$\..\..\..\..\Packages" /> </config> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> </configuration>
This configuration file will allow you to consolidate all your packages in a single place so you don't have 20 different copies of the same package floating around on your file system. The relative path will change depending on your solution directory architecture but it should point to a directory common to all your solutions.
You need to restart visual studio after doing step 5. Nuget won't recognize the changes until you do so.
Finally, you may have to use the 'Nuget Package Manager for Solutions' to uninstall and then re-install the packages. I don't know if this was a side-effect of the Powershell script I ran or just a method to kick NuGet back into gear. Once I did all these steps, my complicated build architecture worked flawlessly at bringing down new packages when I checked projects out of TFVC.