{"slug":"target-authoring","title":"target-authoring","summary":"Canonical patterns for writing custom MSBuild targets. USE FOR: diagnosing and fixing custom target authoring anti-patterns; broken SDK target chains across files (e.g., Directory.Build.targets silently redefining SDK targets); targets that replace CompileDependsOn instead of ext","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-08-24T05:37:28.72998Z","repo":{"url":"https://github.com/dotnet/skills","stars":6296,"forks":500,"license":"MIT","updatedAt":"2026-11-19T18:23:42Z"},"bodyHtml":"<hr>\n<h2>name: target-authoring\ndescription: \"Canonical patterns for writing custom MSBuild targets. USE FOR: diagnosing and fixing custom target authoring anti-patterns; broken SDK target chains across files (e.g., Directory.Build.targets silently redefining SDK targets); targets that replace CompileDependsOn instead of extending it with $(CompileDependsOn); query targets returning stale results from Outputs vs Returns misuse; missing Inputs/Outputs causing unnecessary rebuilds; missing FileWrites registration. Covers DependsOnTargets vs BeforeTargets vs AfterTargets, the Build→CoreBuild three-level pattern, and the $(XxxDependsOn) chain-extension pattern. DO NOT USE FOR: incremental build tuning (use incremental-build), parallelization (use build-parallelism), general anti-patterns (use msbuild-antipatterns), non-MSBuild build systems.\"\nlicense: MIT</h2>\n<h1>Custom Target Authoring Patterns</h1>\n<p>Canonical patterns from <code>Microsoft.Common.CurrentVersion.targets</code> in the MSBuild repository.</p>\n<h2>The Three-Level Target Chain</h2>\n<p>Every major entry point (Build, Rebuild, Clean) delegates to a <strong>property</strong> listing its dependencies, which chains through Before → Core → After:</p>\n<pre><code>&lt;PropertyGroup&gt;\n  &lt;BuildDependsOn&gt;\n    BeforeBuild;\n    CoreBuild;\n    AfterBuild\n  &lt;/BuildDependsOn&gt;\n&lt;/PropertyGroup&gt;\n\n&lt;Target Name=\"Build\"\n    Condition=\" '$(_InvalidConfigurationWarning)' != 'true' \"\n    DependsOnTargets=\"$(BuildDependsOn)\"\n    Returns=\"@(TargetPathWithTargetPlatformMoniker)\" /&gt;\n\n&lt;!-- Empty extensibility targets — users override these --&gt;\n&lt;Target Name=\"BeforeBuild\" /&gt;\n&lt;Target Name=\"AfterBuild\" /&gt;\n</code></pre>\n<p><code>CoreBuild</code> delegates to <code>$(CoreBuildDependsOn)</code> and includes error handlers:</p>\n<pre><code>&lt;Target Name=\"CoreBuild\" DependsOnTargets=\"$(CoreBuildDependsOn)\"&gt;\n  &lt;OnError ExecuteTargets=\"_TimeStampAfterCompile;PostBuildEvent\"\n      Condition=\"'$(RunPostBuildEvent)' == 'Always'\" /&gt;\n  &lt;OnError ExecuteTargets=\"_CleanRecordFileWrites\" /&gt;\n&lt;/Target&gt;\n</code></pre>\n<h3>Rules</h3>\n<ul>\n<li>Delegate to a property (<code>DependsOnTargets=\"$(MyTargetDependsOn)\"</code>), not hardcoded targets.</li>\n<li><code>OnError</code> goes inside the orchestrating target to ensure cleanup runs even on failure.</li>\n<li>Empty Before/After targets are extensibility points. Users override them; SDKs never put logic in them.</li>\n</ul>\n<h2>Chain Extension — Append, Never Overwrite</h2>\n<p>When adding a custom target to an existing chain, <strong>append</strong> to the <code>DependsOn</code> property:</p>\n<pre><code>&lt;!-- GOOD: Append to existing chain --&gt;\n&lt;PropertyGroup&gt;\n  &lt;CompileDependsOn&gt;$(CompileDependsOn);MyCodeGenTarget&lt;/CompileDependsOn&gt;\n&lt;/PropertyGroup&gt;\n\n&lt;!-- BAD: Overwrites the entire chain, dropping SDK targets --&gt;\n&lt;PropertyGroup&gt;\n  &lt;CompileDependsOn&gt;MyCodeGenTarget&lt;/CompileDependsOn&gt;\n&lt;/PropertyGroup&gt;\n</code></pre>\n<h2>DependsOnTargets vs BeforeTargets vs AfterTargets</h2>\n<table>\n<thead>\n<tr>\n<th>Mechanism</th>\n<th>Defined in</th>\n<th>Best for</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>DependsOnTargets</code></td>\n<td>The target that needs deps</td>\n<td>Target explicitly requires others</td>\n</tr>\n<tr>\n<td><code>BeforeTargets</code></td>\n<td>The injecting target</td>\n<td>Insert before a target you don't own</td>\n</tr>\n<tr>\n<td><code>AfterTargets</code></td>\n<td>The injecting target</td>\n<td>Insert after a target you don't own</td>\n</tr>\n</tbody>\n</table>\n<p>Validation targets use <code>BeforeTargets</code> to intercept all entry points:</p>\n<pre><code>&lt;Target Name=\"_CheckForInvalidConfigurationAndPlatform\"\n    BeforeTargets=\"$(BuildDependsOn);Build;$(RebuildDependsOn);Rebuild;$(CleanDependsOn);Clean\"&gt;\n&lt;/Target&gt;\n</code></pre>\n<p><strong>Rules:</strong></p>\n<ul>\n<li>Use <code>DependsOnTargets</code> when your target needs specific prerequisites.</li>\n<li>Use <code>BeforeTargets</code>/<code>AfterTargets</code> when injecting into a pipeline you don't own.</li>\n<li>Prefer <code>BeforeTargets=\"CoreCompile\"</code> over modifying <code>$(CompileDependsOn)</code> when you don't control the targets file.</li>\n</ul>\n<h2>Returns vs Outputs</h2>\n<pre><code>&lt;!-- Build returns items for consumption by referencing projects --&gt;\n&lt;Target Name=\"Build\"\n    DependsOnTargets=\"$(BuildDependsOn)\"\n    Returns=\"@(TargetPathWithTargetPlatformMoniker)\" /&gt;\n\n&lt;!-- GetTargetPath is a lightweight query target --&gt;\n&lt;Target Name=\"GetTargetPath\" Returns=\"@(TargetPathWithTargetPlatformMoniker)\" /&gt;\n</code></pre>\n<ul>\n<li><strong><code>Returns</code></strong> specifies what the MSBuild task receives when calling this project. Use for inter-project communication.</li>\n<li><strong><code>Outputs</code></strong> on inner targets is for incrementality (timestamp checks). Use for up-to-date detection.</li>\n<li>Never mix the two purposes. Query targets (<code>GetTargetPath</code>, <code>GetTargetFrameworks</code>) should use <code>Returns</code>, not <code>Outputs</code>.</li>\n</ul>\n<h2>Target Naming Conventions</h2>\n<table>\n<thead>\n<tr>\n<th>Pattern</th>\n<th>Meaning</th>\n<th>Example</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>_PrefixedName</code></td>\n<td>Internal/private target</td>\n<td><code>_TimeStampBeforeCompile</code></td>\n</tr>\n<tr>\n<td><code>CoreXxx</code></td>\n<td>The actual implementation</td>\n<td><code>CoreBuild</code>, <code>CoreCompile</code></td>\n</tr>\n<tr>\n<td><code>BeforeXxx</code> / <code>AfterXxx</code></td>\n<td>Empty extensibility hooks</td>\n<td><code>BeforeBuild</code>, <code>AfterCompile</code></td>\n</tr>\n<tr>\n<td><code>PrepareXxx</code></td>\n<td>Setup/validation phase</td>\n<td><code>PrepareForBuild</code></td>\n</tr>\n<tr>\n<td><code>ResolveXxx</code></td>\n<td>Discovery/resolution phase</td>\n<td><code>ResolveReferences</code></td>\n</tr>\n<tr>\n<td><code>GetXxx</code></td>\n<td>Lightweight query (no side effects)</td>\n<td><code>GetTargetPath</code></td>\n</tr>\n</tbody>\n</table>\n<h2>Complete Custom Target Template</h2>\n<pre><code>&lt;!-- 1. Define the DependsOn chain for extensibility --&gt;\n&lt;PropertyGroup&gt;\n  &lt;MyFeatureDependsOn&gt;\n    _ValidateMyFeatureInputs;\n    BeforeMyFeature;\n    CoreMyFeature;\n    AfterMyFeature\n  &lt;/MyFeatureDependsOn&gt;\n&lt;/PropertyGroup&gt;\n\n&lt;!-- 2. Outer target with Returns for inter-project communication --&gt;\n&lt;Target Name=\"MyFeature\"\n    DependsOnTargets=\"$(MyFeatureDependsOn)\"\n    Returns=\"@(MyFeatureOutput)\" /&gt;\n\n&lt;!-- 3. Empty extensibility points --&gt;\n&lt;Target Name=\"BeforeMyFeature\" /&gt;\n&lt;Target Name=\"AfterMyFeature\" /&gt;\n\n&lt;!-- 4. Core implementation with Inputs/Outputs for incrementality --&gt;\n&lt;Target Name=\"CoreMyFeature\"\n    Inputs=\"$(MSBuildAllProjects);@(MyFeatureInput)\"\n    Outputs=\"$(IntermediateOutputPath)myfeature.generated.cs\"&gt;\n  &lt;Exec Command=\"my-tool.exe -o $(IntermediateOutputPath)myfeature.generated.cs\" /&gt;\n  &lt;!-- 5. Register outputs for clean tracking --&gt;\n  &lt;ItemGroup&gt;\n    &lt;Compile Include=\"$(IntermediateOutputPath)myfeature.generated.cs\" /&gt;\n    &lt;FileWrites Include=\"$(IntermediateOutputPath)myfeature.generated.cs\" /&gt;\n  &lt;/ItemGroup&gt;\n&lt;/Target&gt;\n\n&lt;!-- 6. Validation target runs first in the dependency chain --&gt;\n&lt;Target Name=\"_ValidateMyFeatureInputs\"&gt;\n  &lt;Error Text=\"MyFeatureInput items are required.\"\n         Condition=\"'@(MyFeatureInput)' == ''\" /&gt;\n&lt;/Target&gt;\n</code></pre>\n<h2>Common Pitfalls</h2>\n<ul>\n<li><strong>Overwriting <code>DependsOn</code> properties</strong> drops SDK targets silently. Always include <code>$(ExistingProperty)</code> when appending.</li>\n<li><strong>Using <code>Outputs</code> on query targets</strong> causes MSBuild to skip them when \"up to date,\" returning stale data. Use <code>Returns</code>.</li>\n<li><strong>Defining targets in <code>.props</code></strong> means <code>BeforeTargets</code> on SDK targets have nothing to hook into yet. Move targets to <code>.targets</code>.</li>\n<li><strong>Forgetting <code>OnError</code></strong> in orchestrating targets means file tracking fails on build errors, breaking subsequent incremental builds.</li>\n</ul>\n","files":[{"path":"SKILL.md","sizeBytes":6662,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-08-24T05:40:33.811258Z","sha256":"3BB95A590F001A1DF67D65A5D1B551BB92846436479BAF5C23CD2DB9A462DBF9","sizeBytes":2607},"review":null,"source":{"repositoryUrl":"https://github.com/dotnet/skills","path":"plugins/dotnet-msbuild/skills/target-authoring","license":"MIT","commit":"4e03bfdade5d1f9b7d1c2e95f64e31d1cb7af5ab","subtreeSha":"EA0FEAF5F2F4AB27735A75660A743AC1E104B511E851C67C54B40AAAC3E440E4","lastSyncedAt":"2026-11-19T19:30:03.195544Z"},"reviewedAt":"2026-08-24T05:49:23.716931Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/dotnet/skills/tree/main/plugins/dotnet-msbuild/skills/target-authoring"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install dotnet-skills@llmmart"},{"target":"git","command":"git clone https://github.com/dotnet/skills.git"}]}