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

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.

Related

Default package source for NuGet defaulting to newly added source and not All or nuget.org

I added a NuGet 'Package source' to Visual Studio since we have an internal server at work.
Visual Studio now always defaults to my internal server and cannot find most of our NuGet packages until I switch the package source to All or nuget.org.
This is a minor inconvenience, but for the past few weeks, I have been doing many more clicks every time I need to work with the NuGet package manager. Can anyone tell me how to default to All instead of having it always default to Internal nuget?
Can anyone tell me how to default to All instead of having it always default to Internal nuget?
To my knowledge, the default package source of NuGet Package Manager depends on the order of package source and the property of activePackageSource in nuget.config.
The order of package source:
When you open NuGet Package Manager setting, Tools->Options->NuGet Packager Manager->Package Source, you will notice that there are up and down arrows for package source:
NuGet Package Manager will give priority for default NuGet package source in the top of the package source list. For example, if I set NuGet package source LocalServer at the top of that list, when I create a new project, the default NuGet source will be changed to LocalServer:
However, this priority of default NuGet source can easily be broken by the property of activePackageSource in nuget.config.
Open the nuget.config file from %appdata%\NuGet\NuGet.config, add the following code:
<activePackageSource>
<add key="LocalServer2" value="D:\LocalServerTest" />
</activePackageSource>
Restart Visual Studio, then create a new project, you will find the default NuGet source change to the LocalServer2:
So, if you want to default nuget.org instead of having it always default to Internal nuget, you can set the NuGet source nuget.org at top of NuGet source list or set the nuget.org as activePackageSource in the nuget.config:
<activePackageSource>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</activePackageSource>
Update for comment:
Is there no way to default to All? Maybe you can suggest defaulting to All (looks like you work for MS)
if you want to set the default NuGet source to All, you can use the setting below in nuget.config:
<activePackageSource>
<add key="All" value="(Aggregate source)" />
</activePackageSource>

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.

NuGet packages getting installed to the wrong folder

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.

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.

Am I doing this right? Nuget Package Restore with centralized packages folder

I placed a nuget.config file in my project root folder that specifies where all NuGet packages should go for all my solutions.
Here is the file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="D:\Projects\Development\NuGet Packages" />
</config>
</configuration>
I have also enabled NuGet Package Restore on all my solutions.
When I first set this up, the nuget.config file was being ignored--all NuGet packages were being placed in a "packages" folder in the solution folder. I tried deleting this folder numerous times, but invariably, NuGet would recreate it and fill it with packages.
After much trial and error, I stumbled upon the <PackagesDir> element in "NuGet.targets" and noticed that commenting it out finally allowed the packages to be placed in the correct folder in my project root.
Here's the relevant line from NuGet.targets
<PackagesDir>$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir>
My question is simply whether commenting out the PackagesDir element in the NuGet.targets file is the intended method to get my nuget.config file to be followed or if I'm missing a more obvious solution.
There are two things in play in your question:
Where does NuGet store packages?
NuGet package restore for the open project(s) / solution
Regarding the first: setting the repositoryPath key in your nuget.config file is the correct approach to instruct NuGet to extract packages in a different folder from the default. This setting is used whenever a developer adds a package refernce to your project.
For NuGet package restore, thissetting is overridden by the setting you found in NuGet.targets. This setting is used when restoring packages upon build of a solution.
If you want to use package restore and a custom path, you indeed have to specify both. The first one for your devs, the second one for package restore.

Resources