.NET 本機工具
使用時機
以下情況適合使用此技能:
- 在開發團隊中建立一致的工具環境
- 確保 CI/CD 管線與本機開發使用相同的工具版本
- 管理專案專屬的 CLI 工具(例如 docfx、incrementalist、dotnet-ef 等)
- 避免不同專案之間的全域工具版本衝突
什麼是「本機工具」?
本機工具是指每個儲存庫層級安裝與版本控制的 .NET CLI 工具,而非安裝在全域環境中。它們定義在 .config/dotnet-tools.json,並透過 dotnet tool restore 進行還原。
本機工具 vs 全域工具
| 項目 | 全域工具 | 本機工具 |
|---|---|---|
| 安裝方式 | dotnet tool install -g | dotnet tool restore |
| 範圍 | 整台機器 | 單一儲存庫 |
| 版本控制 | 手動 | 寫在 .config/dotnet-tools.json 中 |
| CI/CD 整合 | 需逐一安裝每個工具 | 單一還原指令 |
| 衝突風險 | 可能發生版本衝突 | 專案間獨立隔離 |
設定本機工具
初始化工具清單
# 建立 .config/dotnet-tools.json
dotnet new tool-manifest
這會產生:
.config/
└── dotnet-tools.json
安裝工具到本機
# 將工具安裝到本機
dotnet tool install docfx
# 安裝特定版本
dotnet tool install docfx --version 2.78.3
# 從特定來源安裝
dotnet tool install MyTool --add-source https://mycompany.pkgs.visualstudio.com/_packaging/feed/nuget/v3/index.json
還原工具
# 從清單還原所有工具
dotnet tool restore
dotnet-tools.json 格式
{
"version": 1,
"isRoot": true,
"tools": {
"docfx": {
"version": "2.78.3",
"commands": [
"docfx"
],
"rollForward": false
},
"dotnet-ef": {
"version": "9.0.0",
"commands": [
"dotnet-ef"
],
"rollForward": false
},
"incrementalist.cmd": {
"version": "1.2.0",
"commands": [
"incrementalist"
],
"rollForward": false
},
"dotnet-reportgenerator-globaltool": {
"version": "5.4.1",
"commands": [
"reportgenerator"
],
"rollForward": false
}
}
}
欄位說明
| 欄位 | 說明 |
|---|---|
version | 清單結構版本(固定為 1) |
isRoot | 標記此清單為根(避免向上搜尋父目錄) |
tools | 工具設定的字典 |
tools.<name>.version | 要安裝的確切版本 |
tools.<name>.commands | 工具提供的 CLI 指令 |
tools.<name>.rollForward | 是否允許較新版本(為確保可重複性,通常設為 false) |
常見工具
文件產生
# DocFX - API 文件產生器
dotnet tool install docfx
"docfx": {
"version": "2.78.3",
"commands": ["docfx"],
"rollForward": false
}
用法:
dotnet docfx docfx.json
dotnet docfx serve _site
Entity Framework Core
# EF Core CLI(用於資料庫遷移)
dotnet tool install dotnet-ef
"dotnet-ef": {
"version": "9.0.0",
"commands": ["dotnet-ef"],
"rollForward": false
}
用法:
dotnet ef migrations add InitialCreate
dotnet ef database update
程式碼涵蓋率
# ReportGenerator - 涵蓋率報表產生器
dotnet tool install dotnet-reportgenerator-globaltool
"dotnet-reportgenerator-globaltool": {
"version": "5.4.1",
"commands": ["reportgenerator"],
"rollForward": false
}
用法:
dotnet reportgenerator -reports:coverage.cobertura.xml -targetdir:coveragereport -reporttypes:Html
增量建置
# Incrementalist - 只建置有變更的專案
dotnet tool install incrementalist.cmd
"incrementalist.cmd": {
"version": "1.2.0",
"commands": ["incrementalist"],
"rollForward": false
}
用法:
# 取得從 main 分支以來受影響的專案
incrementalist --branch main
程式碼格式化
# CSharpier - 固執己見的 C# 格式化工具
dotnet tool install csharpier
"csharpier": {
"version": "0.30.3",
"commands": ["dotnet-csharpier"],
"rollForward": false
}
用法:
dotnet csharpier .
dotnet csharpier --check . # CI 模式,若需變更則失敗
程式碼分析
# JB dotnet-inspect(需要授權)
dotnet tool install jb
"jb": {
"version": "2024.3.4",
"commands": ["jb"],
"rollForward": false
}
CI/CD 整合
GitHub Actions
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: 設定 .NET
uses: actions/setup-dotnet@v4
with:
global-json-file: global.json
- name: 還原工具
run: dotnet tool restore
- name: 建置
run: dotnet build
- name: 測試(含涵蓋率)
run: dotnet test --collect:"XPlat Code Coverage"
- name: 產生涵蓋率報表
run: dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:coveragereport
- name: 建置文件
run: dotnet docfx docs/docfx.json
Azure Pipelines
steps:
- task: UseDotNet@2
inputs:
useGlobalJson: true
- script: dotnet tool restore
displayName: '還原 .NET 工具'
- script: dotnet build -c Release
displayName: '建置'
- script: dotnet test -c Release --collect:"XPlat Code Coverage"
displayName: '測試'
- script: dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:$(Build.ArtifactStagingDirectory)/coverage
displayName: '產生涵蓋率報表'
管理工具版本
更新工具
# 更新到最新版本
dotnet tool update docfx
# 更新到指定版本
dotnet tool update docfx --version 2.79.0
列出已安裝的工具
# 列出本機工具
dotnet tool list
# 列出並檢查是否有新版本
dotnet tool list --outdated
移除工具
dotnet tool uninstall docfx
最佳做法
1. 務必設定 isRoot: true
避免 MSBuild 向上搜尋父目錄中的工具清單:
{
"version": 1,
"isRoot": true,
...
}
2. 固定確切版本
使用 "rollForward": false 以確保可重複建置:
"docfx": {
"version": "2.78.3",
"rollForward": false
}
3. 在 CI 中先還原再使用
在使用任何本機工具之前,務必執行 dotnet tool restore:
- run: dotnet tool restore
- run: dotnet docfx docs/docfx.json
4. 在 README 中說明工具需求
在 README 中加入註解或章節:
## 開發環境設定
1. 還原工具:`dotnet tool restore`
2. 建置:`dotnet build`
3. 測試:`dotnet test`
5. 使用 Dependabot 處理更新
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: "nuget"
directory: "/"
schedule:
interval: "weekly"
# 包含 .config/dotnet-tools.json 中的本機工具
疑難排解
還原後找不到工具
請確認你在儲存庫根目錄下執行:
# 錯誤 - 從子目錄執行
cd src/MyApp
dotnet docfx # 錯誤:找不到工具
# 正確 - 從解決方案根目錄執行
cd ../..
dotnet docfx docs/docfx.json
版本衝突
若遇到版本衝突,請檢查:
- 全域工具版本不同:
dotnet tool list -g - 多份工具清單:檢查父目錄中是否有
.config/dotnet-tools.json
清除工具快取
# 清除 NuGet 工具快取
dotnet nuget locals all --clear
# 重新還原工具
dotnet tool restore
範例:完整的開發環境設定
{
"version": 1,
"isRoot": true,
"tools": {
"docfx": {
"version": "2.78.3",
"commands": ["docfx"],
"rollForward": false
},
"dotnet-ef": {
"version": "9.0.0",
"commands": ["dotnet-ef"],
"rollForward": false
},
"dotnet-reportgenerator-globaltool": {
"version": "5.4.1",
"commands": ["reportgenerator"],
"rollForward": false
},
"csharpier": {
"version": "0.30.3",
"commands": ["dotnet-csharpier"],
"rollForward": false
},
"incrementalist.cmd": {
"version": "1.2.0",
"commands": ["incrementalist"],
"rollForward": false
}
}
}
開發流程:
# 初始設定
dotnet tool restore
# 提交前格式化程式碼
dotnet csharpier .
# 執行測試並取得涵蓋率
dotnet test --collect:"XPlat Code Coverage"
dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:coverage
# 建置文件
dotnet docfx docs/docfx.json
# 檢查哪些專案已變更(適用於大型儲存庫)
incrementalist --branch main