NuGet packages getting installed to the wrong folder - visual-studio

I'm working on a Xamarin.Forms PCL mobile app in Visual Studio 2017, using project.json for package management (I'm not using PackageReference, since Visual Studio 2017 is required for that, and some of our team are still using Visual Studio 2015). I have multiple projects within the solution, and I have multiple branches of the project, like so:
MobileApp/
packages/ <<--- (I want nuget packages to be installed here)
Branches/
DevBranchSolution/
MobileApp.sln
nuget.config
ProjectA/
ProjectB/
I want all my (projects / solutions / branches) to be able to reference packages from a single location, so you'll notice I've added the packages folder at the root level in the MobileApp folder. I have a nuget.config file per solution that looks something like:
nuget.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="..\..\packages" />
</config>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="CustomPackagesLocation" value="..\..\packages" />
</packageSources>
<disabledPackageSources />
</configuration>
In Visual Studio when I right-click on the solution, click "Manage NuGet Packages For Solution...", and install a package (e.g. Newtonsoft.Json), I would expect that it would install those package files inside my MobileApp/packages/ folder, the location I set in the solution's nuget.config. But it doesn't. Instead the files are getting put into the global NuGet packages location, which is %USERPROFILE%\.nuget\packages.
Why? Shouldn't my nuget.config file be overriding that? I have verified that when I go to Package Manager Settings, the location of CustomPackagesLocation is correct, but apparently the repositoryPath setting doesn't seem to affect anything.
I also noticed that inside the project.json.lock and Project.nuget.targets files, the package folder is set to the global NuGet packages location (the %USERPROFILE%/.nuget/packages one). Why? Where is it pulling this value from??

Why? Where is it pulling this value from??
The default packages directory for Project.json file is %USERPROFILE%/.nuget/packages and project.json project doesn't support repositoryPath config now. This is the reason why you have changed the repositoryPath, but NuGet still put packages into the global NuGet packages location. You can refer to the same issue on GitHub.
If you want to change packages default location for project.json, you can set "NUGET_PACKAGES" environment variable. Just Set "NUGET_PACKAGES" = "....\packages". Or you can place a NuGet.Config file next to the solution with the following content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value="..\..\packages" />
</config>
</configuration>
See NuGet.Config reference for details on config section.

I had a very similar problem where it wasn't using the packages folder for a class library. For some reason my csproj file had set for a few assemblies. I removed this line and did a update-package -reinstall -project myclasslibrary and it worked again just fine.
I'm not sure what set the HintPath in the first place.

Related

How to change the NuGet-package reference in a Visual Studio project to use Nuget.config

I have a Visual Studio project file with the extension .csproj. Inside it are references like this:
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props"....
I have now made a NuGet.config file in the parent folder of from the solution folder. And I removed the local "packages" folder. In the new nuget.config I set up a common location for storing packages.
nuget.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="D:\Data\NuGet" />
</config>
<packageRestore>
<add key="enabled" value="True" />
</packageRestore>
</configuration>
When I building I now get this error:
This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information...
The missing file is ..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props.
How can I solve this?
If I manually have to replace the (Import Project="..\packages...) elements in the project file, what should I change it to, so that it follows the configuration from the Nuget.config?
If I manually have to replace the (Import Project="..\packages...)
elements in the project file, what should I change it to, so that it
follows the configuration from the Nuget.config?
Since you use the new nuget.config file which changed the path of the local nuget reference(like this <add key="repositoryPath" value="xxxxxx" />).
And Restore will only restore the missing nuget packages but will not change to use the new nuget package location in xxx.csproj.
So you can follow my steps to resolve the issue:
Solution
1) Tools-->Nuget Package Manager-->Package Manager Console-->
type Update-Package -reinstall to reinstall these packages to reference the new right path.
2) enter the xxxx.csproj file, delete these duplicate, old import info like these:
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\..\..\..\..\installed_packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props'))" />
3) Then rebuild your project and will solve this issue.
Update 1
The new Nuget.config file will specify that the newly installed nuget packages use the new reference address, but for previously installed nuget packages, the reference address in the xxx.csporj file will remain the old address. The Restore procedure only restores the nuget package under the new path, but it does not make any changes to the nuget reference in xxx.csproj file, so it can only be reinstalled.
Besides, the import node is created by Microsoft.Net.Compilers props file from the build folder in the microsoft.net.compilersnuget package. And it is a nuget mechanism which can do some operation in xxx.csproj file when you install the nuget package.
However, this file is also special and when you change the nuget reference path.
Because nuget enabled the new address mechanism, during the uninstallation process, the old address of Microsoft.Net.Compilers.props is still not recognized, so it cannot be uninstalled. In fact, when you execute the reinstall nuget package, a new address has been created in the xxx.csproj file. See this:
So you should just delete duplicate files from the old address.
Visual Studio option to change the Nuget Package References
In Visual Studio Tools=> Nuget Package Manager => Package Sources.
You can change the package sources here.

Change NuGet package location folder

