TExcelTAD
Home › .NET Builds: MSBuild & NuGet › dotnet tool & Local Tool Manifests

dotnet tool & Local Tool Manifests

Build pipelines need command-line tools: code formatters, report generators, EF Core's dotnet ef, versioning tools. In .NET these are distributed as .NET tools: NuGet packages containing a console app, installed with dotnet tool. A local tool manifest pins the tools and their versions in the repository, so every developer and CI agent runs the same ones, the same way they restore the same packages.

Making a tool

No third-party tool is seeded on this machine, so we build one: any console app becomes a tool with PackAsTool.

bash · try it yourself
mkdir countlines && cd countlines
cat > countlines.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <PackAsTool>true</PackAsTool>
    <ToolCommandName>countlines</ToolCommandName>
    <PackageId>Ada.CountLines</PackageId>
    <Version>0.1.0</Version>
  </PropertyGroup>
</Project>
XML
cat > Program.cs <<'CS'
foreach (var file in args)
    System.Console.WriteLine(
quot;{file}: {System.IO.File.ReadAllLines(file).Length} lines"); CS dotnet pack -o out -nologo -v:q unzip -Z1 out/Ada.CountLines.0.1.0.nupkg | grep tools/ | head -4 unzip -p out/Ada.CountLines.0.1.0.nupkg Ada.CountLines.nuspec | grep packageType dotnet nuget push out/Ada.CountLines.0.1.0.nupkg -s local
tools/net10.0/any/DotnetToolSettings.xml
tools/net10.0/any/countlines.deps.json
tools/net10.0/any/countlines.dll
tools/net10.0/any/countlines.pdb
    <packageTypes>
      <packageType name="DotnetTool" />
    </packageTypes>
Pushing Ada.CountLines.0.1.0.nupkg to '/opt/nuget-feed'...
Your package was pushed.

A tool package puts the app under tools/<tfm>/any/ with a DotnetToolSettings.xml naming the command, and declares the package type DotnetTool, which is how dotnet tool install recognises it (and refuses ordinary libraries).

Local tools: pinned per repository

bash · try it yourself
mkdir countlines && cd countlines
cat > countlines.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <PackAsTool>true</PackAsTool>
    <ToolCommandName>countlines</ToolCommandName>
    <PackageId>Ada.CountLines</PackageId>
    <Version>0.1.0</Version>
  </PropertyGroup>
</Project>
XML
echo 'foreach (var f in args) System.Console.WriteLine(
quot;{f}: {System.IO.File.ReadAllLines(f).Length} lines");' > Program.cs dotnet pack -o out -nologo -v:q && dotnet nuget push out/*.nupkg -s local > /dev/null cd .. && mkdir repo && cd repo && git init -q dotnet new tool-manifest dotnet tool install Ada.CountLines cat dotnet-tools.json; echo printf 'one\ntwo\nthree\n' > notes.txt dotnet countlines notes.txt dotnet tool run countlines notes.txt git add -A && git commit -qm "pin tools" echo "--- a fresh clone on another machine" rm -rf ~/.nuget/packages/ada.countlines cd .. && git clone -q repo clone && cd clone dotnet tool restore dotnet tool list
The template "Dotnet local tool manifest file" was created successfully.

You can invoke the tool from this directory using the following commands: 'dotnet tool run countlines' or 'dotnet countlines'.
Tool 'ada.countlines' (version '0.1.0') was successfully installed. Entry is added to the manifest file /home/learner/repo/dotnet-tools.json.
{
  "version": 1,
  "isRoot": true,
  "tools": {
    "ada.countlines": {
      "version": "0.1.0",
      "commands": [
        "countlines"
      ],
      "rollForward": false
    }
  }
}
notes.txt: 3 lines
notes.txt: 3 lines
--- a fresh clone on another machine
Tool 'ada.countlines' (version '0.1.0') was restored. Available commands: countlines

Restore was successful.
Package Id          Version      Commands        Manifest
--------------------------------------------------------------------------------------
ada.countlines      0.1.0        countlines      /home/learner/clone/dotnet-tools.json
  • dotnet new tool-manifest creates dotnet-tools.json. With the .NET 10 SDK it's created in the current folder (older SDKs used .config/dotnet-tools.json; both locations are still searched, walking up from the current directory).
  • dotnet tool install without -g installs into the manifest, pinning the version, and the package lands in the normal global packages folder.
  • The tool runs as dotnet countlines or dotnet tool run countlines, only inside the repository.
  • In a fresh clone, dotnet tool restore installs exactly the pinned versions. Put it in CI right after checkout, next to dotnet restore.
  • "rollForward": false means the tool must run on the framework it targets; set it to true (dotnet tool install --allow-roll-forward) for tools built for an older .NET than the one installed.

Global tools and one-shot runs

Command Installs to Use for
dotnet tool install <id> (with a manifest) the repo's manifest build tools the project depends on
dotnet tool install -g <id> ~/.dotnet/tools (on PATH after setup) personal utilities
dotnet tool install --tool-path ./tools <id> any folder self-contained CI images
dotnet tool exec <id> / dnx <id> (.NET 10) a cache, nothing installed running a tool once
bash · try it yourself
mkdir countlines && cd countlines
cat > countlines.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <PackAsTool>true</PackAsTool>
    <ToolCommandName>countlines</ToolCommandName>
    <PackageId>Ada.CountLines</PackageId>
    <Version>0.1.0</Version>
  </PropertyGroup>
</Project>
XML
echo 'foreach (var f in args) System.Console.WriteLine(
quot;{f}: {System.IO.File.ReadAllLines(f).Length} lines");' > Program.cs dotnet pack -o out -nologo -v:q && dotnet nuget push out/*.nupkg -s local > /dev/null cd .. dotnet tool install -g Ada.CountLines ls ~/.dotnet/tools ~/.dotnet/tools/countlines countlines/Program.cs echo "--- one-shot" dotnet tool exec Ada.CountLines --yes -- countlines/countlines.csproj
Tools directory '/home/learner/.dotnet/tools' is not currently on the PATH environment variable.
If you are using bash, you can add it to your profile by running the following command:

cat << \EOF >> ~/.bash_profile
# Add .NET Core SDK tools
export PATH="$PATH:/home/learner/.dotnet/tools"
EOF

You can add it to the current session by running the following command:

export PATH="$PATH:/home/learner/.dotnet/tools"

You can invoke the tool using the following command: countlines
Tool 'ada.countlines' (version '0.1.0') was successfully installed.
countlines
countlines/Program.cs: 1 lines
--- one-shot
countlines/countlines.csproj: 10 lines

The global install printed setup instructions because ~/.dotnet/tools isn't on this machine's PATH, so the tool is called by its full path. dotnet tool exec (also available as the dnx script that ships next to the dotnet executable) downloads the tool into a cache and runs it without touching any manifest or PATH; --yes skips the confirmation prompt. It's handy in scripts and documentation; for anything the build relies on, prefer the manifest, which pins the version.

Test yourself

What makes a NuGet package installable with dotnet tool install?

  1. Its id ends in .Tool
  2. It was packed with PackAsTool and declares the DotnetTool package type
  3. It contains an .exe
  4. It's on nuget.org

A new CI agent checks out a repo with dotnet-tools.json. Which command installs the pinned tools?

  1. dotnet tool install -g
  2. dotnet tool restore
  3. dotnet restore
  4. dotnet tool update

Why prefer local tools over global tools for build tooling?

  1. Local tools run faster
  2. The version is pinned in the repository, so every machine and CI run uses the same one
  3. Global tools can't run on Linux
  4. Local tools don't need NuGet