MSBuild 输出路径冲突检测修复
check-bin-obj-clash
检测存在 OutputPath 或 IntermediateOutputPath 冲突的 MSBuild 项目,并修复该冲突。当构建失败并报出 'Cannot create a file when that file already exists' 或 'The process cannot access the file because it is being used by another process',或在多项目、多目标框架(multi-targeting)构建中因 bin/obj 或 project.assets.json 冲突而间歇性失败却能在重试时通过,或出现输出被覆盖时使用。不适用于操作系统层面的文件锁定、单项目单 TFM 构建,或非 MSBuild 构建系统。
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1
- Forks
- 1
01 前言 Front Matter
- name
- check-bin-obj-clash
- license
- MIT
- description
- Detects MSBuild projects with conflicting OutputPath or IntermediateOutputPath and fixes the clash. Use when builds fail with 'Cannot create a file when that file already exists' or 'The process cannot access the file because it is being used by another process', fail intermittently but pass on retry, or overwrite outputs in multi-project or multi-targeting builds where bin/obj or project.assets.json collide. Not for OS-level file locking, single-project single-TFM builds, or non-MSBuild build systems.
02 资源文件 13
- 参考文档ARD.md7.0 KB
- 参考文档config/settings.yaml1.1 KB
- 许可证LICENSE1.1 KB
- 其他NOTICE1.6 KB
- 参考文档PRD.md2.2 KB
- 参考文档references/common-causes-and-fixes.md13.1 KB
- 参考文档references/errors.md1.8 KB
- 参考文档references/examples.md3.1 KB
- 参考文档references/implementation.md1.3 KB
- 脚本scripts/data_fetcher.py7.5 KB
- 脚本scripts/formatters.py5.9 KB
- 脚本scripts/metrics_calculator.py7.9 KB
- 脚本scripts/onchain_analytics.py10.2 KB
03 SKILL.md 正文
<!-- cratis-ai-managed: skills/check-bin-obj-clash/SKILL.md -->
<!-- Modified by Cratis from dotnet's original (MIT): frontmatter, applicability, navigation and the changes listed in NOTICE. Upstream: https://github.com/dotnet/skills (plugins/dotnet-msbuild/skills/check-bin-obj-clash/SKILL.md; see NOTICE) -->
Detecting OutputPath and IntermediateOutputPath Clashes
Cratis rules in .cratis/ai/rules/ (including csharp.md, specs.csharp.md, and orleans.md) and the cratis-* skills win where they differ.
Overview
This skill helps identify when multiple MSBuild project evaluations share the same OutputPath or IntermediateOutputPath. This is a common source of build failures including:
- File access conflicts during parallel builds
- Missing or overwritten output files
- Intermittent build failures
- "File in use" errors
- NuGet restore errors like
Cannot create a file when that file already exists- this strongly indicates multiple projects share the sameIntermediateOutputPathwhereproject.assets.jsonis written
Clashes can occur between:
- Different projects sharing the same output directory
- Multi-targeting builds (e.g.,
TargetFrameworks=net8.0;net9.0) where the path doesn't include the target framework - Multiple solution builds where the same project is built from different solutions in a single build
Cratis qualification: BuildProjectReferences=false only suppresses building referenced projects; it does not prove that this project instance is read-only. Exclude an instance only when its actual targets and writes show a metadata-only query (for example, GetTargetPath).
When to Use This Skill
Invoke this skill immediately when you see:
Cannot create a file when that file already existsduring NuGet restoreThe process cannot access the file because it is being used by another process- Intermittent build failures that succeed on retry
- Missing output files or unexpected overwriting
Step 1: Generate a Binary Log
Use the binlog-generation skill to generate a binary log with the correct naming convention.
Primary workflow — binlog MCP
The MCP server exposes structured tools for inspecting a .binlog without
parsing text logs. Call them directly instead of replaying the binlog to a text
file. Call tools/list for the MCP first if you are unsure which tools are available.
Important constraints:
- The
.binlogfile is a binary format — do NOT try tocat,head,strings, or read it directly. Use only the MCP tools to query it. - Synthesize findings as you go. Do not spend all available time investigating — once you have enough evidence, present your conclusions.
Step 2: Get an overview and list projects
Use the MCP overview and projects tools to understand the build and list all projects that participated.
Step 3: Check evaluations and global properties
Use the MCP evaluations and evaluation_global_properties tools to find all evaluations per project. Look for:
- Multiple evaluations for the same project (indicates multi-targeting or multiple build configurations)
- Differing global properties between evaluations (
TargetFramework,Configuration,RuntimeIdentifier,SolutionFileName,PublishReadyToRun, etc.)
Step 4: Get output paths for each evaluation
Use the MCP properties tool to query OutputPath, IntermediateOutputPath, BaseOutputPath, and BaseIntermediateOutputPath for each project evaluation.
Step 5: Check for double writes
Use the MCP double_writes tool if available — it directly detects files written by multiple project instances.
Step 6: Identify clashes
Compare the OutputPath and IntermediateOutputPath values across all evaluations:
- Normalize paths - Convert to absolute paths and normalize separators
- Group by path - Find evaluations that share the same OutputPath or IntermediateOutputPath
- Filter out non-build evaluations - Exclude only instances proven by their targets and writes to be metadata-only queries;
BuildProjectReferences=falsealone is insufficient. - Report clashes - Any group with more than one evaluation indicates a clash
Fallback workflow — text-log replay (when MCP is unavailable)
Use this only when the MCP server cannot be started.
Replay the binlog to a diagnostic text log, then grep for the same signals the MCP tools surface:
dotnet msbuild build.binlog -noconlog -fl "-flp:v=diag;logfile=full.log"
Then extract the clash signals:
- Projects & evaluations — list evaluation starts and count them per project:
grep 'Evaluation started' full.log | grep -oiE '"[^"]+\.[a-z]+proj"' | sort | uniq -c. Matching the full quoted path keeps same-named projects in different directories distinct (and tolerates spaces in paths); a path with a count ≥ 2 was evaluated more than once (multi-targeting or extra global properties). (grep -calone only totals evaluations across the whole log, so it can't reveal per-project duplication.) - Output paths —
grep -iE 'OutputPath[[:space:]]*=|IntermediateOutputPath[[:space:]]*=|BaseOutputPath[[:space:]]*=|BaseIntermediateOutputPath[[:space:]]*=' full.log | sort -u, or query a project directly:dotnet msbuild MyProject.csproj -getProperty:OutputPath(andIntermediateOutputPath,BaseIntermediateOutputPath). - Distinguishing global properties —
grep -iE 'TargetFramework|Configuration|Platform|RuntimeIdentifier|SolutionFileName|PublishReadyToRun' full.log. See the Global Properties to Check table for which affect the path and which just fork a redundant instance. - Corroborating evidence (optional) —
grep 'Target "CopyFilesToOutputDirectory"' full.logplusgrep 'SkipUnchangedFiles' full.logshow a second instance writing (or skipping a masked write) to the same path; a long vs ~0 msCoreCompiledistinguishes the real build from a redundant instance.
Then identify clashes: normalize paths to absolute, group evaluations by OutputPath and by IntermediateOutputPath, and exclude only instances proven by their targets and writes to be metadata-only queries, not merely marked BuildProjectReferences=false — plus, for OutputPath only, MSBuildRestoreSessionId restore evaluations. Any group with more than one remaining evaluation is a clash.
Read references/common-causes-and-fixes.md when selecting the relevant diagnostic workflow.
Tips
- The SDK default paths include
$(TargetFramework)— clashes often occur when projects override these defaults; normalize relative paths to absolute before comparing. - Cross-project
IntermediateOutputPathclashes cannot be fixed withAppendTargetFrameworkToOutputPath— files likeproject.assets.jsonare written directly to the intermediate path. For multi-targeting clashes within the same project,AppendTargetFrameworkToOutputPath=trueis the correct fix. - Error messages that indicate a path clash:
Cannot create a file when that file already exists(NuGet restore),The process cannot access the file because it is being used by another process, or intermittent failures that succeed on retry.
Global Properties to Check When Comparing Evaluations
When multiple evaluations share an output path, compare these global properties to understand why:
| Property | Affects OutputPath? | Notes |
|---|---|---|
TargetFramework |
Yes | Different TFMs should have different paths. Exception: re-injecting a single-targeting project's own TFM (e.g. via SetTargetFramework with the same value) is path-neutral — it forks a redundant instance sharing the output path (see "SetTargetFramework re-injecting...") |
RuntimeIdentifier |
Yes | Different RIDs should have different paths |
Configuration |
Yes | Debug vs Release |
Platform |
Yes | AnyCPU vs x64 etc. |
SolutionFileName |
No | Identifies which solution built the project — different values indicate multi-solution clash |
SolutionName |
No | Solution name without extension |
SolutionPath |
No | Full path to the solution file |
SolutionDir |
No | Directory containing the solution file |
CurrentSolutionConfigurationContents |
No | XML with project entries — count of entries reveals which solution |
BuildProjectReferences |
No | false = P2P query, not a real build - ignore these |
MSBuildRestoreSessionId |
No | Present = restore phase evaluation |
PublishReadyToRun |
No | Publish setting, doesn't change build output path but creates distinct project instances |
_IsPublishing |
No | Publish flag; an <MSBuild> Build/Publish call with this set (in this project or another that consumes it) forks a publish instance sharing the build output path (see "Explicit <MSBuild> Build/Publish with extra global properties") |
Testing Fixes
After making changes to fix path clashes, clean and rebuild to verify. See the binlog-generation skill's "Cleaning the Repository" section on how to clean the repository while preserving binlog files.
04 同仓库更多 Skills
.NET SIMD 向量化代码
vectorization
设计、实现、优化和审查 .NET 中的 SIMD 代码:使用 TensorPrimitives、Vector64、Vector128、Vector256、Vector512 或硬件内部函数将标量循环向量化,并审查现有 SIMD 代码(包括泛型 Vector 类型)的契约等价性、尾部处理、内存安全性、可移植性、回退方案和实测性能。当正在编写或审查 .NET 循环或 SIMD 例程时使用。不用于与 SIMD 无关的性能工作。
MSBuild自定义目标编写
target-authoring
编写自定义 MSBuild target 的规范模式:DependsOnTargets 与 BeforeTargets、AfterTargets 的区别,Build-to-CoreBuild 三级模式,以及 XxxDependsOn 链式扩展模式。用于诊断或修复自定义 target 的反模式:Directory.Build.targets 重新定义 SDK target、target 替换而非扩展 CompileDependsOn、Outputs 与 Returns 误用导致返回过期结果、缺少 Inputs/Outputs 导致不必要的重新构建,或缺少 FileWrites 注册。不适用于增量构建调优或一般反模式审查(使用 msbuild 技能),也不适用于非 MSBuild 构建系统。
.NET测试命令执行
run-tests
选择并运行与仓库兼容的 .NET 测试命令:针对 MSTest、xUnit、NUnit 和 TUnit 的 classic、VSTest、bridged MTP 或 native MTP 语法。在运行 .NET 测试或需要给出测试命令/标志时使用:运行全部测试、单个测试、测试类、类别、trait 或目标框架、组合筛选器、--filter-query、--no-build、--diag、TRX、覆盖率收集、崩溃或挂起转储、筛选错误或无法识别的选项。不适用于仅做平台识别(使用 platform-detection)、编写或调试测试代码、解读覆盖率报告(使用 coverage-analysis)、CI 调查、迁移或持续的热重载循环。
.NET 性能剖析
profiling
使用免费的官方 .NET 诊断 CLI 工具在代码仓库中进行性能剖析和运行时调查:CPU、GC 压力、分配增长、异常风暴、锁竞争和慢代码。当 .NET 应用程序需要性能或运行时剖析时使用。不适用于用临时跟踪替代真实的性能或负载测试、无暂停风险计划的生产堆内存采集、崩溃转储(使用 dump-collect),或基准测试设计(使用 microbenchmarking)。
.NET 测试平台识别
platform-detection
识别 .NET 项目的测试平台、框架、命令模式,以及 SDK 样式与经典项目系统,并解析 global.json、项目文件、packages.config、Directory.Build.props 和 Directory.Packages.props 对 MSTest、xUnit、NUnit 和 TUnit 的优先级。当询问“使用哪种测试平台或框架”“VSTest 还是 MTP”或“该项目使用什么测试运行器”时使用,包括桥接设置、UseVSTest 退出选项,以及 VSTest/MTP 配置冲突。不适用于运行或筛选测试、命令、标志、TRX、转储或测试命令错误(使用 run-tests),也不适用于热重载或迁移请求。
Orleans 分布式系统设计
orleans
从每种原语的用途和失效模型出发设计 Microsoft Orleans 系统:grain、数字孪生、状态与数据库、事务、消息传递、流(streams)、计时器(timers)、提醒(reminders)、Durable Jobs(持久作业)、无状态工作进程、grain 服务、启动与托管。当任务需要做出 Orleans 设计决策时使用:检查版本与拓扑、选择原语、实现并验证。不适用于其他 actor 技术栈、批处理、纯关系型 CRUD,或不涉及 Orleans 决策的建议。