How to change target directories structure of nuget package - visual-studio

While installing Nuget package like bootstap using visual studio 2017, for example, nuget manager installing the css files of bootstrap package in "~/Content/" directory while i prefer it to be located in "~/Content/css".
i know that i can move the files manually and to change the reference manually but then, i will need to do it again every time i am updating the package using visual studio nuget package manager.
i am wondering, if there is an option to tell every package where to install the relevant files so it will be remembered for later updates.

i am wondering, if there is an option to tell every package where to install the relevant files so it will be remembered for later updates.
I am afraid there is no such option to tell every package where to install the relevant files. According to the conventions for .nuspec file:
The convention for content is:
Think of the content folder as the root of the target application that ultimately consumes the package. To have the package add an image
in the application's /images folder, place it in the package's
content/images folder.
Just like Matt comment, "The location of the files is determined by the NuGet package itself", so there is no such option to tell every package where to install the relevant files.

Related

How Does Nuget Decide Where To Install Packages Within A Solution

I noticed a curious thing when installing some Nuget packages and am hoping someone can shed some light on why it happens.
I have a VS 2022 solution with the following layout:
MySolution.sln
MySolution.proj
CustomLibrary_1.proj
CustomLibrary_2.proj
BaseLibrary.proj
BaseLibrary is the base and does not depend on anything else.
CustomLibrary_1 and CustomLibrary_2 reference BaseLibrary.
MySolution is at the top and references CustomLibrary_1 and CustomLibrary_2.
I go to BaseLibrary and install a Nuget package using the VS GUI however (right click on the project, Manage Nuget Packages). I expected the package to be installed into BaseLibrary\Packages, but it gets installed to MySolution\Packages. There is no Packages folder for BaseLibrary however it does have a Packages.conf.
So my questions is:
Why do the packages get installed to the MySolution\Packages and not BaseLibrary\Packages? It looks like it's something to do with how the Solution also has a project named the same thing because when I look at the project file for BaseLibrary, I see entries like "..\MySolution\packages\SomePackage\lib\net452\SomePackage.dll".
We can see from this link:
repositoryPath (packages.config only)
The location in which to install NuGet packages instead of the default $(Solutiondir)/packages folder. A relative path can be used in project-specific nuget.config files.
It may be that the names of your project and solution are both MySolution causing the error of the value of $(Solutiondir).
According to your description, if you want to put packages in BaseLibrary\Packages, you can create a nuget.config in Solution folder and set repositoryPath like this:
#Set repositoryPath in project-level files
nuget config -set repositoryPath=c:\packages -configfile c:\my.Config
nuget config -set repositoryPath=c:\packages -configfile .\myApp\NuGet.Config
See this for more details.

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.

Why in VS 2015 community Manage NuGet creates both packages and scripts folder, and hides packages folder?

If I install AngularJS Core package using NuGet Package Manager, I will get a folder name "package" on the root of the project with the scripts populated, but at the same time a "scripts" folder will be created with duplicates of the same scripts.
Is there any reason for both "scripts" and "packages" folder being created?
Why if I click "Show All Files" the solution explorer will continue to hide the "packages" folder?
The packages folder exist on the solution root and it has all the packages added to all projects inside the solution including any config or manifest files.
If you added a package once, it will be added to the packages folder and depending on this package, some folders will be created inside your project, ex: Scripts, some other packages modify web.config or add some line of code to some files, depending on what the package does.
If you added the same package to another project, it will not be downloaded again, but it will be copied from the local packages folder.
Most of the packages have DLLs, and when you add such package, the project will reference the DLL directly from the packages folder, it will not copy it to the project folder itself.
However, with packages that have content such as scripts, it must be copied to the project folder as it will be published with the project and this mainly happens with the web project itself.
Actually the package folder is not in the project root folder, it is in the solution folder. It will not be added to the solution explorer windows since it was used to manage the whole solution packages for all projects.
(1) The Scripts folder in the "Packages" folder like screen shot 1 was belong to the package, it was a part of the downloaded package.
(2) The scripts folder in the solution explorer like screen shot 2 were added to the specific project as the content folder. My understanding is that just the specific package has this function, for example, it will be added to the setup file if you want to install the app in other machine. But we couldn't install all packages in the "Package" folder for this project.
Actually Haitham Shaddad provided the correct define before, but in short, my understanding is that this script content reference was different from the assembly reference, it was related to how the package members create the package. Of course, if you have the NuGet Package Explorer tool, you would find the structures of this package.
For most nuget packages, only "packages" folder will be created when you restore them. The reason you didn't see it from solution explorer is that it is just a folder created to place the nuget package files and you can change the path of the folder if you want, it is not a part of the project/solution. And if you are using version control, it is also not recommended to check the "packages" folder in to source control.
For the "scripts" folder, it is controlled by the nuget package author and the name of the folder can be anystring. If the author want the folder to be copied and added to the project, they will place it in "content" folder when create the nuget package.

Nuget package manager console - Package does not contain a manifest

I suddenly get this error on every package I try to update/reinstall in my project.. How can I start to debug this problem? Does the package manager create a log file with more details than this? Where is the manifest that it does not find?
Same package in different project but same solution works fine.
I m having the same issue.I think it might be a nuget version issue.I tried to install the package at my visual studio 2013, with nuget version is 2.12.0.817 and i got the same error. Then i tried installing the same package at my visual studio 2015, running nuget version 3.4.4.1321 and the package got installed successfully. So if i were you i would check my nuget version.
I faced two different problems:
the tool used to compress the folder (like Compress-Archive)
adds the root folder name inside the compressed file
https://github.com/PowerShell/PowerShell/issues/14312
the tool used to compress the folder has a compression method incompatible
with the client trying to unzip the .nupkg file.
For example, in my case the System.IO.Compression.ZipFile class used to compress the content of the folder was incompatible with the client extraction capabilities.
However, when I manually compressed the same folder content
with Right-click\Send To\Compressed (Zipped) Folder, it was working fine.

Resources