Skip to content
Latchkey

dotnet "PackageReference ... but it was not restored" in CI

The build found a PackageReference whose assets were never restored - the project.assets.json is missing or stale. The build needs a completed restore for the same project before it can compile.

What this error means

Build fails saying a package "was not restored" or that you must "Run a NuGet package restore to generate this file". It happens when a build step runs before restore, or restores a different solution/project than it builds.

dotnet build output
error : Your project does not reference "Microsoft.Extensions.Logging" for the
target framework. Run a NuGet package restore to generate the file
'project.assets.json'.

Common causes

Build ran before restore

A pipeline that builds with --no-restore (or a custom step) before any restore completed has no project.assets.json, so referenced packages are not available.

Restored a different project than built

Restoring one project/solution and then building another (or a different configuration) leaves the built project without its restored assets.

How to fix it

Restore the same target you build

Run restore for the exact solution/project before building it, or let build restore implicitly.

Terminal
dotnet restore MyApp.sln
dotnet build MyApp.sln -c Release --no-restore

Or build without --no-restore

Letting dotnet build restore implicitly guarantees assets exist for the project being built.

Terminal
dotnet build MyApp.sln -c Release   # implicit restore

How to prevent it

  • Restore and build the same solution/project and configuration.
  • Prefer an explicit dotnet restore step, then --no-restore on build, for clarity.
  • Cache the global packages folder so the restore step is fast and reliable.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →