New to Claude Skills? Learn how to install them →

Idotnet on GitHub

Including Generated Files

Free

Ensure generated files are included in your MSBuild process.

by dotnet5.1k stars on dotnet/skills
1 views
Updated Aug 10, 2026
Get this skill

Free · Opens the source repo

What Including Generated Files does

The Including Generated Files skill addresses a common issue in MSBuild where files created during the build process are not included in the compilation or output. This can lead to frustrating errors, such as missing types (CS0246) or files that are not recognized by subsequent build targets. The skill provides a structured approach to modify your MSBuild targets to ensure that generated files are correctly included in the build process.

The core of the solution involves adding generated files to the appropriate item groups within the MSBuild targets. By utilizing the Compile and FileWrites item groups, you can ensure that the compiler recognizes generated source files and that the Clean target effectively removes stale files from previous builds. The skill emphasizes the importance of correct timing for these modifications, recommending that the target generating the files be executed before the CoreCompile and BeforeCompile targets.

Additionally, it highlights the significance of using the $(IntermediateOutputPath) variable for file paths instead of hardcoded values. This practice not only enhances portability across different build configurations but also aligns with best practices for managing generated files. The skill also provides specific XML snippets that illustrate how to integrate these changes into your MSBuild project files, making it easier for developers to implement the solution in their own projects.

This skill is particularly useful for developers working with custom build tasks or those who rely on generated files that are not handled by the Roslyn pipeline or T4 design-time generation. By following the guidelines provided, users can resolve issues related to file visibility during the build process, ensuring a smoother development experience.

When to use it

Use this skill when you have custom build tasks that generate files during the build but those files are not being compiled or included in the output.

When not to use it

This skill is not suitable for C# source generators that already function correctly via the Roslyn pipeline or for projects using non-MSBuild build systems.

What you can build with it

Fixing Missing Generated Source Files

A developer encounters CS0246 errors due to generated .cs files not being recognized during compilation. By applying this skill, they can modify their MSBuild targets to include these files properly.

Cleaning Up Generated Files

After multiple builds, stale generated files accumulate in the output directory. This skill helps ensure that these files are registered with the Clean target for proper removal.

Integrating Custom Build Tasks

A team has custom build tasks that create configuration files during the build. Using this skill, they can ensure these files are included in the output directory and handled correctly.

How to install Including Generated Files

View source

1. Install with the skills CLI

npx skills add dotnet/skills/including-generated-files --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by dotnet

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 to None or Content. Runs early enough for copy-to-output scenarios.
  • BeforeTargets="CoreCompile;BeforeCompile" — For generated source files added to Compile. Ensures the file is included before the compiler runs.
  • BeforeTargets="AssignTargetPaths" — The "final stop" before None and Content items (among others) are transformed into new items. Use as a fallback if BeforeBuild is too early.

Globbing Behavior

Globs behave according to when the glob took place:

Glob LocationFiles Captured
Outside of a targetOnly files visible during Evaluation phase (before build starts)
Inside of a targetFiles 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

Frequently asked questions about Including Generated Files

Similar skills