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

Viewed 3069

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.

3 Answers

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.

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.

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.

Related