NeverTrust
NeverTrust

Reputation: 6187

Assets file project.assets.json not found. Run a NuGet package restore

I'm trying to use nopCommerce(Which is written in .NET Core) but when I want to run the project I face 52 Errors telling me Run a nuget package restore

Assets file ~\obj\project.assets.json' not found. Run a NuGet package restore to generate this file. Nop.Web.MVC.Testsote

when I use the right click on the solution and selecting Restore Nuget Packages I get this message:

All packages are already installed and there is nothing to restore.

but those 52 Errors are still there and in Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution there is nothing installed on the solution,also I Recently updated my VS2017 to 15.5.4

Upvotes: 592

Views: 472797

Answers (30)

prisan
prisan

Reputation: 1421

In case when dotnet restore doesn't work, following steps may help:

  1. Visual Studio >> Tools >> Options >> Nuget Manager >> Package Sources
  2. Uncheck any third party package sources.
  3. Rebuild solution.

Upvotes: 96

Zarepheth
Zarepheth

Reputation: 2603

Not yet mentioned is the possibility that Windows has not been opted in to support long file paths.

Update the build machine's registry so the LongPathsEnabled value is set to 1. Other tools involved in the NuGet restore and build may require their own configuration to support Windows long file paths (for instance, Git).

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000001

Upvotes: -1

YanMax
YanMax

Reputation: 146

This may be obvious or an edge case but check your path as well.

When I cloned the repo I got this path:

repos\MyRepo\Field%20Service%20Project\Azure Functions\func-dev-sat-eus-01\obj\project.assets.json

The escaped characters confused the build command so that it could not reach the file because it could not find that folder.

In my case, I just replaced the '%20' with an actual space in the directory name.

Upvotes: 2

Marc Cayuela
Marc Cayuela

Reputation: 1592

Alright I might have two causes to your problem, it's the exact the same error and related to NuGet (restoring), so many users might face the same issue and end up here. So three options:

  1. Old version of Visual Studio. According to this bug, versions of Visual Studio previous to 17.8 would not build if they had EnableManagedPackageReferenceSupport to true. Note that at the time of the writing Azure Hosted pipelines already contain version 17.10 if using windows-2022 image. So it should not be a problem in general.
  2. NuGetCommand restore (on Azure Devops) not applying to your vcxproj. Reported here. This one is very weird but it's documented how it happens. Just do not remove your Win32 configuration (even if you don't use it) from your C++/CLI projects. Otherwise the restore NuGet will ignore them. Azure pipeline showing that vcxproj is not restored
  3. Could be due to NuGet caching, as explained here.

Upvotes: 1

mishal153
mishal153

Reputation: 1554

I had this issue on a Teamcity build. I had a build step of type nuget installer. Inside it there's a setting named Restore mode. The fix for me was to set its value to Restore (it was set to install)

Upvotes: 0

Tailslide
Tailslide

Reputation: 81

I got this error and missed the error above it saying missing workload. Fixed by running:

dotnet workload install <workloadname>

In my case

dotnet workload install wasm-tools-net7

Upvotes: 0

Todd
Todd

Reputation: 642

Wow. This one has clearly been giving people trouble (including myself). As others have noted, it seems that a large number of failure cases may lead to a similar set of evidence.

Anyway, for me I was getting this "project.assets.json not found" error when trying to publish. After trying many of the suggestions found here, and elsewhere, and losing a half-day, I found something that got me unstuck and I thought I'd add my solution in case it helps anyone.

  1. Ensure that the directory "{PROJECT-ROOT}\obj\publish\browser-wasm" exists, creating it if needed.

  2. Find these 5 files in your "{PROJECT-ROOT}\obj" directory:

    project.assets.json
    project.nuget.cache
    {PROJECT-NAME}.csproj.nuget.dgspec.json
    {PROJECT-NAME}.csproj.nuget.g.props
    {PROJECT-NAME}.csproj.nuget.g.targets
  1. Copy those 5 files into "{PROJECT-ROOT}\obj\publish\browser-wasm", overwriting them if needed.

  2. If the dependencies for any projects that are dependencies of your project have changed then you should do steps 1, 2, and 3 for each of those projects as well.

  3. Restart Visual Studio.

