ChatGPT解决这个技术问题 Extra ChatGPT

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.


A
Alexandre Dion

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 tried this on one of my projects. It updated NuGet.exe but it seemed to have removed it from my solution. I know I can add it back it, but that seemed odd.
Ah right, I tried a real update this time and nuget.exe got removed from VS solution for me as well. (I didn't get that previously since nuget.exe was up to date). It happens because during the update the current nuget.exe is moved to nuget.exe.old first so the file reference is broken. I doubt this will be fixed, I'd run the command with the solution closed.
It seems like doing the self update wouldn't give you a new NuGet.targets file, which might be a problem. I think the NuGet team made some changes to the targets file between versions 1.6 and 1.7.
@William Gross well you could check if there was an update to nuget.org/packages/NuGet.Build (that's the package that contains nuget.targets)
worked for me, just remember to run the cmd as administrator
C
Chris Kirby

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.


+1 - I have found that this is the best way to get updates to the NuGet.exe as well as the NuGet.targets file (which is also important). Just make sure you save any customizations you have made to the NuGet.targets file (e.g. custom PackageSources) so that you can re-apply them to the new file.
great that worked for me like charm!! thanks for the information
So many years later, but.... This saved my day. Thank you very much!
R
Rodolpho Brock

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>

The question asks how to automatically update nuget.exe to latest version when using package restore. David Ebbo'sblog post only explains how to restore packages using nuget.exe.
You should probably specify which part of the question you answer then, when looking at the title of the question and the accepted answer, it is easy to consider this answer misplaced...