跳到正文
Skillpedia
部署与云(DevOps)5.8k · #14602

incremental-build

它能做什么

这项技能找出为何在没有任何改动的第二次运行中,MSBuild 仍会重新构建目标,并修复设置。适合无改动构建和完整构建一样慢的开发者。

适用场景

  • ✓当构建在没有任何改动的情况下仍重新运行所有内容时。
  • ✓当自定义目标每次都运行,而不是被跳过时。
  • ✓当构建在重复运行时很慢,而第一次运行却不慢的时候。

技能链接

https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/incremental-build/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/incremental-build/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我没有做任何改动,但我的 .NET 构建仍然重新构建整个项目。请找出原因并修复它。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会比较缓慢目标的输入和输出时间戳,找出缺失的输入和输出,或写入到跟踪输出之外的文件,然后提出修复方案,并通过两次完全相同的构建进行验证。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Required diagnosis output

For every diagnose or explain request, the final answer must explicitly cover all of these points, even when the immediate cause is simply missing Inputs/Outputs:

  • the target and exact root-cause evidence;
  • the actual input/output timestamp comparison;
  • missing, deleted, cleaned, or rewritten outputs;
  • complete inputs, including dynamic or volatile inputs;
  • evaluated input/output paths and one-to-one mapping when item lists are batched;
  • two identical no-change builds and the expected second-build skip/out-of-date evidence.

Do not edit unless the user asks for a fix. If a check cannot be run, label it as an unverified check rather than omitting it or claiming success. When Inputs or Outputs are absent, report timestamp comparison or one-to-one mapping as not applicable — the incremental mechanism is not engaged, not as an unverified comparison.

How MSBuild Incremental Build Works

MSBuild's incremental build mechanism allows targets to be skipped when their outputs are already up to date, dramatically reducing build times on subsequent runs.

  • Targets with Inputs and Outputs attributes: MSBuild compares the timestamps of all files listed in Inputs against all files listed in Outputs. If every output file is newer than every input file, the target is skipped entirely.
  • Without Inputs/Outputs: The target runs every time the build is invoked. This is the default behavior and the most common cause of slow incremental builds.
  • Timestamp-based comparison: MSBuild uses file system timestamps (last write time) to determine staleness. It does not use content hashes. This means touching a file (updating its timestamp without changing content) will trigger a rebuild.
<!-- This target is incremental: skipped if Output is newer than all Inputs -->
<Target Name="Transform"
        Inputs="@(TransformFiles)"
        Outputs="@(TransformFiles->'$(OutputPath)%(Filename).out')">
  <!-- work here -->
</Target>

<!-- This target always runs because it has no Inputs/Outputs -->
<Target Name="PrintMessage">
  <Message Text="This runs every build" />
</Target>

Why Incremental Builds Break (Top Causes)

  1. Missing Inputs/Outputs on custom targets — Without both attributes, the target always runs. This is the single most common cause of unnecessary rebuilds.

  2. Volatile properties in Outputs path — If the output path includes something that changes between builds (e.g., a timestamp, build number, or random GUID), MSBuild will never find the previous output and will always rebuild.

  3. File writes outside of tracked Outputs — If a target writes files that aren't listed in its Outputs, MSBuild doesn't know about them. The target may be skipped (because its declared outputs are up to date), but downstream targets may still be triggered.

  4. Missing FileWrites registration — Files created during the build but not registered in the FileWrites item group won't be cleaned by dotnet clean. Over time, stale files can confuse incremental checks.

  5. Glob changes — When you add or remove source files, the item set (e.g., @(Compile)) changes. Since these items feed into Inputs, the set of inputs changes and triggers a rebuild. This is expected behavior but can be surprising.

  6. Property changes — Properties that feed into Inputs or Outputs paths (e.g., $(Configuration), $(TargetFramework)) will cause rebuilds when changed. Switching between Debug and Release is a full rebuild by design.

  7. NuGet package updates — Changing a package version updates project.assets.json and potentially many resolved assembly paths. This changes the inputs to ResolveAssemblyReferences and CoreCompile, triggering a rebuild.

  8. Build server VBCSCompiler cache invalidation — The Roslyn compiler server (VBCSCompiler) caches compilation state. If the server is recycled (timeout, crash, or manual kill), the next build may be slower even though MSBuild's incremental checks pass, because the compiler must repopulate its in-memory caches.

Diagnosing "Why Did This Rebuild?"

Use binary logs (binlogs) to understand exactly why targets ran instead of being skipped.

Before recommending a fix, report this compact checklist for each target that reran:

  1. Actual timestamp comparison — name the newest resolved input and oldest resolved output, with their last-write times, and state which input is newer.
  2. **Output existence
常见问题

无改动构建是什么意思?

这是没有编辑的第二次构建,理应快速完成,且其目标被跳过。

它会未经询问就修改文件吗?

它会先诊断,只有在你要求时才进行修复。

为什么易变的值会导致重新构建?

输出路径中的时间戳或随机 GUID 等值每次构建都会改变,因此 MSBuild 会把输出视为新的。

详情

作者的原始描述

Guide for optimizing MSBuild incremental builds. USE FOR: builds slower than expected on subsequent runs, 'nothing changed but it rebuilds anyway', diagnosing why targets re-execute unnecessarily, fixing broken no-op builds. Covers 8 common causes: missing Inputs/Outputs on custom targets, volatile properties in output paths (timestamps/GUIDs), file writes outside tracked Outputs, missing FileWrites registration, glob changes, Visual Studio Fast Up-to-Date Check (FUTDC) issues. Key diagnostic: look for 'Building target completely' vs 'Skipping target' in binlog. DO NOT USE FOR: first-time buil

repo: dotnet/skills

热度

GitHub 星标
5.8k
排名
#14602
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能