Disabling NuGet Package Restore

Yes, this is counterproductive and a step backwards, but if you are working in an environment where your build/CI server doesn't allow NuGet Package restore then you will need to disable it. This may sound straightforward and something that should be easily supported by NuGet, but I'm afraid you are out of luck. To disable NuGet package restores, you will need to perform some manual changes to your .csproj files

To fully remove package restore just follow the steps below:

1. Close down the solution

Before performing any project file changes, you will need to close down the solution in Visual Studio. VS2010 can be particularly notorious as it seems to cache csproj/msbuild files and your changes will be overwritten, unless the solution is shut down.

2. Remove the .Nuget folder

When you enable package restore at solution level, a .nuget folder is added at the root level of the solution. Delete this folder.

3. Edit all .csproj files that use NuGet

Open each .csproj file in the editor of your choice and remove the following settings:

<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />

Now you can re-open the solution again and perform a build to ensure that everything is working as expected.

NOTE: Remember to check-in/push your changes to the build server as well.

Happy coding...

  • Share this post on