Including Generated Files
.NET (Microsoft)OfficialPublished by .NET (Microsoft)
- Skill
- Tool integration
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This guide explains how to correctly integrate files generated during the MSBuild execution phase into your project. Because MSBuild performs globbing and item evaluation before the execution phase, files created during the build are often missed by the compiler or output processes. This skill provides the patterns for manually adding these artifacts to the appropriate item groups (Compile, Content, or None) and ensuring they are tracked for cleanup via FileWrites, while emphasizing the importance of correct target timing and the use of the IntermediateOutputPath property.
What it does
- Explains why generated files are often ignored by MSBuild.
- Provides patterns for adding generated files to Compile, Content, and None item groups.
- Demonstrates how to use FileWrites to ensure generated artifacts are cleaned up properly.
- Details the correct use of BeforeTargets for different types of generated files.
- Advises on using $(IntermediateOutputPath) to ensure build portability.
- Clarifies the difference between evaluation-time and execution-time globbing.
When to use it
- When your build process generates source code that needs to be compiled.
- When generated configuration or data files are missing from the output directory.
- When you need to ensure generated files are removed during a clean build.
- When you encounter issues with files not being captured by default build globs.
- When you need to control the order of file generation relative to compilation.
Advantages
- Prevents stale artifacts by ensuring proper cleanup.
- Ensures generated source code is correctly picked up by the compiler.
- Improves build reliability across different environments and CI systems.
- Provides a standardized approach to handling build-time file generation.
Usage examples
- “How do I make sure my generated .cs file is compiled in MSBuild?”
- You should create a target that runs before CoreCompile or BeforeCompile, generate your file, and then add it to the Compile and FileWrites item groups within that target.
- “Why are my generated files not being deleted when I run a clean build?”
- You need to add your generated files to the FileWrites item group within your generation target so MSBuild knows to track and remove them during the clean process.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/dotnet/including-generated-files/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip including-generated-files.zip -d .agents/skills
Documentation by .NET (Microsoft)
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 comes first, providing robust ordering regardless of customizations in the build.
Target Timing
Choose the BeforeTargets value based on the type of file being generated:
BeforeTargets="BeforeBuild"— For non-code files added toNoneorContent. Runs early enough for copy-to-output scenarios.BeforeTargets="CoreCompile;BeforeCompile"— For generated source files added toCompile. Ensures the file is included before the compiler runs.BeforeTargets="AssignTargetPaths"— The "final stop" beforeNoneandContentitems (among others) are transformed into new items. Use as a fallback ifBeforeBuildis too early.
Globbing Behavior
Globs behave according to when the glob took place:
| Glob Location | Files Captured |
|---|---|
| Outside of a target | Only files visible during Evaluation phase (before build starts) |
| Inside of a target | Files visible when the target runs (can capture generated files if timed correctly) |
This is why the solution places the <ItemGroup> inside a <Target> - the glob runs during execution when the generated files exist.
Relevant Links
Files
- ATTRIBUTION.mdAdded by AutoGineer864 B
- LICENSELicence file added by AutoGineer1.1 KiB
- SKILL.md7.0 KiB
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Published by
- .NET (Microsoft)
- Publisher
- .NET (Microsoft) (Official)
- Source
- dotnet/skills · plugins/dotnet-msbuild/skills/including-generated-files · commit 96c3d68
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by .NET (Microsoft) or .NET (Microsoft).
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download including-generated-files.zip