This should be a short post.
It is really just a placeholder for something that always bothers me.
Do you work with TFS and Visual Studio?
Do you have to keep excluding your NuGet packages from TFS after you added these to your solution\project?
In this article we are going to create an exception so TFS will not keep track of those files every time a new package in installed.
Adding New NuGet Packages… What Should I Check-In and What Should Not.
Every time you add a new NuGet package (and its dependencies) to your Visual Studio project, is going to update your package.config file with one or more new entries.
And that file you need to check-in, so other developers and during deployment, the correct dependencies can be install and\or deploy.
This is an example of a packages.config file for a very small application:
Basically, you can see what it looks like a list of all packages references by your Visual Studio project.
Using this list, NuGet can restore each of the project’s dependencies during deployment.
It looks nice and pretty, mostly the “targetFramework” values. All are targetting “net461”. One of the worst troubleshooting issues I encountered was updating the target .Net Framework of the project and matching the packages.config accordingly.
Back to TFS.
We need to check-in the “packages.config” file.
What we don’t need is the folder packages and all the dependencies downloaded by NuGet to your machine.
First, those files can be easily restored and second, there is no need to increment our repository space.
Any developer can easily restore the NuGet packages running for example:
- dotnet restore
- nuget restore
- msbuild -t:restore
You can even enable automatic restore for missing packages during build in Visual Studio:
Sorry, again going back to TFS:
Let’s make some changes so TFS can skip tracking these files.
How to Ignore NuGet Packages When Using TFS
First, an important note:
- If you already had checked-in a packages folder (that might or not include the dependencies files – dlls), then manually delete the “packages” folder (from Source Explorer) and check in that change before continuing with the instructions below.
These are the steps to make TFS (Team Foundation Server) ignore certain files (like project.lock.json) and folders (like packages):
1. Create a folder “.nuget” in the solution root (should be on the same level as the .sln file).
2. Inside “.nuget” folder, create a new text file named “NuGet.config“.
3. Add the following code lines to disable Source Control integration:
<?xml version="1.0" encoding="utf-8"?> <configuration> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> </configuration>
By default, nuget “disableSourceControlIntegration” is set to “false“. This entry changes that.
4. Add another file to the root of the solution and name it “.tfignore”
5. Open to edit and copy the following:
# Ignore NuGet Packages *.nupkg # Ignore the NuGet packages folder in the root of the repository. If needed, prefix 'packages' # with additional folder names if it's not in the same folder as .tfignore. packages # Exclude package target files which may be required for MSBuild, again prefixing the folder name as needed. !packages/*.targets # Omit temporary files project.lock.json project.assets.json *.nuget.props
6. Add the new folder and both new files (NuGet.config and .tfignore) to your TFS and check in your changes.
That’s it.
You can try adding a new NuGet package, and if you open your “Pending Changes”, you should not see any of the new package dependency files.
BTW, a similar file (.gitignore) is use if you are using a Git repository like GitHub or Gitlab.
In Visual Studio 2017, both are ignoring the packages folder.
Final Words
Againg, this article is my placeholder.
The more complete article you can find it here: https://docs.microsoft.com/en-us/nuget/consume-packages/packages-and-source-control
Please share this article if you think it can help someone you know.