Where do I put files in a nuget package so that they will be restored? - visual-studio

I'm working on setting up nuget packages for dacpacs for some large database schemas we have. Here's a sample of the metadata:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>xxxx.DACPAC</id>
<version>1.0.0</version>
<title>xxxxDatabase dacpac</title>
<authors>xxxxxxxxx</authors>
<owners>xxx</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<projectUrl>xxx-database.git</projectUrl>
<iconUrl>http://github.com/contoso/UsefulStuff/nuget_icon.png</iconUrl>
<description>DACPAC for xxxx Database</description>
<releaseNotes>Initial release</releaseNotes>
<copyright>Copyright ©2019 xxxx</copyright>
<tags>dacpac database xxxxxx</tags>
</metadata>
</package>
The .nupkg looks like this
and the content directory like this:
This works fine. I then create a solution with an ssdt project and a skeleton c# project. I use the c# project to hold the dacpacs. I can nuget them, then in the ssdt project add database refs pointing to the dacpacs. The dacpacs are not in git (part of my goal is to keep them out).
I commit the repo then clone it to an empty directory. Good so far.
Next, i use nuget to restore the packages in visual studio. However the dacpacs are not restored! I think that's because they are in the "content" directory in the package and from what I can find, those files are not restored by nuget.
So, my question is, where to put these files so that they will be restored? Which nupkg directory? Content doesn't work. Tried tools to no avail. Thought I should ask before another blind attempt.

So, my question is, where to put these files so that they will be
restored? Which nupkg directory? Content doesn't work. Tried tools to
no avail. Thought I should ask before another blind attempt.
Nuget Restore is to download the mssing packages. And the content in xx.nupkg will only be added to projects at install time of one package. Nuget restore can't get the content files back, that's not restore's job. So it has nothing to do with which directory we put the files in, it's just one not supported job for nuget restore...
As a workaround: You can use Update-Package -reinstall in Package Manager Console to reinstall the package. And the content files will be added back to project. More details see this document. Hope it helps :)

Related

How can I fix missing nuget references after moving project in Visual Studio 2015

I had a project structure like this:
WebApp
-- WebApp
-- WebApp.sln
WebApp.Tests
-- WebApp.Tests.csproj
I moved WebApp.Tests into WebApp using a move (simple click & drag into the WebApp folder). I edited WebApp.sln to fix the project reference so that it will load.
When I build, Nuget complains that packages are missing and to do a restore.
I downloaded and used nuget.exe restore on my solution and it reported everything was there.
Based on other Stack Overflow answers, I have tried the following:
Edit the test project reference hint paths. I changed from ..\WebApp\packages\PACKAGE to ..\packages\PACKAGE
Reload Visual Studio (multiple times)
Delete contents of packages folder and bin/obj folders of the projects
Use the package manager console to reinstall packages on the Test Project
All of these failed to fix the problem. When I used the package manager to try to reinstall the packages with the command, it gave me the same error that project building does - I must restore the packages first.
Is there any quick way to fix my project? I really don't want to go through each package and uninstall/reinstall manually. Also, how could I have prevented this problem in the first place? Is there a built-in way to move project locations?
There was XML similar to this at the end of my project file:
<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('..\WebApp\packages\SPECIFICPACKAGE')" Text="$([System.String]::Format('$(ErrorText)', '..\WebApp\packages\SPECIFICPACKAGE'))" />
</Target>
By changing the ..\Webapp\packages to ..\packages like the rest of the file, my solution compiles just fine now.
I had the same issue where I physically moved a project folder into the src folder and got the missing packages error. My fix was to open up the .csproj file in notepad and replace every instance of
this:
..\packages\
with this:
..\..\packages\
It solved the issue...

How do I set Visual Studio to build a NuGet package?

