Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2019 restore completed nuget loop

I'm having an issue with VS2019 profesional 16.5.5 and 16.5.4 with my C# multi-project solution. It compiles, tests and executes correcly, but in the background I have the constant restore of nuget packages, completing once every few seconds. The CPU is close to 100%.

I have tried Update-Package -reinstall, nuget locals all --clear (from: How do I fix this NuGet Restart loop in Visual Studio 2017?), updating VS, removing my .vs folder, cleaning the solution, checking out the project again from git and even restarting. Nothing seems to help.

The project has a few main projects: two console, one AspNetCore, one WPF. They use common libs in Standard 2.0 or shared C# projects.

The Package Manager log in Output window has hundereds of entries like:

Time Elapsed: 00:00:00.7441577
========== Finished ==========

Time Elapsed: 00:00:00.8448774
========== Finished ==========

Time Elapsed: 00:00:00.8410551
========== Finished ==========

Time Elapsed: 00:00:00.7351174
========== Finished ==========

Time Elapsed: 00:00:00.7997720
========== Finished ==========

Time Elapsed: 00:00:00.9367757
========== Finished ==========

Unloading some projects helps a bit, but only temporarily - until I load them again.

How can I fix the constant Nuget package restore issue?

like image 938
Krzysztof Bociurko Avatar asked May 13 '20 23:05

Krzysztof Bociurko


People also ask

How do I rerun a NuGet restore?

Restore packages manually using Visual StudioEnable package restore by choosing Tools > Options > NuGet Package Manager. Under Package Restore options, select Allow NuGet to download missing packages. In Solution Explorer, right click the solution and select Restore NuGet Packages.

How do I reinstall NuGet packages in Visual Studio 2019?

In Visual Studio, the Package Manager Console provides many flexible options for updating and reinstalling packages. On the Installed tab, select a package, record its name, then select Uninstall. Switch to the Browse tab, search for the package name, select it, then select Install).

Was not found it might have been deleted since NuGet restore?

It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. Here are some actions you can take to resolve this error: Add the /restore option to your MSBuild.exe command.


Video Answer


2 Answers

I found what causes the issue. Some time ago I tried adding auto versioning to the console projects (with help of Auto Versioning in Visual Studio 2017 (.NET Core)). To be exact, I added to my .csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <!-- ... -->
    <VersionPrefix>1.1</VersionPrefix>
    <VersionSuffix>$([System.DateTime]::UtcNow.ToString(yyyyMMddTHHmmss))</VersionSuffix>
  </PropertyGroup>
  <!-- ... -->
</Project>

This was the direct cause of the issue. When commenting out the dynamic expression in VersionSuffix, the issue disappeared. What is more interesting, changing to yyyyMMddTHHmm (removed ss) also fixed this. And no, it did not change the frequency of Nuget restore to minutely - it stopped completely.

Even more, I have found a post on Microsoft Developer forums that details the same issue. The post has been since removed, but I managed to archive it for purposes of this answer. The post has just been moved: Using current datetime as VersionSuffix in csproj causes restoring of packages forever .

like image 76
Krzysztof Bociurko Avatar answered Oct 16 '22 16:10

Krzysztof Bociurko


I know you found where the issue was coming from, but here is the fix, for those who want to keep the seconds in their version number.

Add the following to your .csproj:

<PropertyGroup>
    <VersionPrefix>1.0.0</VersionPrefix>
    <VersionSuffix Condition="'$(DesignTimeBuild)' != 'true' OR '$(BuildingProject)' == 'true'">beta-$([System.DateTime]::UtcNow.ToString(yyyyMMdd-HHmmss))</VersionSuffix>
</PropertyGroup>

It nicely updates all various places with the right versioning string, including your .dll and NuGet, only when building the project (and not at design time).

like image 37
sw1337 Avatar answered Oct 16 '22 17:10

sw1337