With the .NET 10 SDK, what do dotnet tool exec and the dnx script do, and when would you use them instead of installing a tool?
answer
- one-shot run, no install
- package@version, ranges like @2.*
- downloads to the NuGet cache
- honours the local manifest version
- dnx script on PATH, dotnet dnx hidden
basics
~20 sdotnet tool exec (alias of dotnet tool execute) downloads a tool package to the NuGet cache and runs it once without installing it or touching PATH; dnx is the shell script shortcut for the same command, added in the .NET 10 SDK.
solid answer
~40 s`dotnet tool exec <id>[@<version>] [-- args]` resolves a version (the one you give, a range such as `@2.*`, or the latest), downloads the package into the NuGet cache if it is not there, runs it and returns the tool's exit code. Nothing is added to PATH or to a manifest. If you give no version and a local manifest in scope lists the package, it runs the manifest's version instead. The installer puts a `dnx` script on PATH (`dnx.cmd` on Windows), so `dnx <id>` is the short form; `dotnet dnx` is a hidden alias used by that script. Options mirror install: `--source`, `--add-source`, `--prerelease`, `--allow-roll-forward`. It suits trying a tool or a one-off CI step, while a manifest stays the way to pin tools a repository depends on.
code
bash · 8 lines# Try a tool without installing it
dnx dotnetsay -- Hello from the print room
# Reproducible one-off CI step: version pinned in the pipeline
dotnet tool exec [email protected] -- --version
# Inside a repo whose manifest pins dotnet-ef: runs the manifest's version
dnx dotnet-ef -- migrations listgo deeper
Recall that the .NET 10 SDK can run a tool without installing it, with dnx <tool> or dotnet tool exec <tool>.
Explain how exec picks a version, where the package goes, that it returns the tool's exit code, and that a manifest version wins when no version is given.
Decide per use: dnx for trials, a pinned exec for a single pipeline step, a manifest for anything the repository depends on; never leave an unpinned call in CI.
Weigh convenience against supply-chain control: one-shot execution makes pulling arbitrary packages trivial, so feed restrictions and pinning rules matter more, not less.
## The problem it solves Before .NET 10, running a .NET tool meant installing it first: globally (`-g`), into a folder (`--tool-path`) or into a manifest. For a tool you need once, such as a report generator in a single pipeline step or a utility someone mentioned in a code review, that leaves an install behind or edits a file you then have to revert. The .NET 10 SDK adds **one-shot execution**. ## The command `dotnet tool execute` (alias **`dotnet tool exec`**) takes a package id, optionally with a version, and the tool's own arguments after `--`: 1. It decides the version: the exact one you give (`[email protected]`), the best match for a range (`dotnetsay@2.*`), or the latest from your feeds. 2. It downloads the package into the NuGet cache if it is not already there. 3. It runs the tool with the forwarded arguments. 4. It returns the tool's exit code, so a pipeline step fails when the tool fails. Nothing is written to PATH, to `~/.dotnet/tools` or to a manifest. The cached package is reused next time. ## Interaction with local manifests When you give **no version** and a tool manifest in scope already lists the package, `dotnet tool exec` restores and runs the **manifest's** version rather than the latest. Inside a repository that pins its tools, `dnx dotnet-ef` therefore runs the pinned `dotnet-ef`; asking for an explicit version bypasses the manifest. ## dnx The SDK installer places a small script named **`dnx`** next to the `dotnet` executable, so it is on PATH wherever `dotnet` is (`dnx.cmd` on Windows). `dnx <id> [args]` forwards to the hidden `dotnet dnx` command, which is the same implementation as `dotnet tool exec`. A `dnx.ps1` variant appeared in .NET 10 previews and was removed before GA, because PowerShell swallowed the `--` separator. ## Options | Option | Purpose | |---|---| | `--source`, `--add-source`, `--configfile` | which feeds to resolve from | | `--prerelease` | allow prerelease versions when resolving | | `--allow-roll-forward` | run on a newer installed runtime if the tool's target runtime is missing | | `--interactive` | allow authentication prompts for private feeds | | `-v` | verbosity of the restore | ## When to use which - **Trying a tool** or running it once on your machine: `dnx <id>`. - **A single CI step** with no other use of the tool: `dotnet tool exec <id>@<version>`, with the version written in the pipeline so the step is reproducible. - **Tools the repository depends on** (the EF Core tool, a formatter in a pre-commit check): a **manifest** with `dotnet tool restore`, because the version is reviewed with the code and every developer gets it. - **A personal convenience** you use everywhere: `-g`. An unpinned `dnx <id>` in a pipeline runs whatever version is newest on the day, which is exactly the drift manifests exist to prevent. ## Common mistakes - Expecting `dnx` on an SDK older than 10.0.100: the script and the command do not exist there, so pipelines that mix SDK versions must pin the SDK first. - Passing the tool's own options before `--` when they clash with exec's options, such as `--version` or `--source`: exec consumes them. Put tool arguments after the separator. - Treating exec as sandboxed. The downloaded package runs in full trust exactly like an installed tool.
- Why can an unpinned dnx call break a pipeline months later?Without a version, and with no manifest entry, `dnx` resolves the latest version on the configured feeds each time. A new major release with changed options or behaviour then runs on the next build, with no change in your repository to explain it.
- Does dotnet tool exec leave anything behind?Only the downloaded package in the NuGet cache, which later runs reuse. It does not add a command to PATH, create a global install or modify a tool manifest.
- dotnet tool exec installs the tool globally the first time it runs.
- dnx ignores local tool manifests, so it always runs the latest version.
- dnx is an external utility you install separately from the SDK.
- dotnet tool exec works on the .NET 8 SDK as well.
- A one-shot dnx call is the recommended way to pin a team's tools.
Assumes the .NET 10 SDK (10.0.100 or later); dotnet tool exec, dotnet dnx and the dnx script did not exist in earlier SDKs.