How can I get Visual Studio to build a NuGet package for my library component on build?
I’m using a Portable Class Library as the example project.
Ensure the NuGet.exe file in .nuget folder is latest.
Default values come from AssemblyInfo.cs, so clean that up.
Add a NuGet package reference if you do not reference any, preferably something simple like JSON.NET. Often, PCL projects have no external dependencies, in which case no NuGet refs and without any NuGet refs, the required MSBuild config won't get set properly, so we need to add a 'dummy'.
Enable NuGet Package Restore.
Edit the NuGet.targets file and ensure BuildPackage is true.
<!-- Property that enables building a package from a project -->
<BuildPackage Condition=" '$(BuildPackage)' == '' ">true</BuildPackage>
Edit your .csproj file and add this to the first, default PropertyGroup
<BuildPackage>true</BuildPackage>
Rebuild your project and then search in the Output for nupkg and confirm creation and location of the package file.
Remove the dummy NuGet package reference and build again and check the Output.
To further customize the package creation, you can stick a MyProjectName.nuspec file next to your .csproj file.
See http://docs.nuget.org/docs/reference/nuspec-reference for more on the NuSpec format. You can also pull one from an existing package (its just a zip file) and have a nose around, see how it was done.
Add a post-build event like this:
$(SolutionDir)\.nuget\nuget.exe pack "$(MSBuildProjectFullPath)" -p Configuration=Release -o "$(MSBuildProjectDirectory)\bin\Release" -symbols"
And download and place nuget.exe in the .nuget folder alongside your solution file.
You can use nuget update -self to keep the .exe fresh.
Note
nuget.exe pack has a bug currently where it'll see a packages.config file and try to look for the packages it mentions in your solution but it fails to find them if the packages folder is in a strange place, e.g. if your solution file isn't a level up from the project.
To workaround this, add another post build event to copy the packages folder into the project folder.
The repositorypath config setting seems to do nothing for me.
See GitHub reports:
https://github.com/NuGet/Home/issues/5316
So funny. I was having problems with my usual way of auto-building a package on build when I arrived at this new way. So I looked for a suitable SO question to answer with my new post-build method when I came across my own question here!

TFS/VS 2013 - Ignore ALL NuGet packages [duplicate]

