5

I have Visual Studio 2015 Enterprise with Update3. In one VS solution i have 3 different projects. 2 projects are ASP.Net Core and 3rd project is classic .Net 4.6.2

All 3 projects have nuget packages and i want to store them at one location relative to solution file. (eg C:Root\Lib\packages) Certain packages will shared between 2 ASP.Net Core projects. However note that i am NOT trying to share a package between ASP.Net Core projects and classing .net 4.6.2 project.

So i put nugget.config file as shown below at the C:\root.

 <?xml version="1.0" encoding="utf-8"?>
 <configuration>
    <config>
        <add key="repositoryPath" value=".\Lib\packages" />
    </config>  
  </configuration>

The above settings stores the nugget packages for .Net 4.6.2 project into C:\Root\Lib\packages\ folder. Exactly where i wanted.

However the ASP.NET Core projects does not respect repositoryPath. I found discussion here that says

project.json project doesn't support repositoryPath config now, if you want to change packages default location, you can set "NUGET_PACKAGES" environment variable to do that.

So on both the ASP.NET Core projects i added NUGET_PACKAGES environment variable and set the value to .\Lib\packages and clicked on restore packages. However that still restores ASP.NEt Core packages to C:\Users{user-name}\ .nuget\packages

Update1
I set environment variable in ASP.NET Core project's property page.
Right Click on Project ->Properties->Debug->Add

enter image description here

Update2

so based on the suggestions below I added NUGET_PACKAGES environment variable via windows at user level. and sets its value to C:\Root\Lib\packages folder. and also deleted 'nuget.config file from C:\root I noticed only asp.net core projects which has package.json file honors the environment variable. The classic .Net 4.6.2 project restore packages to their default location, it does not honor NUGET_PACKAGES variable.

2nd approach I tried
Also as per the documentation here under Environment variables in configuration section, we can now use environment variable in nuget.config file. I am not able to get this working for any project.
1. I removed NUGET_PACKAGES environment variable from windows.
2. Add new variable 'MyPath' with value C:\Root
3. Add nuget.config file at c:\Root

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <config>
        <add key="repositoryPath" value="%MyPath%\Lib\packages" />
    </config>  
</configuration>

Doing so asp.net core projects restored the package to C:\Users{user-name}\ .nuget\packages that is not what i have configured.
While classic .net 4.6.2 project gives error The given path format is not supported

LP13
  • 30,567
  • 53
  • 217
  • 400
  • @LP12, if you have an existing packages folder under your solution root, please delete it before NuGet will place packages in the new location, and then check it again. Is your Nuget 3.4 x version? Do you mean that you add the environment variables in the Nuget.confi file? http://docs.nuget.org/consume/nuget-config-file – Jack Zhai Aug 11 '16 at 04:25
  • @JackZhai I'm using Nuget version 3.5.0.1484 ( VS 2015 Update 3). I added Environment variable by right clicking on project ->properties->debug->add environment variable. ( see update 1 above) – LP13 Aug 11 '16 at 06:29
  • Environment variables added via the project properties are only available at runtime. You need the environment variable available at the time the NuGet packages are restored, prior to building the project. So add the environment variable via Windows, not Visual Studio. You'll probably need to restart Visual Studio before it's picked up. – Andy Lamb Aug 11 '16 at 07:09
  • If I add the environment variable at machine level wouldn't that affect all the projects on that machine? including `classing .net` projects as well as `.net core projects` – LP13 Aug 11 '16 at 15:35
  • Also with environment variable approach all the developers on the team have to add this environment variable on their machine. Does not make sense. With nuget.config file approach, we had source controlled the nuget.config file and that approached worked for all developers. – LP13 Aug 11 '16 at 15:53
  • please see update2 – LP13 Aug 11 '16 at 18:20
  • @LP13, It is the system environment variable which sets a global packages path: http://stackoverflow.com/questions/35710866/can-the-dnx-dotnetcore-package-cache-location-be-changed. In addition, the path /.nuget\packages should be a local machine cache: http://stackoverflow.com/questions/35283331/nuget-creating-two-package-folders – Jack Zhai Aug 12 '16 at 04:44

1 Answers1

1

You could use "NUGET_PACKAGES" variable to config the repository path for Asp.Net Core project and also place a Nuget.Config file in the root folder to control the repository path for classic .Net project. It works at my side.

The only issue is that all the developers need to add this environment variable just as you mentioned to achieve this. There isn't any way to avoid this for now as I know. However, if your team members don't want to change the location of the packages for Asp.Net Core project, then they don't need to add this environment variable. The packages will be restored to the default path on their machine and the .Net Core project could find and use them automatically.

Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60