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.
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.
dotnet restore MyApp.sln
dotnet build MyApp.sln -c Release --no-restoreOr build without --no-restore
Letting dotnet build restore implicitly guarantees assets exist for the project being built.
dotnet build MyApp.sln -c Release # implicit restoreHow to prevent it
- Restore and build the same solution/project and configuration.
- Prefer an explicit
dotnet restorestep, then--no-restoreon build, for clarity. - Cache the global packages folder so the restore step is fast and reliable.