including-generated-files

Configure MSBuild targets to include build-generated files in compilation and output.

1|Updated Jun 1, 2026
One-click install
npx skills add https://github.com/D1ssolve/craft-agents --skill including-generated-files-d1ssolve
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: including-generated-files
Source: https://github.com/D1ssolve/craft-agents/tree/main/skills/including-generated-files
Command: npx skills add https://github.com/D1ssolve/craft-agents --skill including-generated-files-d1ssolve

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 execution creates them. This causes generated source files to be skipped by the compiler (e.g., CS0246 errors for types that should exist), generated content to be missing from output, and stale artifacts to survive the Clean target. ## Core Features & Use Cases - Correct Target Timing: Choose the right BeforeTargets hook (BeforeBuild, CoreCompile;BeforeCompile, or AssignTargetPaths) based on the type of file being generated. - Item Group Registration: Add generated files to Compile, None, or Content item groups inside the generating target, and register them in FileWrites so the Clean target removes them. - Path Best Practices: Use $(IntermediateOutputPath) instead of hardcoded obj/ paths so targets work with redirected intermediate output directories. - Use Case: A custom build task generates a .cs file at build time, but the compiler reports the type does not exist. Hook the target with BeforeTargets="CoreCompile;BeforeCompile" and add the file to the Compile and FileWrites item groups. ## Quick Start Fix my MSBuild target so the .cs file it generates during the build is 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 files in an MSBuild compilation?

Add the generated file to the Compile item group inside the target that creates it, and hook that target with BeforeTargets="CoreCompile;BeforeCompile". This ensures the file exists and is registered before the compiler runs.

Why are my build-generated files not being compiled in MSBuild?

MSBuild expands globs during the evaluation phase, before any target executes, so files created during the build are never captured. You must explicitly add generated files to item groups inside a target that runs after generation.

What BeforeTargets value should I use for generated source files?

Use BeforeTargets="CoreCompile;BeforeCompile" for generated .cs files so they are added to Compile before the compiler runs. For non-code files copied to output, BeforeTargets="BeforeBuild" is sufficient, with AssignTargetPaths as a fallback.

How do I make MSBuild Clean remove generated files?

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

Should I hardcode the obj folder path for generated files?

No. Use $(IntermediateOutputPath) as the base directory because the intermediate output path can be redirected in shared output or CI configurations. Hardcoding obj\ or constructing it manually breaks in those environments.

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

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