BaltoStar
BaltoStar

Reputation: 8997

.NET Core pull-down NuGet packages to User cache

I have a .NET Core 2.2.0 app that references various NuGet packages.

.NET Core relies on PackageReference style packages defined in .csproj project files. It does not rely on packages.config

Nevertheless I expected Restore NuGet Packages ( or Build ) to pull-down all referenced packages and their dependency packages into the project packages folder ( peer level to .csproj or .sln file ).

I expected this because my understanding is that .NET Core follows many of the conventions of open-source package managers such as npm that operate on the principle of self-sufficient app domains.

However the vast majority of the packages are pulled-down to this folder :

C:\Users\username\.nuget\packages\

( A couple packages are pulled into the project packages folder - and I don't see what about these packages is different. )

Apparently Visual Studio and/or NuGet prefers to avoid duplication of effort and cache almost all packages centrally.

Why is this ? And can it be configured differently ?

Upvotes: 0

Views: 341

Answers (1)

omajid
omajid

Reputation: 15223

First, the "open source package manager" behaviour you describe is not really an "open source package manager" behaviour. It's an implementation of some package managers. Java's Maven (mvn), for example, does the same thing as .NET Core and you can't claim maven is closed source.

Second, some package managers, such as npm copy over all dependencies into a package directory (eg, node_modules) because they use sources, not binaries, and because they grab transitive sources and multiple dependencies same names but different versions. NuGet doesn't do that. It uses binaries only and doesn't support multiple versions. NuGet only resolves exactly one version for each dependency in your project. See this stackoverflow post for more information. The exact version resolved and used is written out to the .deps.json file in your build directory.

Third, .NET Core's Self-Contained Deployments - which are meant to be executible/distributable without any other additional dependencies - don't make use of the package cache. When you publish your application (dotnet publish on the command line), all the dependencies are copied to the publish directory. The version is that in the deps.json file, like I said above.

So the main nuget package cache - which is shared on your system with other projects - is just a simple cache of binaries for .NET Core. There's no reason it has to be unique to your project to copied over to your project directory.

Upvotes: 1

Related Questions