关于 dotnet-local-tools
面向 .NET 开发团队的技能指南,介绍如何在仓库中管理 CLI 工具及版本。通过清单固定工具版本,帮助本地开发与 CI/CD 使用一致工具并减少项目间冲突。
主要功能
- 01按仓库安装和还原工具
- 02通过清单固定工具版本
- 03涵盖文档、迁移与格式化工具
- 04提供 CI/CD 配置示例
- 05介绍工具更新与故障排查
适用场景
- 01为团队统一开发工具版本
- 02在 CI/CD 中还原项目工具
- 03避免不同项目的全局工具冲突
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ aaronontheweb/ dotnet-skills --skill dotnet-local-tools安装后在 Claude Code 中输入/dotnet-local-tools确认已加载。
详细介绍
.NET 本地工具
何时使用此技能
在以下情况下使用此技能:
- 为开发团队设置统一的工具
- 确保 CI/CD 流水线使用的工具版本与本地开发一致
- 管理项目专用的 CLI 工具(docfx、incrementalist、dotnet-ef 等)
- 避免不同项目之间发生全局工具版本冲突
什么是本地工具?
本地工具是按仓库安装和进行版本管理的 .NET CLI 工具,而非全局安装。它们定义在 .config/dotnet-tools.json 中,并通过 dotnet tool restore 还原。
本地工具与全局工具
| 方面 | 全局工具 | 本地工具 |
|---|---|---|
| 安装 | dotnet tool install -g | dotnet tool restore |
| 范围 | 整台计算机 | 每个仓库 |
| 版本控制 | 手动 | 在 .config/dotnet-tools.json 中 |
| CI/CD | 必须逐个安装工具 | 只需一条还原命令 |
| 冲突 | 可能发生版本冲突 | 按项目隔离 |
设置本地工具
初始化清单
# Create .config/dotnet-tools.json
dotnet new tool-manifest
这会创建:
.config/
└── dotnet-tools.json
在本地安装工具
# Install a tool locally
dotnet tool install docfx
# Install specific version
dotnet tool install docfx --version 2.78.3
# Install from a specific source
dotnet tool install MyTool --add-source https://mycompany.pkgs.visualstudio.com/_packaging/feed/nuget/v3/index.json
还原工具
# Restore all tools from manifest
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 documentation generator
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 for migrations
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 for coverage reports
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 - build only changed projects
dotnet tool install incrementalist.cmd
"incrementalist.cmd": {
"version": "1.2.0",
"commands": ["incrementalist"],
"rollForward": false
}
用法:
# Get projects affected by changes since main branch
incrementalist --branch main
代码格式化
# CSharpier - opinionated C# formatter
dotnet tool install csharpier
"csharpier": {
"version": "0.30.3",
"commands": ["dotnet-csharpier"],
"rollForward": false
}
用法:
dotnet csharpier .
dotnet csharpier --check . # CI mode - fails if changes needed
代码分析
# JB dotnet-inspect (requires license)
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: Setup .NET
uses: actions/setup-dotnet@v4
with:
global-json-file: global.json
- name: Restore tools
run: dotnet tool restore
- name: Build
run: dotnet build
- name: Test with coverage
run: dotnet test --collect:"XPlat Code Coverage"
- name: Generate coverage report
run: dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:coveragereport
- name: Build documentation
run: dotnet docfx docs/docfx.json
Azure Pipelines
steps:
- task: UseDotNet@2
inputs:
useGlobalJson: true
- script: dotnet tool restore
displayName: 'Restore .NET tools'
- script: dotnet build -c Release
displayName: 'Build'
- script: dotnet test -c Release --collect:"XPlat Code Coverage"
displayName: 'Test'
- script: dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:$(Build.ArtifactStagingDirectory)/coverage
displayName: 'Generate coverage report'
管理工具版本
更新工具
# Update to latest version
dotnet tool update docfx
# Update to specific version
dotnet tool update docfx --version 2.79.0
列出已安装的工具
# List local tools
dotnet tool list
# List with outdated check
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 中添加注释或章节:
## Development Setup
1. Restore tools: `dotnet tool restore`
2. Build: `dotnet build`
3. Test: `dotnet test`
5. 使用 Dependabot 更新
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: "nuget"
directory: "/"
schedule:
interval: "weekly"
# Includes local tools in .config/dotnet-tools.json
故障排除
还原后找不到工具
请确保从仓库根目录运行:
# Wrong - running from subdirectory
cd src/MyApp
dotnet docfx # Error: tool not found
# Correct - run from solution root
cd ../..
dotnet docfx docs/docfx.json
版本冲突
如果遇到版本冲突,请检查:
- 版本不同的全局工具:
dotnet tool list -g - 多个工具清单:查找父目录中的
.config/dotnet-tools.json
清除工具缓存
# Clear NuGet tool cache
dotnet nuget locals all --clear
# Re-restore tools
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
}
}
}
开发工作流:
# Initial setup
dotnet tool restore
# Format code before commit
dotnet csharpier .
# Run tests with coverage
dotnet test --collect:"XPlat Code Coverage"
dotnet reportgenerator -reports:**/coverage.cobertura.xml -targetdir:coverage
# Build documentation
dotnet docfx docs/docfx.json
# Check which projects changed (for large repos)
incrementalist --branch main
评论
支持 Markdown