I want to change NuGet package folder, but it does not change it.
What I do is creating file nuget.config:
<configuration>
<config>
<add key="repositoryPath" value="C:\projects\" />
</config>
</configuration>
I added this file in the solution folder (in same folder where is .sln file) or in the project folder and after that restart VS, but nothing happen.
I am using Visual Studio 2017 Community.
Change NuGet package location folder
Depending on what sort of project you are using this setting may or may not be successfully to change NuGet packager folder.
If you are using a .NET Framework project that has a packages.config file then this setting will change the nuget package folder to C:\projects\.
But if you are using a project.json file, then this setting will not successful. Because project.json project doesn't support repositoryPath config.
To change the nuget packager folder, you can you can set "NUGET_PACKAGES" environment variable. Just Set "NUGET_PACKAGES" = "c:\teampackages". Or you can place a NuGet.Config file next to the solution with the following content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages" />
</config>
</configuration>
For the detail info, you can refer to this thread:
Dotnet restore does not honour nuget.config 'repositoryPath'.
Update:
I noticed that you are creating Xamarin.Forms project with Visual Studio Community 2017, the reference should be PackageReference, for this sort of project, you should use add below code to the .csproj file:
<PropertyGroup>
<RestorePackagesPath>D:\Test\packages</RestorePackagesPath>
</PropertyGroup>
Then restart Visual Studio, VS/NuGet will restore nuget packages to the D:\Test\packages, it works fine on my side, you can check my test sample:
Hope this helps.
In Tools/Nuget Package Manager/Package Manager Settings
Options

Manage Nuget Packages Outside Visual Studio

My organization wants to segregate all the development machines on a network without internet access.
I found this article that gives that gives some nuget host product, so that the packages are available offline.
My problem is that I can't find a way to manage the package update, because the machines that have and internet access won't have Visual studio installed.
I was looking if there is a tool that reads a folder where all nupkg files are stored and check if a newer version is available and downloads it, or otherwise reads a manually created packages.config file checks for newer version and download them on a folder.
Does anyone have an idea how to manage nuget packages in this way? I spent the last week trying to find a way but I had no look.
Does anyone have an idea how to manage nuget packages in this way?
According to the NuGet CLI reference:
The update command also updates assembly references in the project
file, provided those references already exist.
So when we use NuGet.exe update the package, we are not only need the packages.config but also need the solution/project, otherwise, you will get the error:
"Unable to locate project file for 'D:\NuGetServer\packages.config'
You can copy a simple project from the machine, which have Visual Studio installed, then use below command line to update the nuget package in the package.config file:
nuget update "YourProjectPath\packages.config"
But NuGet will update the packages into the packages folder under the solution folder by default, so we need change the packages folder to the folder where all nupkg files are stored before update packages.
Detail steps:
Download the nuget.exe from nuget.org, set it to your local machines.
Create a NuGet folder under the path %appdata%, add the NuGet.Config file and change the packages folder by repositoryPath, just set it "D:\NuGetServer":
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<config>
<add key="repositoryPath" value="D:\NuGetServer" />
</config>
</configuration>
Copy a solution from other machine, add the packages in to the package.config file:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="NUnit" version="3.7.0" targetFramework="net45" />
</packages>
Open a CMD file, switch to the path where NuGet is stored in step 1, then use the update command:
You will find packages in the packages.config are updated to the latest version.

MSBuild Macro for NuGet package directory

I'm working on a NuGet package that adds a step to the build process by using a .targets file.
I need to reference other files from my NuGet package in order to complete the build successfully.
In the past, I've used $(SolutionDir)packages\MyPackage and all has worked fine.
However, I was just playing around with the VS 2017 RC, and I noticed that my package was installed in the global NuGet package directory, not in the solution folder.
Is there some macro that I can use from MSBuild, that contains the path for the NuGet packages folder? It is a requirement that I maintain compatibility with VS2012.
The $(NuGetPackageRoot) macro points to the package root.
You can use an alternative method that create a NuGet.Config file in the root of the \Solutions\ folder to set the package repositoryPath of VS 2017 RC, add to NuGet.Config the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="$\..\Packages" />
</config>
</configuration>
For the repositoryPath setting, you can specify an absolute path or relative path (recommended) using the $ token. The $ token is based on where the NuGet.Config is located. In this case, you package will install in $(SolutionDir)packages\ folder, you can used $(SolutionDir)packages\MyPackage for Visual Studio 2017 RC. It`s also maintain compatibility with VS2012.

Change the location of packages for NuGet using the release note 2.1 doesn't work

I am trying to change the location of the default package folder for NuGet
I read many posts including the documentation of the NuGet 2.1 Release Notes, the new config for NuGet version 2.1 as the following:
<configuration>
<config>
<add key=" repositoryPath" value=" C:\myteam\teampackages" />
</config>
...
</configuration>
I also read the following thread, Is it possible to change the location of packages for NuGet?
But this configuration doesn't work?
The only config work is the old one which is as the following:
<settings>
<repositoryPath>C:\myteam\teampackages</repositoryPath>
</settings>
I am using Visual Studio 2012 Update 1 that including NuGet Package version 2.1
Steps to reproduce:
in solution directory, create a file "nuget.config"
edit nuget.config and add:
<settings>
<repositoryPath>C:\myteam\teampackages</repositoryPath>
</settings>
Delete the default packages folder
In visual studio, right-click on Solution, and select Manage NuGet
Packages Install a package (any package)
Verify that package was downloaded to C:\myteam\teampackages
delete the downloaded package in C:\myteam\teampackages
change the nuget.config to the following:
<configuration>
<config>
<add key="repositoryPath" value="C:\myteam\teampackages" />
</config>
</configuration>
Try to install any library again.
I find the library installed in the packages folder not my folder???
>>steps to reproduce:
>>in solution directory, create a file "nuget.config"
From the release notes it looks for nuget.config in the following order
.nuget\nuget.config
Recursive walk from project (.nuget) folder to root
Global nuget.config (%appdata%\NuGet\nuget.config)
So if nuget.config is in the project/solution folder it won't be honored. Can you try moving it to .nuget folder and reload the solution.
It works when you place a \ at the end of the path:
<add key=" repositoryPath" value="C:\myteam\teampackages\" />

Resources