本文へスキップ
Skillpedia
デプロイとクラウド(DevOps)5.8k · #14601

including-generated-files

できること

このスキルは、ビルド中に作成されたファイルがコンパイルされず、コピーもされない問題を直します。MSBuildはビルドの実行前にファイル一覧を決めるためです。ビルドでコードやコンテンツを作成しているのに、それらが抜けてしまう開発者向けです。

使う場面

  • ✓ビルド手順で作られたソースファイルがコンパイルされないとき。
  • ✓生成されたファイルが出力フォルダーにないとき。
  • ✓ビルド中に作成されたファイルがプロジェクトに取り込まれないとき。

スキルのリンク

https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/including-generated-files/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/including-generated-files/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“ビルド中にC#ファイルを生成していますが、コンパイラがそれを認識しません。含まれるようにターゲットを修正してください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

エージェントは、ファイルを作成するターゲットの内部に、そのファイルをコンパイル対象と追跡対象の一覧へ追加し、ターゲットがコンパイルより前に実行されるよう設定します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

Including Generated Files Into Your Build

Overview

Files generated during the build are generally ignored by the build process. This leads to confusing results such as:

  • Generated files not being included in the output directory
  • Generated source files not being compiled
  • Globs not capturing files created during the build

This happens because of how MSBuild's build phases work.

Quick Takeaway

For code files generated during the build - we need to add those to Compile and FileWrites item groups within the target generating the file(s):

  <ItemGroup>
    <Compile Include="$(GeneratedFilePath)" />
    <FileWrites Include="$(GeneratedFilePath)" />
  </ItemGroup>

The target generating the file(s) should be hooked before CoreCompile and BeforeCompile targets - BeforeTargets="CoreCompile;BeforeCompile"

Why Generated Files Are Ignored

For detailed explanation, see How MSBuild Builds Projects.

Evaluation Phase

MSBuild reads your project, imports everything, creates Properties, expands globs for Items outside of Targets, and sets up the build process.

Execution Phase

MSBuild runs Targets & Tasks with the provided Properties & Items to perform the build.

Key Takeaway: Files generated during execution don't exist during evaluation, therefore they aren't found. This particularly affects files that are globbed by default, such as source files (.cs).

Solution: Manually Add Generated Files

When files are generated during the build, manually add them into the build process. The approach depends on the type of file being generated.

Use $(IntermediateOutputPath) for Generated File Location

Always use $(IntermediateOutputPath) as the base directory for generated files. Do not hardcode obj\ or construct the intermediary path manually (e.g., obj\$(Configuration)\$(TargetFramework)\). The intermediate output path can be redirected to a different location in some build configurations (e.g., shared output directories, CI environments). Using $(IntermediateOutputPath) ensures your target works correctly regardless of the actual path.

Always Add Generated Files to FileWrites

Every generated file should be added to the FileWrites item group. This ensures that MSBuild's Clean target properly removes your generated files. Without this, generated files will accumulate as stale artifacts across builds.

<ItemGroup>
  <FileWrites Include="$(IntermediateOutputPath)my-generated-file.xyz" />
</ItemGroup>

Basic Pattern (Non-Code Files)

For generated files that need to be copied to output (config files, data files, etc.), add them to Content or None items before BeforeBuild:

<Target Name="IncludeGeneratedFiles" BeforeTargets="BeforeBuild">
  
  <!-- Your logic that generates files goes here -->

  <ItemGroup>
    <None Include="$(IntermediateOutputPath)my-generated-file.xyz" CopyToOutputDirectory="PreserveNewest"/>
    
    <!-- Capture all files of a certain type with a glob -->
    <None Include="$(IntermediateOutputPath)generated\*.xyz" CopyToOutputDirectory="PreserveNewest"/>

    <!-- Register generated files for proper cleanup -->
    <FileWrites Include="$(IntermediateOutputPath)my-generated-file.xyz" />
    <FileWrites Include="$(IntermediateOutputPath)generated\*.xyz" />
  </ItemGroup>
</Target>

For Generated Source Files (Code That Needs Compilation)

If you're generating .cs files that need to be compiled, use BeforeTargets="CoreCompile;BeforeCompile". This is the correct timing for adding Compile items — it runs late enough that the file generation has occurred, but before the compiler runs. Using BeforeBuild is too early for some scenarios and may not work reliably with all SDK features.

<Target Name="IncludeGeneratedSourceFiles" BeforeTargets="CoreCompile;BeforeCompile">
  <PropertyGroup>
    <GeneratedCodeDir>$(IntermediateOutputPath)Generated\</GeneratedCodeDir>
    <GeneratedFilePath>$(GeneratedCodeDir)MyGeneratedFile.cs</GeneratedFilePath>
  </PropertyGroup>

  <MakeDir Directories="$(GeneratedCodeDir)" />

  <!-- Your logic that generates the .cs file goes here -->

  <ItemGroup>
    <Compile Include="$(GeneratedFilePath)" />
    <FileWrites Include="$(GeneratedFilePath)" />
  </ItemGroup>
</Target>

Note: Specifying both CoreCompile and BeforeCompile ensures the target runs before whichever target

よくある質問

生成されたファイルが無視されるのはなぜですか?

MSBuildはターゲットがファイルを作成する前に、評価の段階でファイルのパターンを読むため、新しいファイルが一覧に入っていません。

ファイルはどこに追加すべきですか?

そのファイルを生成するターゲットの内部に追加します。そうすれば、一覧に間に合います。

クリーンビルドにも対応していますか?

はい。ファイルを追跡対象にしておくと、クリーン操作で削除されやすくなります。

詳細

作者による元の説明

Own MSBuild generated-artifact integration. USE FOR: a target that creates or should create source, Content, None, or another physical file but the artifact is missing from compilation or output; target timing; evaluation-time glob misses; $(IntermediateOutputPath) placement; and FileWrites clean tracking. The prompt may describe the generated artifact without naming the producing task. DO NOT USE when the primary defect is general Include/Remove/Update semantics, item metadata or batching, duplicate/overlapping declarations, or a generated-item identity relationship; use item-management. Excl

repo: dotnet/skills

人気

GitHubのスター
5.8k
ランキング
#14601
ライセンス
MIT
更新日
2026/11/2

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル