build-parallelism
它能做什么
这项技能分析包含多个项目的解决方案或构建文件,查看即使电脑有空闲处理器核心,构建仍然缓慢的原因。适合管理大型 .NET 解决方案且构建缓慢的开发者。
适用场景
- ✓当解决方案构建缓慢,而机器上的工作进程处于空闲状态时。
- ✓当增加并行工作进程也无法让构建变快时。
- ✓当项目引用形成了限制构建速度的长链时。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/build-parallelism/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的解决方案构建很慢,尽管我的机器有很多核心。请找出原因,并告诉我应该改什么。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会采集构建日志,找出最长的依赖项目链,并推荐修改方案,例如删除不需要的项目引用。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Diagnose a slow parallel build (start here)
Work this checklist in order — it targets the usual root cause (a serial dependency chain that no number of cores can parallelize):
- Check how many worker nodes actually built the solution. Capture a
binlog with
dotnet build /bl:{}(PowerShell:dotnet build '-bl:{}') and inspect the node timeline to see whether more than one node ran. EverydotnetCLI command built on the SDK's MSBuild-forwarding path (build,msbuild,test,pack,publish, etc.) enables/maxcpucount(multiple nodes) by default;-m:1forces a single node. A directMSBuild.exeinvocation is sequential by default, so pass-mexplicitly to enable parallel worker nodes there. - Find the critical path. From the binlog, read per-project timings and the node timeline. If total build time ≈ the sum of the projects on one dependency chain, that chain — not CPU count — is the bottleneck.
- Name the chain explicitly, e.g.
Core → Api → Web → Tests. A long serial chain stays serial no matter how large-mis, because each project waits on its predecessor. - Look for unnecessary
ProjectReferenceedges that lengthen the chain — a reference that only needs build order (not the output assembly), or one that could be aPackageReference, forces serialization it doesn't need. - Recommend flattening: break false dependencies so independent projects
build concurrently, and consider
/graphfor better scheduling.
MSBuild Parallelism Model
/maxcpucount(or-m): number of worker nodes (processes)- Every
dotnetCLI command (build,msbuild,test,pack,publish, etc.) passes/maxcpucountby default via the SDK's shared MSBuild-forwarding path;-m:1overrides it with one node - A direct
MSBuild.exeinvocation is sequential by default and requires an explicit-m - Recommended:
-mwithout a number = use all logical processors - Each node builds one project at a time
- Projects are scheduled based on dependency graph
Project Dependency Graph
- MSBuild builds projects in dependency order (topological sort)
- Critical path: longest chain of dependent projects determines minimum build time
- Bottleneck: if project A depends on B, C, D and B takes 60s while C and D take 5s, B is the bottleneck
- Diagnosis: replay binlog to diagnostic log with
performancesummaryand check Project Performance Summary — shows per-project time; grep fornode.*assignedto check scheduling - Wide graphs (many independent projects) parallelize well; deep graphs (long chains) don't
Graph Build Mode (/graph)
dotnet build /graphormsbuild /graph- What it changes: MSBuild constructs the full project dependency graph BEFORE building
- Benefits: better scheduling, avoids redundant evaluations, enables isolated builds
- Limitations: all projects must use
<ProjectReference>(no programmatic MSBuild task references) - When to use: large solutions with many projects, CI builds
- When NOT to use: projects that dynamically discover references at build time
Optimizing Project References
- Reduce a
<ProjectReference>only after proving it is unnecessary; a slow but valid dependency must stay in the graph - Use
<ProjectReference ... SkipGetTargetFrameworkProperties="true">to avoid extra evaluations <ProjectReference ... ReferenceOutputAssembly="false">for build-order-only dependencies- Consider if a ProjectReference should be a PackageReference instead (pre-built NuGet)
- Use
solution filters(.slnf) to build subsets of the solution
BuildInParallel
<MSBuild Projects="@(ProjectsToBuild)" BuildInParallel="true" />in custom targets- Without
BuildInParallel="true", MSBuild task batches projects sequentially - Ensure
/maxcpucount> 1 for this to have effect
Multi-threaded MSBuild Tasks
- Individual tasks can run multi-threaded within a single project build
- Tasks implementing
IMultiThreadableTaskcan run on multiple threads - Tasks must declare thread-safety via
[MSBuildMultiThreadableTask]
Analyzing Parallelism with Binlog
Primary: binlog MCP (preferred)
Use the binlog MCP server (Microsoft.AITools.BinlogMcp, exposed under the binlog MCP namespace):
- Use expensive_projects tool → find the slowest projects and compare individual vs total build time
- Use expensive_targets tool → find bottleneck targets
- Use project_target_times tool → drill into a sp
常见问题
它能帮助非 MSBuild 的构建吗?
不能。它只适用于基于 MSBuild 的解决方案和项目文件。
增加核心能解决很长的串行链吗?
通常不能。长的串行链无论增加多少核心,仍然是串行的。
它会修改我的项目吗?
它会推荐修改方案,由你审阅后再应用。
详情
作者的原始描述
Analyze an MSBuild solution, solution filter, or Build.proj that schedules multiple project files. USE FOR: measured idle worker nodes, `-m` that does not scale, a serial ProjectReference critical path, graph builds, or an MSBuild task that builds a Projects list. Requires at least two distinct project files whose scheduling or throughput must be analyzed. DO NOT USE for non-MSBuild build systems.
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14592
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。