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
InputsandOutputsattributes: MSBuild compares the timestamps of all files listed inInputsagainst all files listed inOutputs. 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.
Why Incremental Builds Break (Top Causes)
-
Missing Inputs/Outputs on custom targets — Without both attributes, the target always runs. This is the single most common cause of unnecessary rebuilds.
-
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.
-
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. -
Missing FileWrites registration — Files created during the build but not registered in the
FileWritesitem group won't be cleaned bydotnet clean. Over time, stale files can confuse incremental checks. -
Glob changes — When you add or remove source files, the item set (e.g.,
@(Compile)) changes. Since these items feed intoInputs, the set of inputs changes and triggers a rebuild. This is expected behavior but can be surprising. -
Property changes — Properties that feed into
InputsorOutputspaths (e.g.,$(Configuration),$(TargetFramework)) will cause rebuilds when changed. Switching between Debug and Release is a full rebuild by design. -
NuGet package updates — Changing a package version updates
project.assets.jsonand potentially many resolved assembly paths. This changes the inputs toResolveAssemblyReferencesandCoreCompile, triggering a rebuild. -
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:
- Actual timestamp comparison — name the newest resolved input and oldest resolved output, with their last-write times, and state which input is newer.
- Output existence — confirm every declared output exists before the second build and is not deleted, cleaned, or rewritten by another target.
- Complete inputs — verify
Inputsincludes every file that should invalidate the output, including volatile or generated dependencies. If a producer touches an input on every build, check whether its content changes. Fix unnecessary rewrites in the producer (for example, write only when content changes); if relevant content really changes, the dependent target must rerun. Exclude a file only with evidence that it cannot affect the output, never just because it changes often. - Stable resolved paths — expand properties and relative paths for both builds; outputs must resolve to the same location and must not contain timestamps, build numbers, or GUIDs.
- One-to-one mapping when batched — for item transforms, map each input identity to its expected output and report missing, duplicate, or unmatched pairs.
- No-change verification — run the same build twice without edits and cite the second build's skip or out-of-date reason.
For a diagnosis or explanation request, do not edit the project unless the user asks for a fix. If build execution is unavailable, distinguish inspected facts from expected behavior and do not claim the fix was applied or verified.
Overwrite="true" on WriteLinesToFile is not itself a reason that an
incremental target reruns. The task rewrites the file only after MSBuild has
already scheduled the target; correct Inputs/Outputs can skip the target
before the task executes.
Step-by-step using binlog
- Build twice without edits and capture both structured and text evidence
from the second build:
The first build establishes the baseline. The second build is the one you want to be incremental. Analyze
second.binlog.
Primary: binlog MCP (preferred)
Use the binlog MCP server (Microsoft.AITools.BinlogMcp, exposed under the binlog MCP namespace) to analyze the second binlog:
- Use
load_binlogto loadsecond.binlog. - Use
search_binlogfor "Building target completely", "Building target incrementally", "Skipping target", and "is newer than output". - Use
get_target_info_by_namefor the target that reran. - Use
get_project_target_listto confirm the target and its project context. - Use
get_expensive_targetsonly to rank the non-skipped targets after the skip reason is understood.
Fallback: diagnostic text log (when MCP is unavailable)
-
Read the diagnostic log captured during the second build. Do not run another build or depend on replay support for the default fallback:
In a perfectly incremental build, most targets should be skipped.
-
Inspect non-skipped targets by looking for their execution messages in the diagnostic log. Check for "out of date" messages that indicate why a target ran.
-
Look for key messages in the binlog:
"Building target 'X' completely"— means MSBuild found no outputs or all outputs are missing; this is a full target execution."Building target 'X' incrementally"— means some (but not all) outputs are out of date."Skipping target 'X' because all output files are up-to-date"— target was correctly skipped.
-
Search for "is newer than output" messages to find the specific input file that triggered the rebuild:
This reveals exactly which input file's timestamp caused MSBuild to consider the target out of date.
Additional diagnostic techniques
- Compare
first.binlogandsecond.binlogside by side in the MSBuild Structured Log Viewer to see what changed. - Use
grep 'Target Performance Summary' -A 30 second-full.logto see which targets consumed the most time in the second build — these are your optimization targets. - Check for targets with zero-duration that still ran — they may have unnecessary dependencies causing them to execute.
FileWrites and Clean Build
The FileWrites item group is MSBuild's mechanism for tracking files generated during the build. It powers dotnet clean and helps maintain correct incremental behavior.
FileWritesitem: Register any file your custom targets create so thatdotnet cleanknows to remove them. Without this, generated files accumulate across builds and may confuse incremental checks.FileWritesShareableitem: Use this for files that are shared across multiple projects (e.g., shared generated code). These files are tracked but not deleted if other projects still reference them.- If not registered: Files accumulate in the output and intermediate directories.
dotnet cleanwon't remove them, and they may cause stale data issues or confuse up-to-date checks.
Pattern for registering generated files
Add generated files to FileWrites inside the target that creates them:
Visual Studio Fast Up-to-Date Check
Visual Studio has its own up-to-date check (Fast Up-to-Date Check, or FUTDC) that is separate from MSBuild's Inputs/Outputs mechanism. Understanding the difference is critical for diagnosing "it rebuilds in VS but not on the command line" issues.
- VS FUTDC is faster because it runs in-process and checks a known set of items without invoking MSBuild at all. It compares timestamps of well-known item types (Compile, Content, EmbeddedResource, etc.) against the project's primary output.
- It can be wrong if your project uses custom build actions, custom targets that generate files, or non-standard item types that FUTDC doesn't know about.
- Disable FUTDC to force Visual Studio to use MSBuild's full incremental check:
- Diagnose FUTDC decisions by viewing the Output window in VS: go to Tools → Options → Projects and Solutions → SDK-Style Projects and set Up-to-date Checks logging level to Verbose or above. FUTDC will log exactly which file it considers out of date.
- Common VS FUTDC issues:
- Custom build actions not registered with the FUTDC system
CopyToOutputDirectoryitems that are newer than the last build- Items added dynamically by targets that FUTDC doesn't evaluate
ContentorNoneitems withCopyToOutputDirectory="PreserveNewest"that have been modified
Making Custom Targets Incremental
The following is a complete example of a well-structured incremental custom target:
Key points in this example:
Inputsincludes$(MSBuildProjectFile): This ensures the target reruns if the project file itself changes (e.g., a property that affects generation is modified).Inputsincludes@(ConfigInput): The actual source files that drive generation.Outputsuses$(IntermediateOutputPath): Generated files go in theobj/directory, which is managed by MSBuild and cleaned automatically.BeforeTargets="CoreCompile": The generated file is available before the compiler runs.FileWritesregistration: Ensuresdotnet cleanremoves the generated file.Compileinclusion: Adds the generated file to the compilation without requiring it to exist at evaluation time.
Common mistakes to avoid
Performance Summary and Preprocess
MSBuild provides built-in tools to understand what's running and why.
-
/clp:PerformanceSummary— Appends a summary at the end of the build showing time spent in each target and task. Use this to quickly identify the most expensive operations:This shows a table of targets sorted by cumulative time, making it easy to spot targets that shouldn't be running in an incremental build.
-
/pp:preprocess.xml— Generates a single XML file with all imports inlined, showing the fully evaluated project. This is invaluable for understanding what targets, properties, and items are defined and where they come from:Search the preprocessed output to find where
InputsandOutputsare defined for any target, or to understand the full chain of imports. -
Use both together to understand what's running (
PerformanceSummary) and what's imported (/pp), then cross-reference with binlog analysis for a complete picture.
Common Fixes
- Always add
InputsandOutputsto custom targets — This is the single most impactful change for incremental build performance. Without both attributes, the target runs every time. - Use
$(IntermediateOutputPath)for generated files — Files inobj/are tracked by MSBuild's clean infrastructure and won't leak between configurations. - Register generated files in
FileWrites— Ensuresdotnet cleanremoves them and prevents stale file accumulation. - Avoid unnecessary volatility in producers — Don't embed timestamps, random values, or build counters in file paths or generated content unless required. Preserve generated-file timestamps when content is unchanged (for example,
Overwrite="true" WriteOnlyWhenDifferent="true"withWriteLinesToFile). Keep real dependencies inInputs; if required volatile content changes, accept the dependent rebuild. Exclude a file only after proving it cannot affect the output. - Use
Returnsinstead ofOutputswhen you need to pass items without creating incremental build dependency —Outputsserves double duty: it defines the incremental check AND the items returned from the target. If you only need to pass items to calling targets without affecting incrementality, useReturnsinstead:


