PackageReference & Restore
A NuGet dependency is an MSBuild item: <PackageReference Include="Id" Version="…" />. Before MSBuild compiles, NuGet's restore turns all those items, across the whole project graph, into exact package versions, makes sure each one is in the local cache and writes a file that tells the build which DLLs to use. This lesson follows a package from the command line into bin/.
Adding a package
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
XML
echo 'System.Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(new[] { 1, 2, 3 }));' > Program.cs
dotnet package add Newtonsoft.Json | grep -v '^info : X.509'
grep PackageReference app.csproj
dotnet runinfo : Adding PackageReference for package 'Newtonsoft.Json' into project '/home/learner/app/app.csproj'.
info : Restoring packages for /home/learner/app/app.csproj...
info : Installed Newtonsoft.Json 13.0.4 from /opt/nuget-feed to /home/learner/.nuget/packages/newtonsoft.json/13.0.4 with content hash pdgNNMai3zv51W5aq268sujXUyx7SNdE2bj1wZcWjAQrKMFZV260lbqYop1d2GM67JI1huLRwxo9ZqnfF/lC6A==.
info : Package 'Newtonsoft.Json' is compatible with all the specified frameworks in project '/home/learner/app/app.csproj'.
info : PackageReference for package 'Newtonsoft.Json' version '13.0.4' added to file '/home/learner/app/app.csproj'.
info : Generating MSBuild file /home/learner/app/obj/app.csproj.nuget.g.props.
info : Generating MSBuild file /home/learner/app/obj/app.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /home/learner/app/obj/project.assets.json
log : Restored /home/learner/app/app.csproj (in 305 ms).
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
[1,2,3]
dotnet package add (the .NET 10 spelling; dotnet add package still works) did four things:
- Resolved a version. With no
--version, it picked the newest stable version on the configured sources, 13.0.4. - Installed it from the
localsource (/opt/nuget-feed) into the global packages folder,~/.nuget/packages/newtonsoft.json/13.0.4/, and checked the package's content hash. - Edited the project file to add the
PackageReference. - Wrote restore outputs to
obj/:project.assets.jsonand two generated MSBuild files,app.csproj.nuget.g.propsand.targets, through which packages can add their own props and targets to your build.
The global packages folder
Packages are extracted once per machine (per user) and shared by every project. Nothing is copied into the project:
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>
</Project>
XML
dotnet restore | grep -v X.509
dotnet nuget locals global-packages --list
ls ~/.nuget/packages/newtonsoft.json/13.0.4
ls ~/.nuget/packages/newtonsoft.json/13.0.4/lib
echo "--- delete obj/ and restore again"
rm -rf obj
dotnet restore -v:n | grep -E 'Installed|Restored|already|NotFound' | sed 's/^ *//' Determining projects to restore...
Restored /home/learner/app/app.csproj (in 370 ms).
global-packages: /home/learner/.nuget/packages/
LICENSE.md
README.md
lib
newtonsoft.json.13.0.4.nupkg
newtonsoft.json.13.0.4.nupkg.sha512
newtonsoft.json.nuspec
packageIcon.png
net20
net35
net40
net45
net6.0
netstandard1.0
netstandard1.3
netstandard2.0
--- delete obj/ and restore again
Restored /home/learner/app/app.csproj (in 126 ms).
- The folder name is the lower-cased id and the version. It holds the original
.nupkg, its SHA-512 (.nupkg.sha512), the.nuspecand the extracted contents:lib/<tfm>/for each target framework the package supports. - The second restore printed no
Installedline. The package was already in the global folder, so restore only rebuiltobj/project.assets.json. That's why restores on a warm machine take milliseconds, and why caching~/.nuget/packagesis the single most effective speed-up for .NET CI. - Other caches:
dotnet nuget locals all --listshows the HTTP cache (downloaded responses from remote feeds) and the temp folder too;dotnet nuget locals all --clearempties them when you suspect a corrupt cache.
project.assets.json
Restore's main output is the assets file: the resolved graph for every target framework, the files each package contributes and where it was found. The build never talks to a feed; it only reads this file.
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>
</Project>
XML
dotnet restore -v:q
grep -A12 '"Newtonsoft.Json/13.0.4": {' obj/project.assets.json | head -13 "Newtonsoft.Json/13.0.4": {
"type": "package",
"compile": {
"lib/net6.0/Newtonsoft.Json.dll": {}
},
"runtime": {
"lib/net6.0/Newtonsoft.Json.dll": {}
}
}
}
},
"libraries": {
"Newtonsoft.Json/13.0.4": {
For net10.0, the assets file says: compile against lib/net6.0/Newtonsoft.Json.dll, and copy the same file to the output at run time. Packages can split those (a ref/ assembly to compile against and a different runtimes/<rid>/lib/ one to run), and the assets file is where that choice is made. You almost never edit or commit it: it lives in obj/, which belongs in .gitignore.
When a package isn't found
mkdir app && cd app
cat > app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
XML
dotnet package add Newtonsoft.Jsn 2>&1 | grep -E '^error'
sed -i 's|</Project>| <ItemGroup>\n <PackageReference Include="Newtonsoft.Jsn" Version="13.0.4" />\n <PackageReference Include="Newtonsoft.Json" Version="[14.0.0]" />\n </ItemGroup>\n</Project>|' app.csproj
dotnet restore 2>&1 | grep -E 'NU1' | awk '!seen[$0]++'error: There are no versions available for the package 'Newtonsoft.Jsn'.
/home/learner/app/app.csproj : error NU1101: Unable to find package Newtonsoft.Jsn. No packages exist with this id in source(s): local
/home/learner/app/app.csproj : error NU1102: Unable to find package Newtonsoft.Json with version (= 14.0.0)
/home/learner/app/app.csproj : error NU1102: - Found 2 version(s) in local [ Nearest version: 13.0.4 ]
| Code | Meaning | Usual cause |
|---|---|---|
NU1101 |
no package with this id on any source | typo, or the package lives on a feed you haven't configured |
NU1102 |
the id exists, but no version satisfies the range | version not published (yet), or a private build on another feed |
NU1103 |
only prerelease versions satisfy it | add -* or a prerelease version explicitly |
NU1301 |
a source couldn't be reached | network, credentials, proxy |
dotnet package add refuses an unknown id up front (There are no versions available); a reference already in the project file fails at restore with the NU code. NuGet lists the versions it did find with NU1102, which usually makes the fix obvious. ([14.0.0] is an exact version, explained in the next lesson.)
Test yourself
Where does restore put package files on Linux by default?
- In the project's packages/ folder
- In the global packages folder ~/.nuget/packages/<id-lowercase>/<version>/
- In bin/
- In /usr/share/dotnet
Which file does the build read to know which package DLLs to compile against?
- nuget.config
- obj/project.assets.json
- The .nuspec
- packages.config
Restore fails with NU1102. What does that tell you?
- No source has a package with that id
- The package exists, but no available version satisfies the requested range
- The feed is unreachable
- The package is deprecated