354

I'm trying to get TFS (2013) to ignore my packages folder. I passionately don't want it source controlled as I'm using NuGet and it's great!

I've tried cloaking (doesn't seem to work), I've tried adding .tfignore files - nothing is ignored. Why don't the TFS team just add an option to permanently ignore a folder or file like lots of the Subversion clients do?!

Palec
  • 12,743
  • 8
  • 69
  • 138
Matt
  • 5,573
  • 4
  • 33
  • 37
  • What versions of TFS and Visual studio are you using? Are you using local or server workspaces? – James Reed Jun 11 '14 at 13:36
  • It's a NuGet bug, apparently: http://docs.nuget.org/docs/reference/package-restore-with-team-build – user2864740 Dec 29 '14 at 21:51
  • possible duplicate of [tfIgnore does not work for me in Visual Studio 2013](http://stackoverflow.com/questions/23416407/tfignore-does-not-work-for-me-in-visual-studio-2013) – Edward Brey Mar 05 '15 at 01:40
  • 1
    You should change the answer on this question – Chris Marisic Aug 06 '15 at 16:06
  • 3
    If you're using NuGet the packages folder is required. You can set NuGet to restore the missing binaries on build though (so that you source control the packages folder, but ignore the binaries). -- But there's problems with that: It's possible for a NuGet binary to get updated (without the version number changing), or to get removed, etc. -- It's possible for all sorts of weird oddities to happen. Don't leave your builds to chance -- check in the whole packages folder. You'll save yourself a lot of headaches. – BrainSlugs83 Oct 05 '15 at 17:19
  • I've succeeded in cloaking the projects subfolder. – Andrei Rînea Mar 05 '16 at 21:10

12 Answers12

525

Here's the deal: We have to tell both NuGet and TFS to ignore the packages, because NuGet is trying to do source-control related stuff that it absolutely shouldn't be doing (bad form, Microsoft!). So you have to do two things.

First, add a file named .tfignore to the solution folder (note the lack of s after the tf). Its contents should be as follows:

\packages

That tells TFS to ignore your packages folder. Now, you would think that this would also ignore the repositories.config file. But it won't. Why? Who knows, the ways of Microsoft are strange and mysterious. Actually, I think it's part of the NuGet stuff I outline below, but if that ever gets fixed in the future and you want to keep the repositories.config file instead of letting VS regenerate it, you should be able to use this:

\packages
!\packages\repositories.config

OK, so now thanks to our .tfignore file, TFS is ignoring your packages. Everything is fine, right? WRONG, because NuGet is mucking around with your source control and adding the packages to your pending changes. So now let's tell NuGet to cut it out already.

Create a folder called .nuget in the root of your solution folder.1 Now, create a file called NuGet.config, and put it in this new folder2. Its contents should look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>
</configuration>

And now your packages should stay out of source control. Just remember to add the NuGet.config and .tfignore files to source control so they never get lost.

EDIT: If you're having issues, you may want to delete your packages folder, check in that change, and then go through the steps above.

ALSO EDIT: It looks like this won't happen with newer versions of Nuget. So maybe if you switch to VS/TFS 2017 this issue will clear up without jumping through the above hoops.

1. Add the folder using Source Control Explorer; right-click the solution->Add folder->.nuget
2. When I figured this out using VS 2013, I found the NuGet.config had to go in the .nuget folder. Even if you already have a NuGet.config file in the root of your solution folder (because, say, your company has an internal nuget feed). However, some in the comments have indicated that it works fine in the solution root in VS 2015. Personally, I switched to using TFS in git mode, so I can't test. Additionally, if you do have a custom feed, ensure that you have both the custom feed and nuget.org as keys in the Nuget.config file, or sometimes TFS will randomly decide it can't restore the packages.

Pharylon
  • 9,796
  • 3
  • 35
  • 59
  • 1
    I believe this requires TFS 2012 at least. http://stackoverflow.com/questions/14365929/team-foundation-server-2010-not-honoring-my-tfignore-file – Jason Parker May 11 '15 at 22:01
  • If one does this, then updating of nuget packages to a newer version gets completely screwed up. Lots of errors saying that it can't update a file because source controlled file with this name is pending a delete - which works fine *without* the nuget.config. The bummer is you don't notice this straight away and think you found a working solution, but then a new version of a nuget package comes out and you are in the world of pain =( – Andrew Savinykh May 26 '15 at 04:51
  • 2
    @zespri What file is pending a delete? The NuGet package? That shouldn't be pending a delete... it should be deleted! After you do what I suggested above, go to Team Explorer and delete all of the Nuget packages that might already be in source control. Or just delete the packages folder altogether. I've been rolling with this setup for months and never experienced that error. – Pharylon May 29 '15 at 19:19
  • 1
    are you referencing any nuget packages that change source files within the project, such as jquery, bootsrap, etc? The pending deletes are on these, not on the package folder. – Andrew Savinykh May 29 '15 at 20:24
  • 1
    Yeah. I just went in and updated JQuery and it went fine. Must be something with the way you have TFS set up. Do you have exclusive locking turned on or something? – Pharylon Jun 02 '15 at 12:16
  • 5
    Fyi in VS2015, nuget.config does **not** have to be inside /.nuget/. It works great in the solution root. – jnm2 Nov 25 '15 at 18:50
  • 143
    For anyone wondering how to create a folder and file that starts with a period, end the name with another period. So that would be .tfignore. and .nuget. – Derek Ziemba Jan 06 '16 at 20:42
  • 17
    @DerekZiemba Neat trick! I've been doing it through the command line all these years. I think it's worth mentioning that when you do that, the trailing '.' will be removed, I initially thought your suggestion was to just live with an extra '.' on the end. –  Jan 11 '16 at 16:22
  • This is a pretty good explanation on how to do this: http://docs.nuget.org/consume/package-restore#omitting-packages-from-source-control – FrankO Feb 13 '16 at 05:43
  • did this, .\packages still shows in TFVC; excluded packages, checked in, packages still listed in changes. in my mind this made sense... however, apparently it will still try to check in the packages no matter what. – Chaim Eliyah Jul 14 '16 at 19:13
  • @Pharylon would be great if you could add to the main answer the other information suggested by ChaimEliyah – DevT Jul 22 '16 at 16:42
  • 1
    I have to say that the "\packages" didn't work for me, I had the .tfignore in the solution root folder, but I make it work without the "\". So my .tfignore looks like packages .fake .vs *.user – Nekeniehl Oct 05 '16 at 12:46
  • 1
    @Pharylon This will stop working with Nuget 3.4 and later: "Settings in this file apply only to solution-wide packages and is supported only in NuGet 3.3 and earlier. It is ignored for NuGet 3.4 and later." ref: https://docs.nuget.org/ndocs/consume-packages/configuring-nuget-behavior – oligofren Oct 10 '16 at 10:45
  • you don't really have to stage the commits if it's causing an issue, at least in VS2017. You can exclude changes to the packages folder from the commit where ignore is added. Then undo those changes and restore packages. Should be good to go after that. – James Pusateri Oct 05 '17 at 13:25
  • 1
    Why, I'm not sure, but I needed this in VS17 and .NET 4.7.1. Thanks so much! I finally have a clean project in TFS ^-^ – CaptainMarvel Feb 09 '18 at 22:12
  • To create the file in my TFS 2015/VS 2017, create the folder ..nuget then RENAME it to .nuget – Brian Leeming May 16 '18 at 20:05
  • Is this still relevant with a git repository today? – SynBiotik May 03 '19 at 15:49
58

An alternative solution to the above is the following.

  • Add the packages folder to TFS (without any files or sub-folders)
  • Right Click the Packages Folder
  • Left Click Advanced
  • Click Cloak

It is worth noting that this solution would need to be applied per TFS workspace. It has worked far more reliably for me rather than using the .tfignore file.

You can read more about this approach in the blog article Prevent TFS from adding installed NuGet packages to source control.

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
24

for people reporting that the .tfignore option wasn't working with the nuget.config setting it might be of interest - these steps finally worked for me:

  1. Delete everything in my packages folder
  2. Make sure TFS doesn't have any changes around that folder pending
  3. Close VS
  4. Re-open VS, and reload solution - using Nuget restore to re-populate packages Note no changes are pending for TFS source control
Adam Stewart
  • 1,983
  • 1
  • 18
  • 25
  • 3
    This worked for me after applying Pharylon's solution, which I did after restoring packages. – user849924 Sep 07 '15 at 09:36
  • 1
    Really Helpful, We must do this for Initial Time(First Commit of the .tfignore file to the TFS), But Not Every one. if i'm correct. – RajeshKdev Dec 29 '16 at 10:53
  • I think this approach will work only on the machine that make the cloak. I'm I right? so this doesn't solve the root problem to all team members. – panox Nov 24 '17 at 15:14
  • A variation of this worked for me - I ensured that nothing from Packages had been committed already, then deleted the packages directory from disk. At this point, it was still showing as a pending changes in the VS team explorer, so I chose to Undo the changes that were pending in the packages directory. This cleaned it up (without me needing to close/reopen), and they're not coming back =) – frax Aug 20 '18 at 17:54
9

Add a nuget.config file in a .nuget folder in your solution. Add the following to the nuget.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>
</configuration>

The disableSourceControlIntegration is what makes the trick for TFS Version Control.

Terje Sandstrøm
  • 1,979
  • 15
  • 14
  • Has no effect - also not a global solution at all. – Jaster Oct 30 '14 at 15:49
  • @So Many Goblins: Have you tried this on a completely new project/sln ? (Just for checking it out) I'm curious what in your setup makes this not work, as it works every time I do it myself, and it is the recommended way of doing this with TFS/nuget. Also, which version of VS/TFS do you see this on? – Terje Sandstrøm Jan 10 '15 at 20:14
  • Visual studio 2013. New solution, aye. I worked around this issue adding a .tfignore ignoring explicitly each package. – SoManyGoblins Jan 11 '15 at 22:06
  • 2
    Okay, this is effectively what "Enable NuGet Package Restore" does. Just be sure to CLOSE and re-open your solution! The NuGet.config file only gets read upon opening your solution. – Riegardt Steyn Mar 24 '15 at 08:09
  • This didn't work for me quite on visual studio online and VS2013. I used Right Click Solution > Enable NuGet Package Restore. This will add the Nuget.config file to solution root Add the .tfignore. I normally do this by adding a text file to the solution root, letting it detect that and then exclude by clicking 'detected add' > right click ignore. Then check everything in, including any packages Then delete all the packages in your solution and check in this change (this will remove the packages from TFS) Open the solution and build which will add the packages but TFS will not pick them up. – David Wilton Jul 16 '15 at 10:38
  • First, Enable Nuget restore is depreceated, and not there in VS 2015, if you go that route later. Second: Checking packages and dlls in will add them to the source control, deleting them in the solution and checking that in will NOT remove them from SC, just from the tip of your branch. – Terje Sandstrøm Jul 16 '15 at 14:46
8

You need to be using local workspaces for .tfignore to work. The .tfignore file must be in the folder that contains the files or folders you want to ignore.

So if your solution structure looks like this:

\Project
   \Packages
   \OtherStuff
   foo.cs

You'd put your .tfignore file in \Project:

\Project
   \Packages
   \OtherStuff
   foo.cs
   .tfignore

The contents of the .tfignore in your case would be:

\packages

Here's some documentation for you: http://msdn.microsoft.com/library/vstudio/ms245454(v=vs.110).aspx#tfignore

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • 2
    I've added the .tfignore files and when I build the project it's still trying to add packages to tfs. There seems little I can do to stop it. – Matt Jun 11 '14 at 08:37
  • Can you post the ignore file you added? – MrHinsh - Martin Hinshelwood Jun 13 '14 at 17:26
  • 2
    See http://docs.nuget.org/docs/reference/package-restore-with-team-build - it's a NuGet bug with .tfignore, but can be corrected by telling NuGet to go shove itself for SCM integration. – user2864740 Dec 29 '14 at 22:01
  • 1
    It looks like you're example might be wrong, It think it should be `packages` instead of `\packages` as it's relative. – Martin Feb 21 '15 at 17:53
6

You can permanently set this once-off in your AppData\Roaming for all solutions (old & new)!

In your %AppData%\NuGet\NuGet.Config file, add the following just before the </configuration> XML tag...

<config>
  <add key="repositoryPath" value="C:\NuGetPackages" />
</config>
<solution>
  <add key="disableSourceControlIntegration" value="true" />
</solution>

...you can specify any path you want - the important thing is putting it OUTSIDE your TFS workspace!

Now you never have to worry about that stuff again. Your solution folder will not contain any packages anymore; all solutions will default to using your custom packages location instead.

NOTE - This works is on a per-user basis.

Riegardt Steyn
  • 5,431
  • 2
  • 34
  • 49
  • 1
    Sounds good and possibly much simpler. You do have to enforce this setup across your team, and especially your build servers' login/setup as well if you take this approach? – Chris F Carroll Jan 11 '16 at 16:21
  • 1
    I would imagine so. I have not tested this WRT build servers, sorry. I would also imagine that different folder paths to the lib folders could cause problems (I think some NuGet packages have hard-references to the lib paths if I recall correctly). – Riegardt Steyn Jan 12 '16 at 09:56
2

Set your solution to restore on build, the package folder and packages file will be checked in but the packages won't.

Just TFS
  • 4,697
  • 1
  • 18
  • 22
  • 1
    Please explain "restore on build". – user2864740 Dec 29 '14 at 21:49
  • 10
    The Visual Studio right-click "enable NuGet package restore" functionality is deprecated as of NuGet 2.7 -- TFS 2013 natively supports restoring NuGet packages without the NuGet.targets file and modifications to the proj files. – Daniel Mann Dec 29 '14 at 22:33
1

If you are using Git with TFS you need to add a ".gitignore" file. You can do this in "team project | Settings | 'add ignore file'". Then open the file and uncomment the built in ignore statement for Nuget Packages.

If you are using TFVC and you have Local Workspaces configured you can use the ".tfignore" file that honours an identical format to the Git file. I think you need "packages/".

1

This didn't work for me quite on visual studio online and VS2013.

  • Right Click Solution > Enable NuGet Package Restore. This will add the Nuget.config file to solution

enter image description here

  • Add the .tfignore. I normally do this by adding a text file to the solution root, letting it detect that and then exclude by clicking 'detected add' > right click ignore.

enter image description here

  • Add the packages to .tfignore and tell it to include repositories.config

enter image description here

From the other comments it seems your milage may vary at this point. This is what I do:

  • Check everything in, including any packages.

  • Delete all packages in your solution and then check in this change (this will remove the packages from TFS)

  • Open the solution and build which will add the packages to the project but TFS will not pick them up.

David Wilton
  • 344
  • 2
  • 11
  • 2
    First, Enable Nuget restore is depreceated, and not there in VS 2015, if you go that route later. Second: Checking packages and dlls in will add them to the source control, deleting them in the solution and checking that in will NOT remove them from SC, just from the tip of your branch. – – Terje Sandstrøm Jul 16 '15 at 14:49
  • 1
    @TerjeSandstrøm point taken, but the question is specific to 2013. Yes you are right, you would have to use tf destroy to remove it permanently from source control, but I found no other way to get TFS to adhere to the .tfignore file – David Wilton Jul 22 '15 at 04:57
1

The solution that worked for me was to create both a .tfignore and the following setting in the Nuget.Config:

<configuration>
  ...
  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>  
  ...
</configuration>

My .tfignore contains this line:

\packages

[I'm using Visual Studio 2015 Update 2]

This is not ideal, and is currently logged as an open issue on github/nuget:

Make it easier to omit packages from TFVC #493

redcalx
  • 8,177
  • 4
  • 56
  • 105
0

Terje's answer doesn't work all the time for me, sometimes it will work for a while, but then it will pend a load of "adds" for me all over again.

The only way I have found to solve this permanently is to Cloak the packages folder in my Workspace.

For example:

Type      Server                Local
============================================
Active    $/Work/Main           C:\Code\Main
Cloaked   $/Work/Main/Packages
Community
  • 1
  • 1
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
0

I had the same issue. /packages should work but didn't for me. packages*.* did work.

coding4fun
  • 8,038
  • 13
  • 58
  • 85