Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 497
Martin Komischke Avatar asked Mar 28 '17 12:03

Martin Komischke


2 Answers

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

like image 144
Matt Ward Avatar answered Oct 12 '22 03:10

Matt Ward


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

like image 39
Richard Avatar answered Oct 12 '22 01:10

Richard