Restore NuGet packages failed for project ... : Could not find a part of the path
Asked Answered
L

5

9

I'm trying to run a Unit Test project in Visual Studio (.NET Framework 4.72).

The project is now building/running successfully, however there is still an error that the NuGet packages can't be restored (the file path is actually wrong and does not exist.). How can I change to the right file path and tell VS to not look in the wrong path? Here's the error:

NuGet Package restore failed for project Tests_WeatherData: Could not find a part of the path 'C:......\Begin\packages\MSTest.TestAdapter.1.3.2\build/_common/zh-Hans/Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.resources.dll'

Thank you for any advice how to change the file path/restoring settings.

Loosing answered 8/3, 2022 at 18:54 Comment(0)
R
19

It can be caused by having your project located within a subfolder that is too deeply nested. I think possibly there's some limit for the folder path like maybe 250 characters or something like that.

I saw the same error when I downloaded a zip with some demo projects and when I unzipped it created a folder with many subfolders. The project I was interested in was down pretty deep in the subfolders and when I tried to restore its NuGet packages I got the same errors.

I moved the project folder up nearer to c:\ and then the restore errors went away.

Revenant answered 22/4, 2022 at 7:46 Comment(0)
L
11

This is what worked for me.

  1. Download nuget.exe from here. Screenshot of link where nuget.exe can be downloaded, with an arrow pointing to the location of the download link on the page.

  2. Copy this nuget.exe executable to the folder that contains the solution which is currently failing to build.

  3. Ensure that the LongPathsEnabled registry key is set to 1.

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled

  4. Open cmd prompt, navigate to the above folder location. Run the below command.

    c:\repos\MySolutionFolder>nuget.exe restore

  5. Build the solution in Visual Studio. In my case I used VS2019.

Link that helped me with these steps - Github-Issue.

Laywoman answered 20/9, 2022 at 20:13 Comment(0)
G
1

The issue could be filepaths in your CSPROJ file. Most common cause is project restructure and the location of packages has changed.

In addition to NUGET references in the CSPROJ, there could be a few other spots in the footer of the CSPROJ or the header portion for MSTest adapter entries. I.e. just verify all your filepaths (especially relative) ones are all correct and reload/rebuild.

Grit answered 8/3, 2022 at 19:35 Comment(0)
T
1

Another option to fixing the long path issue is to do it from the Powershell with Admin privileges. (right-click and choose "Run as Administrator").

  1. Close Visual Studio

  2. Paste the following into Powershell:

    New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD ` -Force

  3. Add the NuGet package via the Powersell by navigating to your project's directory and running this command:

    dotnet add package

Traynor answered 4/3 at 15:59 Comment(0)
M
0

After I updated a Nuget package, Visual Studio included newly added folders for both the old and the updated version in my list of Pending Changes. The old version obviously no longer existed, since it was deleted when Nuget updated the package. I had to manually exclude the folder named with the old version number.

Meridith answered 3/7, 2023 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.