Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015 does not respect nuget's repository path for ASP.NET Core projects

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

like image 764
LP13 Avatar asked Nov 09 '22 10:11

LP13


1 Answers

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.

like image 52
Eddie Chen - MSFT Avatar answered Dec 28 '22 05:12

Eddie Chen - MSFT