Why doesn't Visual Studio notice changes to packages.config on build? - visual-studio

I'm not very familiar with Nuget, especially against VC++. I realised I needed to add Curl to my executable simply because it links a library using Curl.
So I edited the .exe's package.config:
<package id="Curl" version="7.59.0.0" targetFramework="native" />
<package id="Curl.redist" version="7.59.0.0" targetFramework="native" />
But again I am having to jump into the Nuget console in VS to update/restore things first. Why isn't this happening automatically? Is it possible to make it work that way?
Worse still, running update-package tells me there are no updates required. I have to reinstall them every time.

Why isn't this happening automatically? Is it possible to make it work
that way?
As zivkan said, it's not a valid workflow to manually edit packages.config so it's not happening automatically. And if you have to consume nuget packages in this way by manually adding and editing packages.config file, you can right-click the solution in solution explorer and click Restore Nuget Packages to restore packages first.
running update-package tells me there are no updates required
For Curl, in Nuget Gallery there's only 7.30.0.2(latest) version which depends on Curl.redist-7.30.0.2 version. I'm not sure where you can find the 7.59.0 version (It seems it has been released,see here), but it's obviously not published to nuget.org. So when you try to restore nuget packages from available nuget source in nuget console or Solution's Restore Nuget packages option mentioned above, you'll get no updates required or error like unable to find version 7.59.0 of package curl.
To resolve this issue, you can go Tools menu=>Nuget Package Manager=>Package Manager Settings=>Package Sources to configure the source where to find the 7.59.0 version of Curl. Then you can use the Restore nuget packages option to restore them easily for all projects in the solution. Or you can just trying changing the version 7.59.0.0 to the 7.30.0.2 which is exactly available in nuget.org.

Related

How to fix all Nuget references in a .net core solution quickly?

