.NET SDKを使うと、dotnetコマンドを使ってビルドやパッケージ追加、プロジェクト作成などができます。さて、パッケージ追加コマンドは.NET8までdotnet add packageでしたが、.NET9からdotnet package addにコマンドが変更されました。.NET9はSTSなので、LTSとしては.NET 10からの変更です。
今回はこの変更について見ていきます。
dotnetコマンドの一覧
dotnetコマンド体系は、基本的にdotnet [sdk-options] [command] [command-options] [arguments]の形で構成されています。1SDKから実行するビルドなどのコマンドとしては、dotnet buildやdotnet run、dotnet testなどがあります。このコマンドは、コマンドとして実行したいbuild、new、runやtestは動詞として用意されていますが、他は名詞先行のコマンド体系になっています。
以下は.NET 8でのヘルプですが、パッケージ追加はdotnet add package、リファレンス追加はdotnet add referenceのように、動詞先行のコマンド体系も混じっていました。
$ dotnet --version 8.0.418 $ dotnet --help ... 省略 SDK commands: add Add a package or reference to a .NET project. build Build a .NET project. build-server Interact with servers started by a build. clean Clean build outputs of a .NET project. format Apply style preferences to a project or solution. help Show command line help. list List project references of a .NET project. msbuild Run Microsoft Build Engine (MSBuild) commands. new Create a new .NET project or file. nuget Provides additional NuGet commands. pack Create a NuGet package. publish Publish a .NET project for deployment. remove Remove a package or reference from a .NET project. restore Restore dependencies specified in a .NET project. run Build and run a .NET project output. sdk Manage .NET SDK installation. sln Modify Visual Studio solution files. store Store the specified assemblies in the runtime package store. test Run unit tests using the test runner specified in a .NET project. tool Install or manage tools that extend the .NET experience. vstest Run Microsoft Test Engine (VSTest) commands. workload Manage optional workloads.
.NET 9以降、先のコマンドがdotnet package addやdotnet reference addのように名詞先行のコマンド体系で扱えます。
$ dotnet --version 10.0.103 $ dotnet --help ... 省略 SDK commands: build Build a .NET project. build-server Interact with servers started by a build. clean Clean build outputs of a .NET project. format Apply style preferences to a project or solution. help Opens the reference page in a browser for the specified command. msbuild Run Microsoft Build Engine (MSBuild) commands. new Create a new .NET project or file. nuget Provides additional NuGet commands. pack Create a NuGet package. package Search for, add, remove, or list PackageReferences for a .NET project. publish Publish a .NET project for deployment. reference Add, remove, or list ProjectReferences for a .NET project. restore Restore dependencies specified in a .NET project. run Build and run a .NET project output. sdk Manage .NET SDK installation. solution Modify Visual Studio solution files. store Store the specified assemblies in the runtime package store. test Run unit tests using the test runner specified in a .NET project. tool Install or manage tools that extend the .NET experience. vstest Run Microsoft Test Engine (VSTest) commands. workload Manage optional workloads.
変更があったのはパッケージ関連とリファレンス関連です。例えば、パッケージ追加はdotnet package add、リファレンスの追加はdotnet reference addになっています。旧来のコマンドもエイリアスとして残っていますが、今後は新しいコマンドを使うといいでしょう。
| New noun-first form (.NET9+) | Alias for (.NET8-) |
|---|---|
dotnet package add |
dotnet add package |
dotnet package list |
dotnet list package |
dotnet package remove |
dotnet remove package |
dotnet reference add |
dotnet add reference |
dotnet reference list |
dotnet list reference |
dotnet reference remove |
dotnet remove reference |
一般的にCLIのコマンドは名詞先行で用意されていることが多く、dotnetにおいてはdotnet addやdotnet listは一意に何をするのか一貫性がないというのが起点のようです。たしかに一貫性は大事。
まとめ
dotnet packageには、add以外にlistやremoveもあるので、パッケージを操作するというのが迷いにくい名詞先行になったのは悪くなさそうです。個人的には、一般的に動詞先行のCLIコマンドが多い気もするのですが、dotnetとして一貫性があればいいと感じます。
実際のところ、パッケージ追加コマンドを始めてみたときにdotnet add packageだったのは違和感がありました。慣れてしまって忘れていましたが、当時を思い返すとdotnet package ...でコマンドが統一されるのは使いやすいと感じます。はじめは戸惑いますが、エイリアスもあるので焦らず慣れていくといいでしょう。
参考
リリースノート
Issue
- Offer noun-first commands for existing verbs where noun isn't obvious #9650 | GitHub
- Update the dotnet CLI docs to document the new noun-first command forms #45001 | GitHub
redditでも好みが分かれる話だったりします
-
[sdk-options]は--list-sdksとかのdotnetコマンド自体のオプションなので、ここでは省略します。↩