Nostromo
Nostromo

Reputation: 1264

Nuget package content in Visual Studio

I created a small project in Visual Studio 2019 that uses the content of a NuGet package (SQLite). When I build my project in Visual Studio and run it in debug mode, everything works fine. But when I copy the built files from the output directory to another directory or to another computer it fails, telling me, that it's not finding files from the NuGet package.

For the NuGet package I'm using it's missing two files. When I'm copying these two missing files from the NuGet package directory to my project files, it's working again.

But is this really the way to go? Let's say my project is getting bigger and is needing more and more NuGet packages, do I really have to guess for every NuGet package, which files I might need from it and where I might find these files on my hard drive?

Another thing is that I found the files from the NuGet package somewhere in my Windows user directory, not in my code directory. So, if I'll archive my code and give it to someone else or use it some time later, the NuGet packages are missing from the code.

So, here are my questions:

1) Is there a way to tell Visual Studio to copy all the necessary files from all used NuGet packages to the output directory automatically?

2) Is it possible to store the downloaded NuGet packages somewhere closer to my source code instead of in my windows user directory? I'd like to just archive one directory to backup all of my source code.

I took a quick look on post build events to maybe copy all the missing files to the output directory in a post build event, but that's can't be the way to go, since I still need the knowledge exactly which files I need to copy and where to find them, and the path to said files might change from computer to computer.

Upvotes: 1

Views: 1533

Answers (2)

Nostromo
Nostromo

Reputation: 1264

Now I was able to take a look at Perry's comments and answers, but I have to say... NuGet is not even close to being convenient to use or close to work as one would expect it to.

Adding

<PropertyGroup>
    <CopyLocalLockFileAssemblies>True</CopyLocalLockFileAssemblies>
</PropertyGroup>

to my project file did indeed copy the referenced file from the NuGet package (SQLite) to the build output folder. One of the necessary files was missing, but I guess that is a problem of SQLite, not NuGet (I do not directly reference this file, but if it is missing, an error message tells me so).

But I was not able to get the NuGet packages to be stored closer to the source code. I know, Nuget is able to do so, because the NuGet packages for NUnit are stored in a folder called "packages" right inside the project folder.

But adding

<config>
    <add key="globalPackagesFolder" value="xxxxx(the custom path)" />
</config>

to the NuGet.config file (like suggested), re-starting Visual Studio and re-building my project did add a lot of packages to my custom path (130 MB), but not the only package I'm referencing on purpose (SQLite).

Upvotes: 1

Mr Qian
Mr Qian

Reputation: 23740

I think you are using PackageReference Nuget Mangement format to manage your nuget packages in your projects.

PackageReference nuget format directly references the nuget from the local nuget cache and will not copy the nuget packages into your project. It seems like Link reference. It is the feature of PackageReference.

When you move your project into another agent and then want to use its content, you should make sure that the nuget package is still in your current agent environment.

So, first of all, you should do a nuget restore. And then you can get the missing packages back on your agent.

Right-click on your solution on the Solution Explorer-->Restore Nuget Packages

Is there a way to tell Visual Studio to copy all the necessary files from all used NuGet packages to the output directory automatically?

Actually, previously, there was a problem with packagereference that from time to time, the content in the nuget package was lost and could not be completely copied to the output path. See this.

So you could try to add this in your xxx.csproj file:

<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

Is it possible to store the downloaded NuGet packages somewhere closer to my source code instead of in my windows user directory? I'd like to just archive one directory to backup all of my source code.

Windows user directory is nuget global caches and it will download all the packages in such folder in your agent and then projects references the nuget package from there.

And also note that:

It works according to the current user rather than the current machine. It is designed by that. All nuget packages are stored in the current user-level directory. Switching OS accounts cannot get the nuget packages in the other user directory.

And if you want to modify its path, you should modify the Nuget.Config(C:\Users\xxx(use r name)\AppData\Roaming\NuGet\NuGet.Config)file:

add these node:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
....
<config>

    <add key="globalPackagesFolder" value="xxxxx(the custom path)" />
</config>
....

</configuration>

And then restart VS and nuget packages which you install later will be installed in such package.

Update 1

First, please check your agent and make sure that the Internet can access fast.

For that paid nuget packages, you should do other operation and then you can install it from the nuget server.

If the nuget package needs to be paid or be deleted from the nuget server, restore operation will not find the package or need your further extra step. And then restore operation will succeed.

For those old abandoned nuget package, it exists under your old agent's global nuget cache, so in your old agent, you can find it.

But when you migrate your project to the new agent, since the nuget package is removed from the nuget server, so the new agent will not find it and cannot restore such package. If you still have the old nuget package in your old agent, you can move to the new environment and then need to configure its new address to the nuget package source, and then restore operation will succeed.

After all, you should make sure that every nuget package can be found and have no extra requirements on nuget.org server or do some operation to them since your project is too old and the nuget packages have had big changes. You should check them carefully.

Upvotes: 1

Related Questions