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

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.

Related

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).

When a Nuget package is added, where are the stuff added located?

In Visual Studio 2019 I added a Nuget package, Microsoft.AspNetCore.Authentication.Google version 3.14. The only visible change I can find is an extra line in <ItemGroup> in the .csproj file.
Where is the code of the package located?
How is version control impacted if my project is checked into TFS?
Nugets are saved under :
{project-root}\bin\{debug-release}\{target-framework}
{project-root}\obj\{debug-release}\{target-framework}
Additionally all nugets are cashed under below folder for future use :
%UserProfile%/.nuget/packages
The packages are in your build folder ie. bin/debug or bin/release
NuGet packages are typically omitted from source control repositories and are restored with project dependencies before a build.
Omitting NuGet packages in source control systems

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.

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.

How do I create a NuGet package from a project file so it includes the necessary assemblies?

To generate a NuGet package I'm currently using the command:
nuget pack project.csproj -Prop Configuration=Release
which I expected would package up the files in the bin\Release folder, i.e. the result of building the project. This folder has several MB of assemblies, however the nupkg file that gets created is only 7kb, which doesn't seem to include any of these files.
I'm running the nuget command from TeamCity so I'm trying to avoid manually editing a nuspec file.
What is the best way to package a project like this using NuGet?
TeamCity 7.0 is finally here and it has its own NuGet server built in.
You can simply ask it to take care of your packages whether you want to publish it locally or/and to nuget.org
TeamCity and NuGet

Resources