Nuget package restore not respecting my repository path configuration? - nuget

I have the following setup in my solution's .nuget/Nuget.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<config>
<add key="repositoryPath" value="C:\Xos\packages\" />
</config>
</configuration>
The point of this is to try and make Nuget package restore manageable, and share packages across our many solutions. I then went into my current solution's packages directory and deleted all the packages (so they would instead restore them to C:\Xos\packages). I then closed VS 2012 and reloaded the solution and rebuilt.
However, all my packages are being restored in my solution's packages directory, NOT the directory specified in repositoryPath configuration.
What am I doing wrong?

I had the same issue with VS2015 RC. One of two things suddenly made it work:
(1) I checked-out the project file from source control, and completely closed Visual Studio (not just the solution).
(2) I edited the csproj file and noticed that the entry for packages.config had a <SubType>Designer</SubType> inner tag, which I removed.
I suspect it was the close-down of Visual Studio...

This appears to be a bug and is fixed in 2.3 (which doesn't appear to be released yet).
Until then I modified my Nuget.Targets as described in that bug and using that for now.

Related

How do I include NuGet packages in my solution for .Net Core projects?

With classic .Net projects, if I added a reference to a NuGet package, it would get downloaded to a packages folder and I could check that into source control along with the rest of my code. This allowed any developer to download the code, along with the NuGet packages, without having to set up a package source to separately download the packages. This is not how .Net Core projects work. There does not seem to be a packages folder for the solution, and it is up to each developer to set up the custom package source and download the packages when they get the code. Is there a way to configure the .Net Core project to do like the classic .Net projects did and manage a packages folder?
A lot of NuGet behaviour can be controlled via NuGet.Config files (See this reference for more details)
If you place a NuGet.Config file next to the solution with the following content, you can override the location that the packages will be restored into:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages" />
</config>
</configuration>
If the problem is that you'd need to set up additional sources in VS on every machine, you can also add those sources via a NuGet.Config in your repository so VS will pick up the feeds to use when opening a solution:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="CompanyFeed" value="https://my.company.com/private/nuget" />
</packageSources>
</configuration>
If you have no feed to host packages and need to include packages with the solution, you can use a directory containing .nupkg files as well in NuGet.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value=".\NuGetPackages" />
</packageSources>
</configuration>

Add custom package source to Visual Studio Code

Does anybody know how to add custom package source to Visual Studio Code?
E.g. I'd like to add https://www.myget.org/F/aspnet-contrib/api/v3/index.json as a package source and drive these packages through project.json.
To add to the answer, adding a nuget.config in the project solves it for the project. Adding to the root is ok. The config could look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyGet" value="https://www.myget.org/F/aspnet-contrib/api/v3/index.json" />
</packageSources>
</configuration>
Another option that worked for me and was actually needed as part of my CI/CD pipeline is to use dotnet nuget add source <repo-url> --name <repo-name>
Simply call that before you call dotnet restore or dotnet build
Reference:
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-add-source
Note - if your custom package source is password protected, then the credentials should also be part of the config file.
<packageSourceCredentials>
<MyGet> <!--package src name-->
<add key="Username" value="something" />
<add key="ClearTextPassword" value="thepassword" />
</MyGet>
</packageSourceCredentials>
And for anyone wondering why the credentials have to be in clear text (which defeats the whole purpose of having credentials in the first place, since this config file will also go into source control).
This is an open issue in dotnet / nuget cli. Ref github issue # 5909 & 1851
You can add a NuGet.config file and specify the package source in there. Some reference docs: https://learn.microsoft.com/en-us/nuget/schema/nuget-config-file

Can I get the location of the packages folder for the current solution when applying an xdt?

I want to install a design time plugin for a visual studio extension (ie runs when using visual studio, and not when the application is running). This requires that I modify the app.config with an entry that points at the dll containing the extension code. The plugin is installed via nuget and if I add this dll to a folder under the project and use a fixed path in the app.config.install.xdt then everything work ok. However what I want is for the xdt to insert a value which points to the dll in the packages folder, where it gets installed via nuget. My problem is that the relative path to the nuget folder is not fixed for the projects. Each project may be in a different folder (several folders deep inside the solution folder) and not just in a direct child of the solution folder, so I want to be able to use some variable in my xdt.
Currently I have this in my xdt:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<specFlow>
<plugins xdt:Transform="InsertIfMissing">
<add name="NCrunch.Generator" path=".\SpecflowPlugins\" type="Generator" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
</plugins>
</specFlow>
</configuration>
but I want something like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<specFlow>
<plugins xdt:Transform="InsertIfMissing">
<add name="NCrunch.Generator" path="$(SolutionDir)\packages\Specflow.Ncrunch.1.0.0\lib\net" type="Generator" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
</plugins>
</specFlow>
</configuration>
is this possible in xdt? if not is there another option for getting this to munge the app.config correctly when my nuget package installs?
The XDT syntax doesn't allow replacement tokens such as the one you put in your example.
One option I can think of would be to modify the app.config.install.xdt file in your init.ps1 PowerShell script, replacing the tokens for the actual values before the XDT transform is applied by NuGet.
For more information regarding init.ps1 see: Creating and Publishing a Package

Can't specify nuget package folder location [duplicate]

We use a custom location for our packages folder which we specify in a nuget.config file in the same folder as our solution:
<settings>
<repositoryPath>..\..\lib\packages</repositoryPath>
</settings>
Visual Studio 2013 picks this up fine and the NuGet package manager installs packages into the specified folder, lists installed packages correctly, etc.
In Visual Studio 2015 RC the NuGet package manager pops up the "Some packages are missing from this solution, click here to restore" message and if I click the button it creates a new packages folder in the same folder as the solution rather than using the location specified in the nuget.config. Installing a completely new package also puts it into a packages folder under the solution folder rather than the specified one.
How do I get Visual Studio 2015 RC to respect the repository path specified in the nuget.config?
Make sure your nuget.config is configured like this:
<configuration>
<config>
<add key="repositoryPath" value="..\..\lib\packages" />
</config>
</configuration>
I filed this bug with NuGet: https://github.com/NuGet/Home/issues/626
A fix has been made but I'm not sure when it will be released.

Is there a way to automatically update nuget.exe in the .nuget folder when using package restore?

My team has been using the Enable Package Restore option since Nuget 1.5 to keep packages out of our source control. When Nuget 1.6 was released we noticed an issue where it wasn't pulling the packages down, and tracked it down to the Nuget.exe in the .nuget folder needed to updated to 1.6 to match the Package Manager.
What's the best way to update a solution once Nuget has been udpated? I don't see an easy way from studio to tell which version of nuget is in the solution folder. So far we blow away the .nuget folder and re-run the Enable Package Restore command.
I would suggest updating .nuget\nuget.exe with this command from the command line:
nuget.exe update -self
[EDIT] : Close VS Solution first. If there's an update and the solution is opened, nuget.exe will be removed from the solution.
You could automatically update nuget.exe on restore by modifying the .nuget\nuget.targets to add the above command. I'd look at the restore command in there as an example. But I'm not sure if it's worth it, nuget.exe updates aren't that common, and backward compatibility should break very rarely.
I've found the best way to handle this is by simply deleting the .nuget folder and re-enabling solution wide package restore. As said above, you could add a self update command to your build, but that will not update the targets or config files if there are changes between versions (or remove the reference from your solution). Perhaps its not that big of a deal, but this is the sure fire way to make sure you have the latest exe and configuration files. And at the end of the day, updating is only an issue if you need access to a new command or there is eventually a breaking change in a new release.
See David Ebbo's post at his blog
Basically, All you need to do is create a NuGet.Config file next to your .sln file, containing:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear/>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="aspnetwebstacknightlyrelease" value="https://www.myget.org/f/aspnetwebstacknightlyrelease/" />
</packageSources>
</configuration>