I'm trying to get TFS (2013) to ignore my packages folder. I passionately don't want it source controlled as I'm using NuGet and it's great!
I've tried cloaking (doesn't seem to work), I've tried adding .tfignore files - nothing is ignored. Why don't the TFS team just add an option to permanently ignore a folder or file like lots of the Subversion clients do?!
Here's the deal: We have to tell both NuGet and TFS to ignore the packages, because NuGet is trying to do source-control related stuff that it absolutely shouldn't be doing (bad form, Microsoft!). So you have to do two things.
First, add a file named .tfignore to the solution folder (note the lack of s after the tf). Its contents should be as follows:
\packages
That tells TFS to ignore your packages folder. Now, you would think that this would also ignore the repositories.config file. But it won't. Why? Who knows, the ways of Microsoft are strange and mysterious. Actually, I think it's part of the NuGet stuff I outline below, but if that ever gets fixed in the future and you want to keep the repositories.config file instead of letting VS regenerate it, you should be able to use this:
\packages
!\packages\repositories.config
OK, so now thanks to our .tfignore file, TFS is ignoring your packages. Everything is fine, right? WRONG, because NuGet is mucking around with your source control and adding the packages to your pending changes. So now let's tell NuGet to cut it out already.
Create a folder called .nuget in the root of your solution folder.1 Now, create a file called NuGet.config, and put it in this new folder2. Its contents should look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
And now your packages should stay out of source control. Just remember to add the NuGet.config and .tfignore files to source control so they never get lost.
EDIT: If you're having issues, you may want to delete your packages folder, check in that change, and then go through the steps above.
ALSO EDIT: It looks like this won't happen with newer versions of Nuget. So maybe if you switch to VS/TFS 2017 this issue will clear up without jumping through the above hoops.
1. Add the folder using Source Control Explorer; right-click the solution->Add folder->.nuget
2. When I figured this out using VS 2013, I found the NuGet.config had to go in the .nuget folder. Even if you already have a NuGet.config file in the root of your solution folder (because, say, your company has an internal nuget feed). However, some in the comments have indicated that it works fine in the solution root in VS 2015. Personally, I switched to using TFS in git mode, so I can't test. Additionally, if you do have a custom feed, ensure that you have both the custom feed and nuget.org as keys in the Nuget.config file, or sometimes TFS will randomly decide it can't restore the packages.
An alternative solution to the above is the following.
Add the packages folder to TFS (without any files or sub-folders)
Right Click the Packages Folder
Left Click Advanced
Click Cloak
It is worth noting that this solution would need to be applied per TFS workspace. It has worked far more reliably for me rather than using the .tfignore file.
You can read more about this approach in the blog article Prevent TFS from adding installed NuGet packages to source control.
for people reporting that the .tfignore option wasn't working with the nuget.config setting it might be of interest - these steps finally worked for me:
Delete everything in my packages folder
Make sure TFS doesn't have any changes around that folder pending
Close VS
Re-open VS, and reload solution - using Nuget restore to re-populate
packages Note no changes are pending for TFS source control
Add a nuget.config file in a .nuget folder in your solution.
Add the following to the nuget.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
The disableSourceControlIntegration is what makes the trick for TFS Version Control.
You need to be using local workspaces for .tfignore to work. The .tfignore file must be in the folder that contains the files or folders you want to ignore.
So if your solution structure looks like this:
\Project
\Packages
\OtherStuff
foo.cs
You'd put your .tfignore file in \Project:
\Project
\Packages
\OtherStuff
foo.cs
.tfignore
The contents of the .tfignore in your case would be:
\packages
Here's some documentation for you: http://msdn.microsoft.com/library/vstudio/ms245454(v=vs.110).aspx#tfignore
You can permanently set this once-off in your AppData\Roaming for all solutions (old & new)!
In your %AppData%\NuGet\NuGet.Config file, add the following just before the </configuration> XML tag...
<config>
<add key="repositoryPath" value="C:\NuGetPackages" />
</config>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
...you can specify any path you want - the important thing is putting it OUTSIDE your TFS workspace!
Now you never have to worry about that stuff again. Your solution folder will not contain any packages anymore; all solutions will default to using your custom packages location instead.
NOTE - This works is on a per-user basis.
Set your solution to restore on build, the package folder and packages file will be checked in but the packages won't.
If you are using Git with TFS you need to add a ".gitignore" file. You can do this in "team project | Settings | 'add ignore file'". Then open the file and uncomment the built in ignore statement for Nuget Packages.
If you are using TFVC and you have Local Workspaces configured you can use the ".tfignore" file that honours an identical format to the Git file. I think you need "packages/".
This didn't work for me quite on visual studio online and VS2013.
Right Click Solution > Enable NuGet Package Restore. This will add the Nuget.config file to solution
Add the .tfignore. I normally do this by adding a text file to the solution root, letting it detect that and then exclude by clicking 'detected add' > right click ignore.
Add the packages to .tfignore and tell it to include repositories.config
From the other comments it seems your milage may vary at this point. This is what I do:
Check everything in, including any packages.
Delete all packages in your solution and then check in this change (this will remove the packages from TFS)
Open the solution and build which will add the packages to the project but TFS will not pick them up.
The solution that worked for me was to create both a .tfignore and the following setting in the Nuget.Config:
<configuration>
...
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
...
</configuration>
My .tfignore contains this line:
\packages
[I'm using Visual Studio 2015 Update 2]
This is not ideal, and is currently logged as an open issue on github/nuget:
Make it easier to omit packages from TFVC #493
Terje's answer doesn't work all the time for me, sometimes it will work for a while, but then it will pend a load of "adds" for me all over again.
The only way I have found to solve this permanently is to Cloak the packages folder in my Workspace.
For example:
Type Server Local
============================================
Active $/Work/Main C:\Code\Main
Cloaked $/Work/Main/Packages
I had the same issue. /packages should work but didn't for me. packages*.* did work.

Am I doing this right? Nuget Package Restore with centralized packages folder

I placed a nuget.config file in my project root folder that specifies where all NuGet packages should go for all my solutions.
Here is the file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="D:\Projects\Development\NuGet Packages" />
</config>
</configuration>
I have also enabled NuGet Package Restore on all my solutions.
When I first set this up, the nuget.config file was being ignored--all NuGet packages were being placed in a "packages" folder in the solution folder. I tried deleting this folder numerous times, but invariably, NuGet would recreate it and fill it with packages.
After much trial and error, I stumbled upon the <PackagesDir> element in "NuGet.targets" and noticed that commenting it out finally allowed the packages to be placed in the correct folder in my project root.
Here's the relevant line from NuGet.targets
<PackagesDir>$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir>
My question is simply whether commenting out the PackagesDir element in the NuGet.targets file is the intended method to get my nuget.config file to be followed or if I'm missing a more obvious solution.
There are two things in play in your question:
Where does NuGet store packages?
NuGet package restore for the open project(s) / solution
Regarding the first: setting the repositoryPath key in your nuget.config file is the correct approach to instruct NuGet to extract packages in a different folder from the default. This setting is used whenever a developer adds a package refernce to your project.
For NuGet package restore, thissetting is overridden by the setting you found in NuGet.targets. This setting is used when restoring packages upon build of a solution.
If you want to use package restore and a custom path, you indeed have to specify both. The first one for your devs, the second one for package restore.

remove nuget package restore from solution

I added the recent nuget package restore feature to a solution using 'Enable NuGet Package Restore':
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
However it broke my build server and I didn't have the time to fix it, so I wanted to remove it. There's no option for that as far as I know, so I removed the following line manually from all my *.csproj files:
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
The problem now is that every time my *.csproj files are checked out or open my solution, the line is automatically added again, breaking my build if I accidentally check it in :(
Any ideas how I can remove it permanently?
UPDATE: despite the answer below it still keeps coming back when opening the solution, anyone with the same problem?
I didn't look very well, there's another property added to the project files:
<RestorePackages>true</RestorePackages>
Just have to remove this as well as all these lines manually from all *.csproj files:
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
UPDATE:
Turns out it's a persistent little bugger, if you're manually editing your project files, make sure to close the solution and delete all the lines from the project at once, otherwise they're just added again once the project reloads...
UPDATE2:
Delete the .nuget folder from the solution root too
UPDATE3:
A later version of NuGet adds another section that you need to remove:
<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>
Update4
Inside the NuGet.Targets located in the .nuget folder, there is another section that gets added to new projects... switch it to false.
<!-- We need to ensure packages are restored prior to assembly resolve -->
<BuildDependsOn Condition="$(RestorePackages) == 'false'">
RestorePackages;
$(BuildDependsOn);
</BuildDependsOn>
To disable Nuget Package Restore:
Delete .nuget folder
Remove specific lines from all .csproj files
Lines to remove:
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<RestorePackages>true</RestorePackages>
Note: Make sure you do all changes in one go before reloading solution or else it will add them back.
This is based on the following article:
http://bartwullems.blogspot.no/2012/08/disable-nuget-package-restore.html
Also, you might want to double-check that this option is disabled:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
Isn't it this setting here?
Options... -> Nuget Package Manager -> [uncheck] Allow Nuget to download missing packages
I'm using Visual Studio Professional + Resharper 8.2
Solutions currently using MSBuild-Integrated package restore can be migrated to Automatic Package Restore. From what I understand, this should help those who are encountering CI build issues. (Please correct me if I am mistaken).
Please refer to the document on the nuget website: Migrating MSBuild-Integrated solutions to use Automatic Package Restore at http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
There is information there for converting with and without TFS.
David Ebbo also posted some information at http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html
We actually have a blog post about it and at the end of the post a powershell script was mentioned to help with the migration.
http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
NuGet has a blog post about migrating to automatic package restore:
http://docs.nuget.org/docs/workflows/migrating-to-automatic-package-restore
There is a powershell script referenced in the blog post that will take care of removing the necessary lines automatically (and recursively):
https://github.com/owen2/AutomaticPackageRestoreMigrationScript/blob/master/migrateToAutomaticPackageRestore.ps1
Offering a Disable NuGet Package Restore option directly is marked as Won't Fix:
https://nuget.codeplex.com/workitem/1883
I had the same issue. What I ended up doing:
1) go into each project .csproj file in the solution, open it in notepad then removed the portion of the xml and saved.
2)Then I removed the all of the package.config files in the entire solution.
3)Then I had to remove the .nuget and package folders.
At this point, I had a completely NuGet free solution.
4)Then I manually referenced any needed DLLs and hit compile and the solution ran like a champ without the NuGet packages being needed.
I followed the accepted solution to no avail using 2012. This did work though,
Completely close the VS
Update the <RestorePackages>true</RestorePackages> to <RestorePackages>false</RestorePackages> and delete the <Import Project="$(SolutionDir)\.nuget\nuget.targets" /> line
Also renamed the nuget.exe to nuget.exe.NotExe
For anyone still needing to clean up a project using the old style NuGet package restore, the IFix tool available here automates the process.
Just run the installer (IFix will be added to PATH) and then run the following:
IFix nugetrestore --fix
You can run it in check mode first to see what it will clean up:
IFix nugetrestore --check
Go to your solution directory where you have [$(SolutionDir)\.nuget\nuget.targets]
.nuget folder and nuget.targets file under it delete the folder,
and change remove lines from your csproj for once last time.
The problem won't come back to bug you again.
Remove the packages.config file within your solution.
I accidentally enabled this "package restore" option while opening my project in VS2012 RC and started getting errors that looked something like:
"Error 1 Unable to locate 'C:\FolderX\SomeProject.nuget\nuget.exe'"
To fix the error I followed the above instructions, opened open each project file in notepad and removed that RestorePackage line.
I was able to resolve this issue by taking these steps:
1) make sure you take a backup of all your current checked-out files changes.
2) physically delete the solution folder from your C:\ (path that is mapped to TFS).
3) get latest from TFS for your solution.
4) copy (if any) your changes from the backup you took in step-1.
hope that helps !
I ran into the exact same problem and tried to remove all .nuget and RestorePackage tags from the project files but one project just wouldn't reload not matter how thoroughly I examined it for .nuget and RestorePackages tags. I guess there's some hidden references to this somewhere.
In the end it was easier to just copy the files and create a new project and import it to the solution.
Nuget sucks. Just remove nugets and remove or comment package elements from packages.config in root directory of the projects where this is a problem. Use direct references into some lib folder instead.
<?xml version="1.0" encoding="utf-8"?>
<packages>
<!--<package id="EntityFramework" version="6.0.2" targetFramework="net45" />-->
</packages>

Resources