This got me unstuck and I am now able to publish. However, I do need to re-do this each time I add new Dependencies to my project.

Upvotes: 1

Fozle Rabbi
Fozle Rabbi

Reputation: 129

To fixed this issue. First you have to close the Visual Studio, then restart again in administrative mode.

Create an new project or run your older project. Then Go

Tools > NuGetPackage Package Manager > Package Manager Console

An console will be open in below

Then put a command

dotnet restore 

It will fixed all your problem.

Upvotes: 10

taylorswiftfan
taylorswiftfan

Reputation: 1517

To those with the same issue as me in Azure DevOps / VSTS environment encountering a similar message:

C:\Program Files\dotnet\sdk\2.2.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(208,5): Error NETSDK1004: Assets file '...\obj\project.assets.json' not found. Run a NuGet package restore to generate this file

Add /t:Restore to your MSBuild Arguments in Build Solution.

In Azure DevOps build pipeline, add /t:Restore here:

enter image description here

Upvotes: 86

user3097514
user3097514

Reputation: 359

I did not have any NuGet in my CLI/C++. VS2022 built fine. However, MSBuild did not work giving me error NETSDK1004: Assets file ... obj\project.assets.json' not found. Run a NuGet package restore to generate this file.

The solution to my issue was removing the following from the vcxproj file.

<EnableManagedPackageReferenceSupport>true</EnableManagedPackageReferenceSupport>

I suppose changing true to false might work. I removed the line and MSBuild now works.

Upvotes: 3

usilo
usilo

Reputation: 365

I haven't found anywhere the method suggested by Microsoft which is to delete the "packages" folder.

Source: Troubleshooting package restore errors

You may encounter build errors due to missing files, with a message saying to use NuGet restore to download them. However, running a restore might say, "All packages are already installed and there is nothing to restore." In this case, delete the packages folder (when using packages.config) or the obj/project.assets.json file (when using PackageReference) and run restore again. If the error still persists, use nuget locals all -clear or dotnet nuget locals all --clear from the command line to clear the global-packages and cache folders as described on Managing the global packages and cache folders.

Upvotes: 0

Speed
Speed

Reputation: 417

For me when i did - dotnet restore still error was occurring.

I went to

1 Tool -> NuGet Package Maneger -> Package Manager settings -> click on "Clear all NuGet Cache(s)"

2 dotnet restore

resolved issues.

Upvotes: 27

tmaj
tmaj

Reputation: 35115

In my case it was .net6 project and the build happening on DevOps windows-2019 agent.

enter image description here

Solution: windows-2022 can build .net6.

Upvotes: 1

m_a_s
m_a_s

Reputation: 240

Had a similar issue. Turned out that I did not have access to %APPDATA%\Local\Temp\NuGetScratch\lock

Once I had access I could run nuget restore, but everything did start working.

Upvotes: 0

DooDoo
DooDoo

Reputation: 13479

I have same problem and dotnet resotre not work for me. I Install this component:

enter image description here

and the run dotnet resotre and the problem has solved.

Upvotes: 4

Steve
Steve

Reputation: 157

What worked for me when I got this error was adding a global.json file but that may be related to using Paket instead of Nuget.

Upvotes: 0

LawMan
LawMan

Reputation: 3625

My issue was the build configuration platform was mismatched. Once, I changed "Any CPU" to "x64", I was able to publish.

enter image description here

Upvotes: 3

Ε Г И І И О
Ε Г И І И О

Reputation: 12371

In my case, I had the following added to my *.csproj files to fully remove obj and bin folders on 'Clean'. Apparently, it was the culprit. Got rid of that and viola, all started to work again. Now I'm using the "Clean Bin" extension instead. Hope this might help anyone who is running into this issue, and none of the mentioned fixes works.

<Target Name="SuperClean" AfterTargets="Clean">
    <!-- Remove obj folder -->
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" />
    <!-- Remove bin folder -->
    <RemoveDir Directories="$(BaseOutputPath)" />
</Target>    

Upvotes: 1

Felipe Augusto
Felipe Augusto

Reputation: 1563

I received one message on Azure DevOps about don't find file

i need to create this on my YAML deployment file, AFTER BUILD TASK

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**\*.sln'
    feedsToUse: 'config'
    noCache: false

