including-generated-files

Includes build-generated files into MSBuild compilation, output, and clean targets.

Updated Jul 12, 2026
One-click install
npx skills add https://github.com/Patrick-Rex/DotNetTechSamples --skill including-generated-files-patrick-rex
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: including-generated-files
Source: https://github.com/Patrick-Rex/DotNetTechSamples/tree/main/.agents/plugins/dotnet-msbuild/skills/including-generated-files
Command: npx skills add https://github.com/Patrick-Rex/DotNetTechSamples --skill including-generated-files-patrick-rex

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Files generated during an MSBuild build are invisible to the build process because globs expand during the evaluation phase, before the files exist. This causes generated source files to be skipped by compilation (e.g., CS0246 errors for types that should exist), generated content to be missing from the output directory, and stale artifacts to survive the Clean target. ## Core Features & Use Cases - Correct Target Timing: Hooks file-generation targets with BeforeTargets="CoreCompile;BeforeCompile" for source files, BeforeBuild for content files, and AssignTargetPaths as a fallback. - Item Group Registration: Adds generated files to Compile, None, or Content items plus the FileWrites item group so the Clean target removes them. - Portable Paths: Uses $(IntermediateOutputPath) instead of hardcoded obj\ paths so builds work with redirected intermediate output. - Use Case: A custom MSBuild task generates a .cs file during the build, but the compiler reports the type does not exist. Add the file to Compile and FileWrites inside a target running before CoreCompile to fix it. ## Quick Start Ask the assistant to fix your MSBuild target so the source files it generates are compiled and cleaned properly.

Frequently Asked Questions about including-generated-files

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I include generated source files in MSBuild compilation?

Add the generated .cs files to the Compile item group inside a target hooked with BeforeTargets="CoreCompile;BeforeCompile". This timing ensures the files exist before the compiler runs, since evaluation-phase globs cannot see files created during execution.

Why are my build-generated files not being compiled?

MSBuild expands globs during the evaluation phase, before any target runs, so files created during the build are never captured. Move the item declaration inside the generating target and add the files explicitly to the Compile item group.

How do I make the MSBuild Clean target remove generated files?

Add every generated file to the FileWrites item group inside your generating target. The Clean target uses FileWrites to know which files to delete, so unregistered generated files accumulate as stale artifacts across builds.

Which BeforeTargets value should I use for generated files in MSBuild?

Use CoreCompile;BeforeCompile for generated source files added to Compile, BeforeBuild for non-code files added to None or Content, and AssignTargetPaths as a fallback when BeforeBuild runs too early for item transformation.

Should I hardcode the obj folder path for generated files?

No, always use $(IntermediateOutputPath) as the base directory for generated files. The intermediate output path can be redirected in shared output or CI configurations, so hardcoding obj\ breaks those builds.

Does this apply to C# source generators or T4 templates?

No, Roslyn source generators already integrate with the compiler pipeline, and T4 design-time generation runs inside Visual Studio. This guidance targets custom MSBuild targets and tasks that produce files during build execution.