NuGet has become an essential tool in modern .NET development. As a package manager for Visual Studio, it streamlines the process of managing dependencies by automatically restoring required references when loading solutions and building projects. This automation is particularly valuable in team environments, eliminating the traditional headaches of managing reference paths and ensuring everyone has the correct DLLs. The official NuGet gallery offers hundreds of packages, and organizations can even host their own custom package servers.

While NuGet’s package management capabilities are impressive, there’s a common issue that many teams encounter when using it with Team Foundation Server (TFS). By default, NuGet packages get stored in your source control system, creating redundant copies of files that will be automatically restored during build anyway. This practice not only bloats your repository but also complicates version control with files that don’t require historical tracking.

The Solution

Fortunately, NuGet 2.7.2 introduced a solution to this problem. You can disable source integration for packages, preventing them from cluttering your source control while maintaining the benefits of automatic package restoration. This approach keeps your repository clean and efficient while ensuring that all team members can still build the project successfully.

Implementation Steps

To implement this optimization, follow these steps:

  1. Enable Package Restore on Solution: Right-click on the solution and select the option to restore packages automatically.
  2. Delete NuGet.exe and NuGet.targets. Ensure NuGet.config contains a key that says “Disable Source Integration” and set it to True.
  3. Save your Solution and exit Visual Studio.
  4. Open the project file in your preferred text editor (vbproj or csproj).
  5. Search and remove all lines that say “NuGet.targets”. There typically is only one.
  6. Delete the contents of the packages folder.
  7. Open your solution in Visual Studio.
  8. Build the solution - it should restore all packages automatically.
  9. Check in to TFS. This should exclude all of the packages that were just installed.

Benefits and Considerations

This optimization offers several advantages for your development workflow. Your TFS repository becomes more manageable, with reduced size and cleaner version history. Team members will experience faster checkouts and updates since they’re not downloading package files through source control. The build process remains reliable as packages are automatically restored when needed.

It’s important to note that while packages are excluded from source control, the packages.config file should still be included. This file tracks your package dependencies and ensures consistent package versions across the team. The automatic package restoration process is transparent to developers, so there’s no additional overhead in the daily workflow.

Best Practices

When implementing this change, consider these best practices:

  • Communicate the change to your team to ensure everyone understands the new workflow
  • Keep the packages.config file in source control
  • Regularly update your packages to maintain security and access new features
  • Consider implementing a package restore policy in your build process
  • Monitor the package restoration process during builds to ensure reliability

By following these steps and best practices, you can maintain a clean and efficient source control system while still benefiting from NuGet’s powerful package management capabilities. This approach represents a best practice for managing NuGet packages in TFS environments, especially as your project grows and accumulates more dependencies.

Note: Always test the package restoration process thoroughly after implementing these changes to ensure your build process remains reliable.