Search code examples
visual-studionuget

Nuget packages are installed outside of the project solution


Let's say I have a solution called MyFirstSolution and inside I have project with name MyFirstProject. Inside that project I'm installing nuget packages like Dapper, EntityFramework etc...

How things should work: packages folder should be created into the folder MyFirstSolution and inside should be downloaded and installer all nuget packages referenced into the project.

The problem I'm having is that the packages folder is not created and the nuget packages are downloaded one level above the MyFirstSolution folder into some folder called XYZHelper.

When I download a solution from some repository nuget packages are restored to that same folder, not to the solution itself and references in csproj file says that they should be in solutionFolder\packages so all of my projects don't build.

If I copy the packages manually to the folder that they should be, it's all good but after every build a copy is made into that XYZHelper folder.

I don't have any postbuild events or anything like it configured into the projects. It is something with the visual studio I guess, but I can't figure out what.


Solution

  • Target folder of nuget packages can also be set using the envirenment variable NUGET_PACKAGES.
    And you can do this in your existing project-file like this:

    <Project Sdk="Microsoft.NET.Sdk">
       ...
       <Target Name="NugetPublicfolder" BeforeTargets="PreBuildEvent">
          <Exec Command="SET NUGET_PACKAGES=C:\MyProjectA\libraries\"/>
       </Target>
       ...
    </Project> 
    

    Now when you build/publish the project the libraries will be placed in C:\MyProjectA\libraries\

    Actually the nuget packages are first downloaded to the socalled http-cache folder (%userprofile%\AppData\Local\NuGet\v3-cache), where it will be extacted from to the above folder (which is called the globalPackagesFolder folder)