-
Notifications
You must be signed in to change notification settings - Fork 355
Add copy-to-output-directory MSBuild skill (IfDifferent / SkipUnchangedFilesOnCopyAlways) #883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
YuliiaKovalova
merged 4 commits into
dotnet:main
from
YuliiaKovalova:ykovalova/copy-to-output-directory-skill
Jul 16, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a608f8e
Add copy-to-output-directory MSBuild skill
YuliiaKovalova 63ffc97
Potential fix for pull request finding
YuliiaKovalova 7b22251
Address review feedback on copy-to-output-directory skill
YuliiaKovalova 7b047f2
Fix article: 'an SQLite' per review
YuliiaKovalova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
plugins/dotnet-msbuild/skills/copy-to-output-directory/SKILL.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| --- | ||
| name: copy-to-output-directory | ||
| description: "Choosing MSBuild CopyToOutputDirectory modes including IfDifferent (MSBuild 17.13+) and $(SkipUnchangedFilesOnCopyAlways), instead of the per-build Always copy perf hit." | ||
| license: MIT | ||
| --- | ||
|
|
||
| # Choosing a CopyToOutputDirectory Mode | ||
|
|
||
| ## Overview | ||
|
|
||
| The `CopyToOutputDirectory` metadata (and its publish counterpart `CopyToPublishDirectory`) controls whether an item — `Content`, `None`, `EmbeddedResource`, or `Compile` — is copied next to your build output, and under what conditions the copy happens. Picking the wrong mode causes either stale files in `bin/` or an unnecessary per-build performance hit. | ||
|
|
||
| As of **MSBuild 17.13 / .NET SDK 9.0.2xx** there are four values: | ||
|
|
||
| | Mode | Copies when… | Incremental cost | Typical use | | ||
| | --- | --- | --- | --- | | ||
| | `Never` (default) | Never | None | Files not needed at runtime | | ||
| | `PreserveNewest` | Source is **newer** than destination (or destination missing) | Cheap (timestamp check) | The common case — source files you edit | | ||
| | `Always` | **Every build**, unconditionally | Expensive — copies on every build even in no-op builds | Legacy workaround; avoid (see below) | | ||
| | `IfDifferent` | Source **differs** from destination in either direction (newer **or** older, or size differs, or destination missing) | Cheap (timestamp + size check) | Destination may be mutated between builds | | ||
|
|
||
| ```xml | ||
| <ItemGroup> | ||
| <None Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" /> | ||
| <None Include="testdata\seed.db" CopyToOutputDirectory="IfDifferent" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| You can use either the attribute form shown above or the child-element form: | ||
|
|
||
| ```xml | ||
| <None Include="testdata\seed.db"> | ||
| <CopyToOutputDirectory>IfDifferent</CopyToOutputDirectory> | ||
| </None> | ||
| ``` | ||
|
|
||
| ## Why `Always` is usually the wrong choice | ||
|
|
||
| `Always` re-copies the file on **every** build, including otherwise-clean incremental/no-op builds. On projects with many or large content files this is a measurable, recurring cost and a common cause of "why is my no-op build not instant?" reports. | ||
|
|
||
| Historically `Always` was the only way to handle a specific scenario: **the destination file can change between builds** — for example a SQLite database, a storage/state file, or a config file that a test run mutates. With `PreserveNewest`, if the destination is modified (making its timestamp *newer* than the source) MSBuild will *not* restore the original source file, because the source is no longer newer. People reached for `Always` to force the file back into a known-good state — paying the copy cost on every build as a side effect. | ||
|
|
||
| ## `IfDifferent`: copy when different, in either direction | ||
|
|
||
| `IfDifferent` is the targeted fix for that scenario. It copies the source over the destination whenever MSBuild considers the two files **different** (destination missing, file size differs, or last-write timestamps differ in either direction) and skips the copy when they are unchanged. | ||
|
|
||
| Under the hood the `_CopyDifferingSourceItemsToOutputDirectory` target uses the `Copy` task with `SkipUnchangedFiles="true"`. MSBuild's "unchanged" check compares **last-write timestamp and file size**; if either differs the file is copied. This usually achieves the "reset a mutated destination" goal of `Always` without the unconditional per-build copy (it is a timestamp+size heuristic, not a content hash). | ||
|
|
||
| Use `IfDifferent` when: | ||
|
|
||
| - A test run or the app itself writes to the copied file (databases, caches, state/storage files, editable config) and you want each build to reset it to the source version. | ||
| - You were using `Always` purely as a "keep the output in sync with the source" mechanism, not because you truly need a copy on every single build. | ||
|
|
||
| ```xml | ||
| <ItemGroup> | ||
| <!-- Reset the fixture DB to the source copy whenever it has drifted, | ||
| but don't pay a copy on every no-op build. --> | ||
| <None Include="fixtures\catalog.db" CopyToOutputDirectory="IfDifferent" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| ## Globally softening `Always` with `$(SkipUnchangedFilesOnCopyAlways)` | ||
|
|
||
| If you have an existing codebase full of `CopyToOutputDirectory="Always"` items and want the performance benefit without editing every item, set the property: | ||
|
|
||
| ```xml | ||
| <PropertyGroup> | ||
| <SkipUnchangedFilesOnCopyAlways>true</SkipUnchangedFilesOnCopyAlways> | ||
| </PropertyGroup> | ||
| ``` | ||
|
|
||
| This makes the `_CopyOutOfDateSourceItemsToOutputDirectoryAlways` target pass `SkipUnchangedFiles="true"` to its `Copy` task, so `Always` items are only copied when they actually differ — effectively giving `Always` the same skip-unchanged behavior as `IfDifferent`. | ||
|
|
||
| - Default is `false` for backwards compatibility (classic `Always` = copy every build). | ||
| - Set it in `Directory.Build.props` to opt an entire repo in at once. | ||
| - Prefer converting individual items to `IfDifferent` when you can; use this property when a bulk, non-invasive opt-in is more practical. | ||
|
|
||
| ## How the modes flow through the build | ||
|
|
||
| `GetCopyToOutputDirectoryItems` buckets each item by its `CopyToOutputDirectory` value into three copy targets, which run from `CopyFilesToOutputDirectory`: | ||
|
|
||
| - `_CopyOutOfDateSourceItemsToOutputDirectory` — `PreserveNewest` items (incremental via `Inputs`/`Outputs` timestamp comparison). | ||
| - `_CopyOutOfDateSourceItemsToOutputDirectoryAlways` — `Always` items (unconditional copy unless `$(SkipUnchangedFilesOnCopyAlways)` is `true`). | ||
| - `_CopyDifferingSourceItemsToOutputDirectory` — `IfDifferent` items (`SkipUnchangedFiles="true"`). | ||
|
|
||
| All copied files are registered in `FileWrites`, so `dotnet clean` removes them. | ||
|
|
||
| **Transitive copy:** items marked `Always`, `PreserveNewest`, or `IfDifferent` also flow to referencing projects through `ProjectReference` (via `_CopyToOutputDirectoryTransitiveItems`). `Never` items do not. `IfDifferent` participates in ClickOnce publish item collection alongside `Always`/`PreserveNewest`. | ||
|
|
||
| ## Version requirement | ||
|
|
||
| `IfDifferent` and `$(SkipUnchangedFilesOnCopyAlways)` require **MSBuild 17.13 or later** (**.NET SDK 9.0.2xx+ / Visual Studio 2022 17.13+**). On older toolsets the value is not recognized: it will not match the `Always`/`PreserveNewest`/`IfDifferent` conditions in the common targets, so the item is silently **not copied**. Gate usage on the toolset if you must support older SDKs, or require the minimum SDK via `global.json`. | ||
|
|
||
| ## Quick decision guide | ||
|
|
||
| - Don't need the file at runtime → `Never` (or omit — it's the default). | ||
| - Normal source file you edit → `PreserveNewest`. | ||
| - Destination gets mutated between builds and must be reset to the source → `IfDifferent`. | ||
| - You truly need a fresh copy on literally every build → `Always` (rare). | ||
| - Stuck with lots of legacy `Always` and want the perf win without edits → keep `Always` but set `$(SkipUnchangedFilesOnCopyAlways)=true`. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.