How to fix deleted DLLs from NuGet's packages? - visual-studio-2010

With NuGet, I added Json.NET (Newtonsoft.Json) to my code. As result of a mistake (adding code to CMS, ignoring DLLs, and checking out), my reference DLLs were deleted.
How do I restore ?
I followed all links in this SO question, but nothing brings back the DLLs.

If you still have the packages.config file you should be able to use nuget.exe to restore the packages in the packages folder. You can run nuget install followed by the path to your packages.config file.
nuget install packages.config
This will restore the packages to the packages folder but not modify your project.
If you do not have the packages.config file you could try running nuget install specifying the package id and the version instead.
If you have files in your packages folder you may want to remove those before running this command.

Related

NuGet reinstall updates the package to newer version

I have cleared all the local NuGet caches and Visual Studio 16.9 is unable to restore the packages.
It says:
All packages are already installed and there is nothing to restore.
When I build the project, I get many errors as the NuGet packages are not restored.
The project has to contain the exact same packages as they are in the PackageReferences inside the csproj file and I've read that using the Package Manager Console can help by executing the Update-Package -reinstall command.
The Install-Package command does not provide an option to force a reinstall, so use Update-Package -reinstall instead.
I tried it and now I have this in my log:
No package updates are available from the current package source for project....
Updates? I don't want to update anything...
Restored D:\Dev\MyProject01\MyProject01.csproj (in 29 ms).
Successfully uninstalled 'Acr.UserDialogs 7.1.0.475' from MyProject01
Successfully installed 'Acr.UserDialogs 7.1.0.481' to MyProject01
How is this a reinstall? Seems more like updating the package to a newer version...
How could I reinstall the same packages?
UPDATE:
When I use the Rebuild command in the Solution Explorer, this is what I see.
Actually, that right-click on the Solution-->Restore nuget packages is for the missing project packages folder with packages.config. And it cannot detect the global cache missing packets alone.
That is why Microsoft has enabled these two settings:
So you do not have to worry about restore step. Just click Rebuild button rather than Build button. The two restore settings are under Rebuild process.
The project has to contain the exact same packages as they are in the
PackageReferences inside the csproj file
update-package -reinstall command is for non-sdk style projects with packages.config nuget management format and always reinstall the same version. And I wonder if your solution has the non-sdk project that used packages.config and that project can use the command.
I have a non-sdk project with net framework 4.5.2. See:
But update-package -reinstall does not work on new-sdk projects.
ConsoleApp1 is a net core project.
So, I wonder if you use Update-Package under it. And that command is to update the old version to the new one which can works on both non-sdk projects and new-sdk projects.
Conclusion
When you face the problem next time, just click Rebuild button. It is more easier.
Try deleting your project's obj/ directory (this is where NuGet stores information about which packages it's decided to use), and then rerunning the restore.
If you have source dependencies (i.e. ProjectReference) you may need to delete their obj/ directories too

Nuget packages are installed outside of the project solution

Let's say I have a solution called MyFirstSolution and inside I have project with name MyFirstProject. Inside that project I'm installing nuget packages like Dapper, EntityFramework etc...
How things should work:
packages folder should be created into the folder MyFirstSolution and inside should be downloaded and installer all nuget packages referenced into the project.
The problem I'm having is that the packages folder is not created and the nuget packages are downloaded one level above the MyFirstSolution folder into some folder called XYZHelper.
When I download a solution from some repository nuget packages are restored to that same folder, not to the solution itself and references in csproj file says that they should be in solutionFolder\packages so all of my projects don't build.
If I copy the packages manually to the folder that they should be, it's all good but after every build a copy is made into that XYZHelper folder.
I don't have any postbuild events or anything like it configured into the projects. It is something with the visual studio I guess, but I can't figure out what.
How things should work: packages folder should be created into the
folder MyFirstSolution and inside should be downloaded and installer
all nuget packages referenced into the project.
Hi Stdfan, not sure about your VS version. But for VS2015 and earlier versions, the nuget packages are controlled by packages.config file. And things should work like what you mentioned.
But for VS2017 and VS2019, they have two methods to manage nuget: Packages.config and PackageReference. And for PackageReference format, the packages are stored in C:\Users\xxx\.nuget\packages. So if your vs version is VS2017 or VS2019, you can try if changing the format to PackageReference help resolve this issue.
The problem I'm having is that the packages folder is not created and
the nuget packages are downloaded one level above the MyFirstSolution
folder into some folder called XYZHelper.
Direction1:
Like zivkan suggested,I also think something affects the restore process.Normal for Packages.config format, the folders would be stored in packages folder. But according to
this document, we can customize nuget.config file to control nuget behavior. So please check locations where nuget.config exists,there might be some changes in the nuget.config for computer or some settings in nuget.config for users which causes this issue.(The nuget.config for user won't exist unless we create it there)
Direction2:
When I download a solution from some repository nuget packages are
restored to that same folder
As the restore process is invisible in build output, so there is possibility that the nuget restore works well, but something in build process move the content of packages folder into XYZhelper.
Check customize your build. Please check your directory structure for the Directory.build.xx file, it can affect your build process if it exists in any folder of the structure: C:\xxx\lancel\source\repos\
I don't have any postbuild events or anything like it configured into
the projects. It is something with the visual studio I guess, but I
can't figure out what.
This is not about VS normal settings. I think some custom file causes this issue(no matter nuget.config or directory.build.xxx), and please check if you've installed any third-party software or vs extension. Try close vs, delete the .vs, bin and obj folders and then run vs as safe mode.
Target folder of nuget packages can also be set using the envirenment variable NUGET_PACKAGES.
And you can do this in your existing project-file like this:
<Project Sdk="Microsoft.NET.Sdk">
...
<Target Name="NugetPublicfolder" BeforeTargets="PreBuildEvent">
<Exec Command="SET NUGET_PACKAGES=C:\MyProjectA\libraries\"/>
</Target>
...
</Project>
Now when you build/publish the project the libraries will be placed in C:\MyProjectA\libraries\
Actually the nuget packages are first downloaded to the socalled http-cache folder (%userprofile%\AppData\Local\NuGet\v3-cache), where it will be extacted from to the above folder (which is called the globalPackagesFolder folder)
The solution packages folder can be changed with a nuget.config (the setting name is called repositoryPath). So, check for nuget.config files that might be setting this value. You can get an exact list of nuget.config files used by a restore by downloading nuget.exe from nuget.org/downloads and running nuget restore MyFirstSolution.sln. Near the end of the output it will list every nuget.config file that was read.
When there is no config file that changes the solution packages folder (repository path), it always defaults to a subdirectory named packages in the same directory as the .sln file. The only way I know of to change this location for packages.config projects is with a nuget.config file, so I feel confident you should be able to solve it by finding the right config file.

Uninstall nuget packages installed with nuget.exe

I was having trouble with nuget Visual Studio 2017 and wanted to test if it was our proxy server or not. I downloaded nuget.exe and dropped it at the root of my C drive. I executed nuget.exe install Microsoft.AspNetCore and it asked for my proxy server username and password. I supplied it, and it proceeded to create dozens of directories in C:\ with that package and all of it's dependencies (can I get a DOH!) Problem is, there's no uninstall in nuget.exe. The package manager in VS isn't available unless you have a solution open, and the package manager console (powershell) won't uninstall unless you have a solution open. Can I just clobber all those directories and their contents? Or will that just make things different and somehow worse:
Can I just clobber all those directories and their contents? Or will that just make things different and somehow worse
Yes, you can (If you confirm that you no longer need these packages).
According to the document install command (NuGet CLI):
The install command does not modify a project file or packages.config;
in this way it's similar to restore in that it only adds packages to
disk but does not change a project's dependencies.
When you use nuget.exe install some packages, it just download those packages to the disk and not change anything in the project, this is similar to some files you downloaded from the internet.
So, if you do not need those packages any more, you can just delete them from the disk manually.
Besides, there is an option delete command (NuGet CLI), which can be used to deletes or unlists a package from a package source. The exact behavior depends on the source. For local folders, for instance, the package is deleted; for nuget.org the package is unlisted. But it only delete the file .nupkg and will keep the folders and other files of other packages, like lib folder. If you want remove the package completely, just delete those packages manually.
Hope this helps.

How reproduce VS2017 Package Manager command "Update-Package -reinstall" with Nuget CLI

I've tried with Nuget Update, Nuget restore, but they are actually not the same and here's why:
I've created a Nuget pkg only to distribute an executable file. When I install the Nuget the executable is added to my project so I can reference it and run it within my application.
Now, I don't want that every developer has to install the Nuget to have the executable in their project, but I just want the .exe to be added when the project is built. And of course, the reference to the package in the config file is present.
If I run Update-Package -Id -reinstall(using the Package Manager of VS) a folder with the .exe is added to the project root, but if I do nuget update or nuget restore (using the nuget CLI) it only re-add the .exe into the packages folder.
The reason why I need to run the nuget CLI is because I'm using TeamCity and I want to add the .exe when the automatic build runs.
I hope it makes sense.
How reproduce VS2017 Package Manager command “Update-Package -reinstall” with Nuget CLI
I am afraid you can NOT do such things with NuGet CLI. That because NuGet CLI does not modify a project file or packages.config. When we modify the project file, we need to do it by NuGet API in Visual Studio, but only package manager console can provide access to visual studio objects:
https://github.com/NuGet/Home/issues/1512
That is the reason why I said the easiest way is using the command line Update-Package -Id -reinstall in your previous thread. Then I also gave you another way to resolve this issue from the root cause, using .targets file to copy the executable file to the the project root, please check my update answer.
Of course, there is another simple idea to resolve this issue, just add a pre-build event in your project to copy the executable file to the the project root:
xcopy /Y "$(SolutionDir)packages\MyToolPackage.1.0.0\Tools\wkhtmltopdf.exe" "$(ProjectDir)"
Hope this helps.

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.

Resources