I am finding it a common issue where when I download a .NET repo from Github, the solution has missing references. See an example below. When using Nuget Package Manager, it says the packages are installed already. The packages folder is missing.
I tried using 'Update-Package -reinstall' and got a bunch of lines with 'No package updates are available from the current package source for project 'project name'.... That's probably due to the missing package files.
I can fix each reference manually but it's a time consuming process. Is there a way to force fix all these references in all the projects in one swoop using a command or a tool that 'cleverly' knows what to do? I am using Visual Studio 2019 16.3 preview 1
In VS2019, those .net core projects or .net standard projects use PackageReference instead of packages.config format to manage nuget packages. And in this way, the content in xx.csproj looks similar to this:
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.1.3" />
<PackageReference Include="MediatR" Version="6.0.0" />
<PackageReference Include="AutoMapper" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
</ItemGroup>
Every time you download a project which uses packageReference format, you only get the xx.csproj and related source files but without nuget packages. So when you open and load the solution or project in VS IDE, it will display the nuget packages name in solution explorer but with yellow warning triangle since now VS can't find those packages locally.
Is there a way to force fix all these references in all the projects
in one swoop using a command or a tool that 'cleverly' knows what to
do?
Just like what zivkan said in his answer, NuGet is normally configured to restore packages automatically if you've enabled those two options. Every time when we load the soultion in IDE, it will automatically restore the packages for us. Not sure the reason why it seems not to work in your machine, but you can check and try:
1.Go Nuget Package Manager=>Package Sources, make sure you've set https://api.nuget.org/v3/index.json as one the Available Package sources.
2.Right-click the solution in Solution Explorer, choose the Restore Nuget Packages to restore packages for whole solution.
3.Or you can Unload and reload the project to check if it helps.
4.By default, the packages are stored at C:\Users\xxx\.nuget\packages folder, you can check if those folders exists there.
(I delate all packages in that folder, so it's clear when I open the git solution in VS, the nuget is installing those necessary packages automatically, I'm not sure if there's any possibility that cause too many packages are being installed,so it will take some time? I check and find, to restore necessary packages for NorthWind solution, it require packages for over 370 MB)
I tried using 'Update-Package -reinstall' and got a bunch of lines
with 'No package updates are available from the current package source
for project 'project name'.... That's probably due to the missing
package files.
And for the reason why Update-Package -reinstall not work well in the project, maybe it's a similar issue like nuget issue #4103, for the packagereference in xx.csproj, several commands for now are not supported.
And after my check in my machine, the command obviously do not work for a PackageReference-format .net standard project. When I've installed related packages successfully, I run this command but only get No package updates are available from the current package source for project xxx, then I delete the installed packages, clean the cache and run the command again, I still get same message!
So I think this command can't recognize PackageRefernece format in .csproj, no matter whether I install related packages or not, it will always throw message No package updates are available ...
NuGet is normally configured to restore packages automatically. See the options page. The second Package Restore option, "Automatically check for missing packages during build in Visual Studio" is enabled by default on clean installs.
Here's a little video I recorded of what happens on my machine when I open the solution. Keep an eye out on the status bar at the bottom left of the window.
If your installation of Visual Studio does not behave in the same way with the automatic restore option enabled, then there is some other issue on your machine, but we don't have enough information from your question, as of the time I wrote this.

NuGet Automatic Restore And Installing NuGets

I recently successfully switched all of our projects over to automatic NuGet restore. To do this I followed this guide. Everything worked great, I was able to delete my /packages folder and restore to my heart's content.
One of the steps in that guide says to edit your .csproj file and remove the EnsureNuGetPackageBuildImports related element.
The problem I am having now is that, after going in and downgrading one of my projects libraries, NuGet seems to have automatically added the EnsureNuGetPackageBuildImports related element back into my .csproj file.
Is there some other setting I should be changing to prevent NuGet from doing this? I would rather not have to edit my .csproj file every time I make a NuGet related change.
Is there some other setting I should be changing to prevent NuGet from doing this?
I`m afraid the answer is not if you going in and downgrading one of your projects libraries.
The target "EnsureNuGetPackageBuildImports" is used to ensures that the imported .props or .targets files are indeed imported in the old way "Enable Package Restore". NuGet has supported NuGet MSBuild support since NuGet 2.5.
Since Nuget 2.7+, Microsoft has dropped support for the 'Enable NuGet Package Restore' in VS2015 and you need to do some manual changes to either migrate old solutions or add the feature to new solutions. Nuget Automatic Package Restore has changed in Nuget 2.7+. Unfortunately, it looks like a non-backward compatible change. We have some developers on 2.7 and before who will now need to upgrade to 2.7+. In this case, NuGet will automatically added the EnsureNuGetPackageBuildImports related element back into your .csproj file when you going in and downgrading one of your projects libraries.
To resolve this, we advice that do not mix 'old' and new methods for automatic package restoration(Remove that files when you use automatic package restoration).
Besides, if going in and downgrading one of your projects libraries is unavoidable, there is a PS script can help you remove it more convenient:
DisableNuGetPackageRestore.ps1

How to force NuGet updates on other machines?

I installed a new NuGet package on my machine to get a library installed. This means I have other versions of the .NET compiler and so on. When I then push the project to my branch and others checkout and pull it, I assume their NuGet packages are different versions than mine. How do I get those to update on their machines, without doing Install NUGET_PACKAGE again, overriding everything I've done (like removing unnecessary folders)? Example:
Before:
<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
After:
<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.2\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.2\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
1.0.0 to 1.0.2.
This is handled by NuGet restore.
The other developers do not need to reinstall the NuGet packages into the project. All the other developers need to do is restore the packages for the projects you have modified after they have checked it out.
If they are using Visual Studio 2015 the restore will generally be done when you build the project. There is also a Restore NuGet Packages menu if you right click the solution in the Solution Explorer if you want to manually trigger the restore.

Creating a vs.net 2013 solution with multiple projects, nuget is causing real problems

When other developers get the vs.net 2013 solution from TFS, Nuget doesn't seem to work as expected and many of the individual projects in the solution do not have the required dll's downloaded (although they are defined in the packages.config file).
If I go to view the installed packages, it shows up as they are installed and the correct projects are selected.
I went into console and ran:
Update-Package -Reinstall
After doing this, my packages.config file was truncated for whatever reason and 80% of my packages were removed from the config file!
What should I be looking into as it is either I have setup the solution incorrectly or nuget just isn't reliable.
Thoughts?
First of all make sure that NuGet Packages Restore is turned ON.
This forces to restore missing binaries directly from NuGet server. You can right click on Solution and enable this option as follow:
Make sure that .nuget folder that contains nuget.exe is created under your Solution.
You'll want to make sure that you have automatic package restore turned on in VS 2013. You can do this in the options.
Then, when you build, VS should grab the packages for you. If you want to manually restore the packages or do it from powershell, you'll want to use the command nuget.exe restore in the folder your solution lives in.
If you're using custom package sources, you'll want to make sure you specified them in your nuget.config file.

Commiting libraries added via NuGet

I have a Visual Studio project which I have committed to VisualSVN (via the VisualSVN => Commit menu in Visual Studio). I have added a number of libraries to this project via NuGet.
My colleague has downloaded the project I have uploaded to SVN (via VisualSVN => Get solution from Subversion...) and has found that these libraries are missing, and he is having to re-download them.
A few questions:
Is this by design? Or have I not committed my Solution properly? Or has my colleague not download the solution to his machine properly?
If this is by design, what is the correct way to re-add references to a solution downloaded from an SVN server? I am worried that I may have added a reference and worked with it, and that it may have been updated since so whenever my colleague re-adds the same reference via NuGet he will get a more up to date version that will be different, and this will break my program. Is this a valid concern?
Yes, this is by design. The whole concept of using Nuget is that you will not have to keep libraries in your version control system.
You need to Right Click on your solution in Visual Studio and select Enable NuGet Package Restore.
This will configure the solution to restore the NuGet packages (if any missing, or in case of none) whenever you'll do a build. Also, all the libraries that you've added for a particular project will have an entry in the packages.config created in the project's source drectory; for eg:
<packages>
<package id="jQuery" version="1.8.3" />
</packages>
This way NuGet makes sure everybody gets the same version.
Just enable "Nuget package restore" in your solution and packages will be automatically downloaded during the build:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages

Resources