Upvotes: 0

Yogesh Dangre
Yogesh Dangre

Reputation: 306

You will get required packages from "https://api.nuget.org/v3/index.json". Add this in Package Resources. Also make sure other packages are unchecked for time being. And Click Restore Nuget Package on Solution Explorer enter image description here

Upvotes: 9

Mike Homol
Mike Homol

Reputation: 479

It was mentioned earlier but I just wanted to re-emphasize the importance of not have space anywhere in your pathing! This is what was getting me. You've been warned.

Upvotes: 10

OutstandingBill
OutstandingBill

Reputation: 2882

Closing and re-opening Visual Studio solved this issue for me, once I had made sure the NuGet packages had been restored as per other answers posted here.

Edit: Sometimes just trying to build again fixes the problem.

Upvotes: 53

seven
seven

Reputation: 107

You can go for : Tools > NuGet Package Manager > Package Manager Console

And then Run:

dotnet restore

Upvotes: 6

Jeremiah Mercier
Jeremiah Mercier

Reputation: 501

If this error occurs as part of a build in Azure DevOps (TFS) and your build already has a NuGet restore task, this error may indicate the NuGet restore task was not able to restore all packages, especially if you use a custom package source (such as an internal NuGet server). Adding /t:Restore;Build to the MSBuild Arguments seems to be one way to resolve the error, but this asks MSBuild to perform an additional NuGet restore operation. I believe this succeeds because MSBuild uses the custom package source configured in Visual Studio. A preferable solution is to fix the NuGet restore task.

To configure a custom package source for the NuGet restore task:

  1. Create a NuGet.config file that lists all of the package sources (Microsoft Visual Studio Offline Packages, nuget.org, and your custom package source) and add it to source control.
  2. In the Nuget restore task under Feeds to use: select the option Feeds in my NuGet.config.
  3. Provide the path to NuGet.config.
  4. Remove the /t:Restore;Build option from the MSBuild task.

Additional information is available here.

Upvotes: 12

dalfonsop
dalfonsop

Reputation: 1

Try this (It worked for me):

  • Run VS as Administrator
  • Manual update NuGet to most recent version
  • Delete all bin and obj files in the project.
  • Restart VS
  • Recompile

Upvotes: -1

J. Volkya
J. Volkya

Reputation: 1000

For me it turned out to be a nuget source credentials problem. I had recently changed my password for accessing a nexus server and visual studio was still using the old password when trying to access a nuget on that server through the windows credential manager. To fix it, I had to delete the entry for those outdated credentials in the credential manager and after, when I did a nuget restore, it prompted me for a password letting me enter the new password, which got saved in the credential manager again. You can access the credential manager from the cmd line using CmdKey.exe.

Upvotes: 0

Goodies
Goodies

Reputation: 2069

Seen this after adding a WinForms Core 3.1 project (from project templates) on VS-2019 vs 16.4.0 and trying to run it out of the box. Clean or Rebuild the entire solution did not work.

I just reloaded my solution.. that is File/Close Solution and then reopening it and rebuilding it solved the problem.

Upvotes: 0

Bikram
Bikram

Reputation: 523

Very weird experience I have encountered!

I had cloned with GIT bash and GIT cmd-Line earlier, I encountered the above issues.

Later, I cloned with Tortoise-GIT and everything worked as expected.

May be this is a crazy answer, but trying with this once may save your time!

Upvotes: 0

Abdulsamet ILERI
Abdulsamet ILERI

Reputation: 165

Solved by adding /t:Restore;Build to MSBuild Arguments

Upvotes: 9

T.S.
T.S.

Reputation: 19384

This problem happening when your build tool is not set to do restore on projects set to use PackageReference vs packages.config and mostly affect Net Core and Netstandard new style projects.

When you open Visual Studio and build, it resolves this for you. But if you use automation, CLI tools, you see this issue.

Many solutions are offered here. But all you need to remember, you need to force restore. In some instances you use dotnet restore before build. If you build using MsBuild just add /t:Restore switch to your command.

Bottom line, you need to see why restoring can't be activated. Either bad nuget source or missing restore action, or outdated nuget.exe, or all of the above.

Upvotes: 5

Related Questions