Langsung ke konten
Skillpedia
Deploy & cloud (DevOps)5.8k Β· #14596

copy-to-output-directory

Fungsinya

Skill ini memandu pemilihan cara file proyek disalin ke output build di MSBuild. Skill ini untuk pengembang yang build-nya lambat karena file disalin setiap kali build, atau folder output-nya berisi file usang.

Kapan dipakai

  • βœ“Saat build lambat karena file disalin di setiap build.
  • βœ“Saat file output usang atau tidak diperbarui setelah perubahan.
  • βœ“Saat Anda perlu memilih mode salin untuk file konten atau data.

Link skill

https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/copy-to-output-directory/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/copy-to-output-directory/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBuild saya menyalin file data pengujian besar setiap kali padahal tidak ada yang berubah. Pilih pengaturan salin yang tepat.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Agent melihat bagaimana setiap file dipakai saat berjalan, lalu merekomendasikan mode salin yang sesuai, seperti mempertahankan file terbaru atau menyalin hanya jika berbeda.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

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
<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:

<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 an 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 different β€” whether the source is newer or older than the destination, whether the size differs, or the destination is missing β€” and skips the copy when the destination is unchanged per MSBuild's heuristic.

Under the hood the _CopyDifferingSourceItemsToOutputDirectory target uses the Copy task with SkipUnchangedFiles="true". That "unchanged" check is a heuristic: it compares last-write timestamp and file size only β€” not a content hash β€” so a destination that was edited to the same size and timestamp as the source is treated as unchanged and is not re-copied. In practice this restores a mutated destination back to the source version on the next build (the reason people reached for Always) while avoiding the unconditional per-build copy.

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.
<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:

<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-uncha

Pertanyaan umum

Apa saja mode salinnya?

Mode yang ada adalah Never, PreserveNewest, Always, dan IfDifferent, dengan IfDifferent tersedia di SDK yang lebih baru.

Mengapa menghindari Always?

Always menyalin di setiap build, bahkan saat tidak ada yang berubah, sehingga memperlambat build inkremental.

Apakah skill ini mencakup output publish?

Ya. Skill ini juga mencakup pengaturan salin untuk publish.

Detail

Deskripsi asli dari pembuat

Choosing an MSBuild CopyToOutputDirectory / CopyToPublishDirectory mode: Never, PreserveNewest, Always, and IfDifferent (MSBuild 17.13+), plus $(SkipUnchangedFilesOnCopyAlways). USE FOR: removing the per-build Always copy perf hit; resetting output files mutated between builds. DO NOT USE FOR: general incremental-build diagnosis (use incremental-build); non-MSBuild build systems.

repo: dotnet/skills

Popularitas

Bintang GitHub
5.8k
Peringkat
#14596
Lisensi
MIT
Diperbarui
2/11/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa