Nuget packages are installed outside of the project solution - visual-studio

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.

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.

packages.config file is ignored during Visual Studio Build and nuget restore commands

I am trying to setup automated nuget package restoration on a vs build or command line build.
When following the specified Microsoft article:
https://learn.microsoft.com/en-us/nuget/consume-packages/package-restore#migrate-to-automatic-package-restore-visual-studio
I am unable to get Visual Studio 2019 or the following commands (nuget restore mysolution.sln, msbuild -t:restore, msbuild -restore) to read from a packages.config file in each of my projects
I have tried the following:
removing any references to PackageReference in each of the project files
deleting the bin and obj folders in each project
adding a local nuget.config with the following:
deleting the local packages folder
deleting the global packages folder
running the following command: nuget locals all -clear
Updated Visual Studio -> Tools -> Options -> NuGet Package Manager and set "Allow Nuget to download missing packages" and "Automatically check for missing packages during build in Visual" and set the Default Package management format to "Packages.config"
Restarting Visual Studio a few times.
When I run the nuget restore command for my solution it attempts to restore from packagereference inside each of the project files but since I do not have any it does nothing but still creates the corresponding nuget.g.props and project.assets.json files. I cannot use packagereferences inside of project files since this is not portable to other projects.
I require the ability to use packages.config file with one of the build commands.
I am stumped as to why this is not working
You have to delete the .vs hidden folder from the solution folder, it still restores the old nuget management format PackageReference, and still use it.
Try the following steps:
1) Close VS, delete .vs hidden folder and every bin and obj folder.
2) make sure the every packages.config is in the project folder so that it will work.
3) If you use your own nuget.config, please make sure its package source contains all the nuget packages. Or the file is redundant, you should remove it and then add the nuget package source under VS IDE.
3) restart VS, run update-package -reinstall under Tools-->Nuget Package Manager-->Package Manager Console
4) After that, you can delete global nuget folder and local nuget folder and then run nuget restore xxx.sln to check it.
EDIT: Make sure your project is not an SDK-style project; SDK projects are not supported with packages.config files (see this).

Nuget package that has no dependents

I am using mspec (machine specification) unit test framework for .NET project.
I used nuget to get all required packages.
There is a specific package, that is not part of the code. It is the "Console" application for the mspec that I used to run my unit tests from outside VS, from the command line.
(It is located here):
When I use Nuget to install it, Nuget doesn't add it to the normal "packages.config" file inside of the Test project.
It is adding it to a "packages.config" folder that exists in ".nuget" folder.
But when my colleague open the project and build, his VS doesn't get that package.
How to change that, to force VS to get the package automatically?
Nuget doesn't add it to the normal "packages.config" file inside of the Test project. It is adding it to a "packages.config" folder that exists in ".nuget" folder.
That is because you are using NuGet 2.7 or later and have a solution that is still configured by old package restore method "MSBuild-integrated restore". This will cause builds to fail with an error stating that you have not given consent to restore packages. Visual Studio will skip automatic package restore.
How to change that, to force VS to get the package automatically?
To resolve this question, you can follow below steps:
Close Visual Studio to avoid file potential file locks and conflicts.
If using TFS: a. Remove nuget.exe and nuget.targets from the solution's .nuget folder and remove those files from the solution workspace. a. Retain nuget.config with the disableSourceControlIntegration setting as explained in Omitting packages with Team Foundation Version Control.
If not using TFS: a. Remove the .nuget folder from the solution and the solution workspace.
Edit each project file in the solution, remove the <RestorePackages> element, and remove any references to the nuget.targets file. For more detail information, you can refer to Migrating to automatic restore.

Should nuget .props and .targets files be part of the source code repository?

Those files are created during package restore that's why I assume they don't have to be in the repository. Is there further documentation of the purpose of those files which might help to answer the question?
For a .NET Core project the nuget.g.targets and nuget.g.props are generated by a NuGet restore. They are created in the obj folder. So you do not need to have these in version control. Typically files in the obj folder are not included in version control.
Visual Studio will automatically restore these files on opening a solution if they are missing.
If you are using a build server then you can run dotnet restore to restore these files. MSBuild 15 when installed with Visual Studio 2017 can also be used to restore these files by running msbuild /t:Restore for the solution.
The nuget.g.targets and nuget.g.props files define various properties, such as the path to where the packages cache is on your machine. They also include any MSBuild imports that NuGet packages referenced by your project need.
For example, if you have the Microsoft.Net.Test.Sdk NuGet package referenced, the nuget.g.targets and nuget.g.props import MSBuild files that are included in that NuGet package. When using a packages.config file these imports would be added directly into your project file (.csproj).
.targets files are included referenced from the project file, thus need to be in place when the project file is read. If NuGet package restore is done at the start of the building the project then those files do not exist at the point the project file is read.
Hence putting these files into source control so they always exist when project files are loaded.
A different approach is to have a project (which all other projects depend on) that forces NuGet Package Restore for the whole solution (but doesn't itself use any packages). After it has build then all targets files are in place when the project files are in place.
Of course many NuGet packages do not use such files and this is not an issue.

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.

Resources