@cratis/pi 7.102.0 → 7.103.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package/corpus/harnesses/copilot/instructions/profiles.instructions.md +10 -1
- package/package/corpus/harnesses/copilot/instructions/pull-requests.instructions.md +2 -0
- package/package/corpus/harnesses/copilot/instructions/secrets.instructions.md +4 -0
- package/package/corpus/hooks/README.md +11 -1
- package/package/corpus/hooks/scripts/cratis-check-pr.mjs +12 -1
- package/package/corpus/hooks/scripts/cratis-guard-secrets.sh +79 -23
- package/package/corpus/hooks/scripts/cratis-guard-writes.sh +145 -40
- package/package/corpus/rules/profiles.md +10 -1
- package/package/corpus/rules/pull-requests.md +2 -0
- package/package/corpus/rules/secrets.md +4 -0
- package/package/corpus/skills/analyzing-dotnet-performance/LICENSE +23 -0
- package/package/corpus/skills/analyzing-dotnet-performance/NOTICE +36 -0
- package/package/corpus/skills/analyzing-dotnet-performance/SKILL.md +214 -0
- package/package/corpus/skills/analyzing-dotnet-performance/references/async-patterns.md +122 -0
- package/package/corpus/skills/analyzing-dotnet-performance/references/collections-and-linq.md +220 -0
- package/package/corpus/skills/analyzing-dotnet-performance/references/critical-patterns.md +291 -0
- package/package/corpus/skills/analyzing-dotnet-performance/references/io-and-serialization.md +135 -0
- package/package/corpus/skills/analyzing-dotnet-performance/references/memory-and-strings.md +219 -0
- package/package/corpus/skills/analyzing-dotnet-performance/references/regex-patterns.md +95 -0
- package/package/corpus/skills/analyzing-dotnet-performance/references/structural-patterns.md +38 -0
- package/package/corpus/skills/binlog-generation/LICENSE +23 -0
- package/package/corpus/skills/binlog-generation/NOTICE +22 -0
- package/package/corpus/skills/binlog-generation/SKILL.md +129 -0
- package/package/corpus/skills/check-bin-obj-clash/LICENSE +23 -0
- package/package/corpus/skills/check-bin-obj-clash/NOTICE +27 -0
- package/package/corpus/skills/check-bin-obj-clash/SKILL.md +127 -0
- package/package/corpus/skills/check-bin-obj-clash/references/common-causes-and-fixes.md +206 -0
- package/package/corpus/skills/coverage-analysis/LICENSE +23 -0
- package/package/corpus/skills/coverage-analysis/NOTICE +32 -0
- package/package/corpus/skills/coverage-analysis/SKILL.md +198 -0
- package/package/corpus/skills/coverage-analysis/references/guidelines.md +68 -0
- package/package/corpus/skills/coverage-analysis/references/output-format.md +91 -0
- package/package/corpus/skills/coverage-analysis/references/report-generation.md +58 -0
- package/package/corpus/skills/coverage-analysis/references/setup-discovery.md +307 -0
- package/package/corpus/skills/coverage-analysis/scripts/Compute-CrapScores.ps1 +80 -0
- package/package/corpus/skills/coverage-analysis/scripts/CoverageData.ps1 +272 -0
- package/package/corpus/skills/coverage-analysis/scripts/Extract-MethodCoverage.ps1 +65 -0
- package/package/corpus/skills/cratis-screenplay-command-surface/SKILL.md +26 -1
- package/package/corpus/skills/cratis-screenplay-projections/SKILL.md +17 -0
- package/package/corpus/skills/cratis-screenplay-render-and-gap-fill/SKILL.md +17 -0
- package/package/corpus/skills/cratis-screenplay-specifications/SKILL.md +8 -0
- package/package/corpus/skills/cratis-screenplay-toolchain/SKILL.md +1 -1
- package/package/corpus/skills/cratis-screenplay-toolchain/references/cheat-sheet-example.md +1 -0
- package/package/corpus/skills/cratis-screenplay-toolchain/references/fast-path.md +7 -0
- package/package/corpus/skills/cratis-screenplay-toolchain/references/traps.md +1 -0
- package/package/corpus/skills/cratis-screenplay-ui-composition/SKILL.md +40 -0
- package/package/corpus/skills/cratis-screenplay-ui-composition/references/response-continuations.md +1 -0
- package/package/corpus/skills/cratis-screenplay-ui-composition/references/worked-flow.md +1 -0
- package/package/corpus/skills/cratis-stage-rendering-and-sandbox/references/admission.md +3 -2
- package/package/corpus/skills/csharp-refactoring/LICENSE +23 -0
- package/package/corpus/skills/csharp-refactoring/NOTICE +22 -0
- package/package/corpus/skills/csharp-refactoring/SKILL.md +175 -0
- package/package/corpus/skills/csharp-refactoring/references/operation-catalog.md +37 -0
- package/package/corpus/skills/directory-build-organization/LICENSE +23 -0
- package/package/corpus/skills/directory-build-organization/NOTICE +21 -0
- package/package/corpus/skills/directory-build-organization/SKILL.md +205 -0
- package/package/corpus/skills/directory-build-organization/references/common-patterns.md +56 -0
- package/package/corpus/skills/directory-build-organization/references/multi-level-examples.md +167 -0
- package/package/corpus/skills/directory-build-organization/references/targetframework-props-pitfall.md +54 -0
- package/package/corpus/skills/dotnet-aot-compat/LICENSE +23 -0
- package/package/corpus/skills/dotnet-aot-compat/NOTICE +18 -0
- package/package/corpus/skills/dotnet-aot-compat/SKILL.md +265 -0
- package/package/corpus/skills/dotnet-aot-compat/references/polyfills.md +43 -0
- package/package/corpus/skills/dotnet-trace-collect/LICENSE +23 -0
- package/package/corpus/skills/dotnet-trace-collect/NOTICE +65 -0
- package/package/corpus/skills/dotnet-trace-collect/SKILL.md +116 -0
- package/package/corpus/skills/dotnet-trace-collect/references/dotnet-monitor.md +97 -0
- package/package/corpus/skills/dotnet-trace-collect/references/dotnet-trace-collect-linux.md +84 -0
- package/package/corpus/skills/dotnet-trace-collect/references/dotnet-trace-collect.md +91 -0
- package/package/corpus/skills/dotnet-trace-collect/references/perfcollect.md +200 -0
- package/package/corpus/skills/dotnet-trace-collect/references/perfview.md +141 -0
- package/package/corpus/skills/dotnet-trace-collect/references/tool-selection.md +160 -0
- package/package/corpus/skills/dump-collect/LICENSE +23 -0
- package/package/corpus/skills/dump-collect/NOTICE +40 -0
- package/package/corpus/skills/dump-collect/SKILL.md +97 -0
- package/package/corpus/skills/dump-collect/references/container-dumps.md +412 -0
- package/package/corpus/skills/dump-collect/references/coreclr-dumps.md +108 -0
- package/package/corpus/skills/dump-collect/references/nativeaot-dumps.md +280 -0
- package/package/corpus/skills/eliminate-redundant-restores/LICENSE +23 -0
- package/package/corpus/skills/eliminate-redundant-restores/NOTICE +19 -0
- package/package/corpus/skills/eliminate-redundant-restores/SKILL.md +112 -0
- package/package/corpus/skills/extension-points/LICENSE +23 -0
- package/package/corpus/skills/extension-points/NOTICE +18 -0
- package/package/corpus/skills/extension-points/SKILL.md +248 -0
- package/package/corpus/skills/filter-syntax/LICENSE +23 -0
- package/package/corpus/skills/filter-syntax/NOTICE +17 -0
- package/package/corpus/skills/filter-syntax/SKILL.md +179 -0
- package/package/corpus/skills/including-generated-files/LICENSE +23 -0
- package/package/corpus/skills/including-generated-files/NOTICE +17 -0
- package/package/corpus/skills/including-generated-files/SKILL.md +139 -0
- package/package/corpus/skills/microbenchmarking/LICENSE +23 -0
- package/package/corpus/skills/microbenchmarking/NOTICE +36 -0
- package/package/corpus/skills/microbenchmarking/SKILL.md +131 -0
- package/package/corpus/skills/microbenchmarking/references/bdn-internals-and-tuning.md +143 -0
- package/package/corpus/skills/microbenchmarking/references/comparison-strategies.md +200 -0
- package/package/corpus/skills/microbenchmarking/references/diagnosers-and-exporters.md +150 -0
- package/package/corpus/skills/microbenchmarking/references/project-setup-and-running.md +189 -0
- package/package/corpus/skills/microbenchmarking/references/writing-benchmarks.md +310 -0
- package/package/corpus/skills/msbuild/LICENSE +23 -0
- package/package/corpus/skills/msbuild/NOTICE +55 -0
- package/package/corpus/skills/msbuild/SKILL.md +117 -0
- package/package/corpus/skills/msbuild/references/antipatterns/additional-antipatterns.md +291 -0
- package/package/corpus/skills/msbuild/references/antipatterns/incremental-build-inputs-outputs.md +57 -0
- package/package/corpus/skills/msbuild/references/antipatterns/private-assets.md +29 -0
- package/package/corpus/skills/msbuild/references/binlog-failure-analysis.md +85 -0
- package/package/corpus/skills/msbuild/references/binlog-generation.md +69 -0
- package/package/corpus/skills/msbuild/references/build-perf-baseline.md +493 -0
- package/package/corpus/skills/msbuild/references/build-perf-diagnostics.md +268 -0
- package/package/corpus/skills/msbuild/references/copy-to-output-directory.md +183 -0
- package/package/corpus/skills/msbuild/references/eval-performance.md +190 -0
- package/package/corpus/skills/msbuild/references/extension-points.md +281 -0
- package/package/corpus/skills/msbuild/references/incremental-build.md +365 -0
- package/package/corpus/skills/msbuild/references/msbuild-antipatterns.md +401 -0
- package/package/corpus/skills/orleans/LICENSE +21 -0
- package/package/corpus/skills/orleans/NOTICE +64 -0
- package/package/corpus/skills/orleans/SKILL.md +140 -0
- package/package/corpus/skills/orleans/references/anti-patterns.md +660 -0
- package/package/corpus/skills/orleans/references/configuration-api.md +933 -0
- package/package/corpus/skills/orleans/references/examples.md +66 -0
- package/package/corpus/skills/orleans/references/grain-api.md +784 -0
- package/package/corpus/skills/orleans/references/grains.md +75 -0
- package/package/corpus/skills/orleans/references/hosting.md +71 -0
- package/package/corpus/skills/orleans/references/implementation.md +591 -0
- package/package/corpus/skills/orleans/references/official-docs-index.md +250 -0
- package/package/corpus/skills/orleans/references/patterns.md +811 -0
- package/package/corpus/skills/orleans/references/persistence-api.md +495 -0
- package/package/corpus/skills/orleans/references/primitive-selection.md +35 -0
- package/package/corpus/skills/orleans/references/scheduling-and-services.md +295 -0
- package/package/corpus/skills/orleans/references/serialization-api.md +299 -0
- package/package/corpus/skills/orleans/references/streaming-api.md +396 -0
- package/package/corpus/skills/orleans/references/testing-patterns.md +210 -0
- package/package/corpus/skills/platform-detection/LICENSE +23 -0
- package/package/corpus/skills/platform-detection/NOTICE +17 -0
- package/package/corpus/skills/platform-detection/SKILL.md +230 -0
- package/package/corpus/skills/platform-detection/references/command-mode.md +27 -0
- package/package/corpus/skills/profiling/LICENSE +21 -0
- package/package/corpus/skills/profiling/NOTICE +50 -0
- package/package/corpus/skills/profiling/SKILL.md +144 -0
- package/package/corpus/skills/profiling/references/commands.md +439 -0
- package/package/corpus/skills/profiling/references/patterns.md +586 -0
- package/package/corpus/skills/profiling/references/profiling.md +190 -0
- package/package/corpus/skills/run-tests/LICENSE +23 -0
- package/package/corpus/skills/run-tests/NOTICE +19 -0
- package/package/corpus/skills/run-tests/SKILL.md +305 -0
- package/package/corpus/skills/target-authoring/LICENSE +23 -0
- package/package/corpus/skills/target-authoring/NOTICE +17 -0
- package/package/corpus/skills/target-authoring/SKILL.md +165 -0
- package/package/corpus/skills/vectorization/LICENSE +23 -0
- package/package/corpus/skills/vectorization/NOTICE +17 -0
- package/package/corpus/skills/vectorization/SKILL.md +189 -0
- package/package/profile-catalog.json +85 -1
- package/package.json +1 -1
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# Multi-level Directory.Build Examples
|
|
2
|
+
|
|
3
|
+
## Contents
|
|
4
|
+
|
|
5
|
+
- Repo-level `Directory.Build.props`
|
|
6
|
+
- `src/Directory.Build.props`
|
|
7
|
+
- `test/Directory.Build.props`
|
|
8
|
+
- Before/After: Centralizing Duplicated Settings
|
|
9
|
+
|
|
10
|
+
Full file examples for a typical multi-level repo layout.
|
|
11
|
+
|
|
12
|
+
## Repo-level `Directory.Build.props`
|
|
13
|
+
|
|
14
|
+
```xml
|
|
15
|
+
<Project>
|
|
16
|
+
|
|
17
|
+
<PropertyGroup>
|
|
18
|
+
<Nullable>enable</Nullable>
|
|
19
|
+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
|
20
|
+
</PropertyGroup>
|
|
21
|
+
|
|
22
|
+
</Project>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## `src/Directory.Build.props`
|
|
26
|
+
|
|
27
|
+
```xml
|
|
28
|
+
<Project>
|
|
29
|
+
|
|
30
|
+
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))"
|
|
31
|
+
Condition="Exists('$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))')" />
|
|
32
|
+
|
|
33
|
+
<PropertyGroup>
|
|
34
|
+
<IsPackable>true</IsPackable>
|
|
35
|
+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
|
36
|
+
</PropertyGroup>
|
|
37
|
+
|
|
38
|
+
</Project>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## `test/Directory.Build.props`
|
|
42
|
+
|
|
43
|
+
```xml
|
|
44
|
+
<Project>
|
|
45
|
+
|
|
46
|
+
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))"
|
|
47
|
+
Condition="Exists('$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))')" />
|
|
48
|
+
|
|
49
|
+
<PropertyGroup>
|
|
50
|
+
<IsPackable>false</IsPackable>
|
|
51
|
+
<NoWarn>$(NoWarn);CS1591</NoWarn>
|
|
52
|
+
</PropertyGroup>
|
|
53
|
+
|
|
54
|
+
<ItemGroup>
|
|
55
|
+
<PackageReference Include="xunit" />
|
|
56
|
+
<PackageReference Include="xunit.runner.visualstudio" />
|
|
57
|
+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
|
58
|
+
<PackageReference Include="NSubstitute" />
|
|
59
|
+
</ItemGroup>
|
|
60
|
+
|
|
61
|
+
</Project>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Before/After: Centralizing Duplicated Settings
|
|
65
|
+
|
|
66
|
+
**Before — duplicated settings in every .csproj:**
|
|
67
|
+
|
|
68
|
+
```xml
|
|
69
|
+
<!-- src/LibA/LibA.csproj -->
|
|
70
|
+
<Project Sdk="Microsoft.NET.Sdk">
|
|
71
|
+
|
|
72
|
+
<PropertyGroup>
|
|
73
|
+
<TargetFramework>net8.0</TargetFramework>
|
|
74
|
+
<Nullable>enable</Nullable>
|
|
75
|
+
<ImplicitUsings>enable</ImplicitUsings>
|
|
76
|
+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
|
77
|
+
<Company>Contoso</Company>
|
|
78
|
+
<Authors>Contoso Engineering</Authors>
|
|
79
|
+
</PropertyGroup>
|
|
80
|
+
|
|
81
|
+
<ItemGroup>
|
|
82
|
+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
|
|
83
|
+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
|
84
|
+
</ItemGroup>
|
|
85
|
+
|
|
86
|
+
</Project>
|
|
87
|
+
|
|
88
|
+
<!-- src/LibB/LibB.csproj — same boilerplate repeated -->
|
|
89
|
+
<Project Sdk="Microsoft.NET.Sdk">
|
|
90
|
+
|
|
91
|
+
<PropertyGroup>
|
|
92
|
+
<TargetFramework>net8.0</TargetFramework>
|
|
93
|
+
<Nullable>enable</Nullable>
|
|
94
|
+
<ImplicitUsings>enable</ImplicitUsings>
|
|
95
|
+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
|
96
|
+
<Company>Contoso</Company>
|
|
97
|
+
<Authors>Contoso Engineering</Authors>
|
|
98
|
+
</PropertyGroup>
|
|
99
|
+
|
|
100
|
+
<ItemGroup>
|
|
101
|
+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
|
|
102
|
+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
|
103
|
+
</ItemGroup>
|
|
104
|
+
|
|
105
|
+
</Project>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**After — centralized with Directory.Build files:**
|
|
109
|
+
|
|
110
|
+
```xml
|
|
111
|
+
<!-- Directory.Build.props -->
|
|
112
|
+
<Project>
|
|
113
|
+
|
|
114
|
+
<PropertyGroup>
|
|
115
|
+
<Nullable>enable</Nullable>
|
|
116
|
+
<ImplicitUsings>enable</ImplicitUsings>
|
|
117
|
+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
|
118
|
+
<Company>Contoso</Company>
|
|
119
|
+
<Authors>Contoso Engineering</Authors>
|
|
120
|
+
</PropertyGroup>
|
|
121
|
+
|
|
122
|
+
</Project>
|
|
123
|
+
|
|
124
|
+
<!-- Directory.Packages.props -->
|
|
125
|
+
<Project>
|
|
126
|
+
|
|
127
|
+
<PropertyGroup>
|
|
128
|
+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
|
129
|
+
</PropertyGroup>
|
|
130
|
+
|
|
131
|
+
<ItemGroup>
|
|
132
|
+
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
|
|
133
|
+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
|
134
|
+
</ItemGroup>
|
|
135
|
+
|
|
136
|
+
<ItemGroup>
|
|
137
|
+
<GlobalPackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
|
|
138
|
+
</ItemGroup>
|
|
139
|
+
|
|
140
|
+
</Project>
|
|
141
|
+
|
|
142
|
+
<!-- src/LibA/LibA.csproj — clean and minimal -->
|
|
143
|
+
<Project Sdk="Microsoft.NET.Sdk">
|
|
144
|
+
|
|
145
|
+
<PropertyGroup>
|
|
146
|
+
<TargetFramework>net8.0</TargetFramework>
|
|
147
|
+
</PropertyGroup>
|
|
148
|
+
|
|
149
|
+
<ItemGroup>
|
|
150
|
+
<PackageReference Include="Newtonsoft.Json" />
|
|
151
|
+
</ItemGroup>
|
|
152
|
+
|
|
153
|
+
</Project>
|
|
154
|
+
|
|
155
|
+
<!-- src/LibB/LibB.csproj — clean and minimal -->
|
|
156
|
+
<Project Sdk="Microsoft.NET.Sdk">
|
|
157
|
+
|
|
158
|
+
<PropertyGroup>
|
|
159
|
+
<TargetFramework>net8.0</TargetFramework>
|
|
160
|
+
</PropertyGroup>
|
|
161
|
+
|
|
162
|
+
<ItemGroup>
|
|
163
|
+
<PackageReference Include="Microsoft.Extensions.Logging" />
|
|
164
|
+
</ItemGroup>
|
|
165
|
+
|
|
166
|
+
</Project>
|
|
167
|
+
```
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# AP-21: Property Conditioned on TargetFramework in .props Files
|
|
2
|
+
|
|
3
|
+
**Smell**: `<PropertyGroup Condition="'$(TargetFramework)' == '...'">` or `<Property Condition="'$(TargetFramework)' == '...'">` in `Directory.Build.props` or any `.props` file imported before the project body.
|
|
4
|
+
|
|
5
|
+
**Why it's bad**: `$(TargetFramework)` is NOT reliably available in `Directory.Build.props` or any `.props` file imported before the project body. It is only set that early for multi-targeting projects, which receive `TargetFramework` as a global property from the outer build. Single-targeting projects (using singular `<TargetFramework>`) set it in the project body, which is evaluated *after* `.props`. This means property conditions on `$(TargetFramework)` in `.props` files silently fail for single-targeting projects — the condition never matches because the property is empty. This applies to both `<PropertyGroup Condition="...">` and individual `<Property Condition="...">` elements.
|
|
6
|
+
|
|
7
|
+
For a detailed explanation of MSBuild's evaluation and execution phases, see [Build process overview](https://learn.microsoft.com/en-us/visualstudio/msbuild/build-process-overview).
|
|
8
|
+
|
|
9
|
+
```xml
|
|
10
|
+
<!-- BAD: In Directory.Build.props — TargetFramework may be empty here -->
|
|
11
|
+
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
|
12
|
+
<DefineConstants>$(DefineConstants);MY_FEATURE</DefineConstants>
|
|
13
|
+
</PropertyGroup>
|
|
14
|
+
|
|
15
|
+
<!-- ALSO BAD: Condition on the property itself has the same problem -->
|
|
16
|
+
<PropertyGroup>
|
|
17
|
+
<DefineConstants Condition="'$(TargetFramework)' == 'net8.0'">$(DefineConstants);MY_FEATURE</DefineConstants>
|
|
18
|
+
</PropertyGroup>
|
|
19
|
+
|
|
20
|
+
<!-- GOOD: In Directory.Build.targets — TargetFramework is always available -->
|
|
21
|
+
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
|
22
|
+
<DefineConstants>$(DefineConstants);MY_FEATURE</DefineConstants>
|
|
23
|
+
</PropertyGroup>
|
|
24
|
+
|
|
25
|
+
<!-- ALSO GOOD: In the project file itself -->
|
|
26
|
+
<!-- MyProject.csproj -->
|
|
27
|
+
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
|
28
|
+
<DefineConstants>$(DefineConstants);MY_FEATURE</DefineConstants>
|
|
29
|
+
</PropertyGroup>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**⚠️ Item and Target conditions are NOT affected.** This restriction applies ONLY to property conditions (`<PropertyGroup Condition="...">` and `<Property Condition="...">`). Item conditions (`<ItemGroup Condition="...">`) and Target conditions in `.props` files are SAFE because items and targets evaluate after all properties (including those set in the project body) have been evaluated. This includes `PackageVersion` items in `Directory.Packages.props`, `PackageReference` items in `Directory.Build.props`, and any other item types.
|
|
33
|
+
|
|
34
|
+
**Do NOT flag the following patterns — they are correct:**
|
|
35
|
+
|
|
36
|
+
```xml
|
|
37
|
+
<!-- OK in Directory.Build.props — ItemGroup conditions evaluate late -->
|
|
38
|
+
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
|
|
39
|
+
<PackageReference Include="System.Memory" />
|
|
40
|
+
</ItemGroup>
|
|
41
|
+
|
|
42
|
+
<!-- OK in Directory.Packages.props — PackageVersion items evaluate late -->
|
|
43
|
+
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
|
44
|
+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.11" />
|
|
45
|
+
</ItemGroup>
|
|
46
|
+
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
|
|
47
|
+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
|
|
48
|
+
</ItemGroup>
|
|
49
|
+
|
|
50
|
+
<!-- OK — Individual item conditions also evaluate late -->
|
|
51
|
+
<ItemGroup>
|
|
52
|
+
<PackageReference Include="System.Memory" Condition="'$(TargetFramework)' == 'net472'" />
|
|
53
|
+
</ItemGroup>
|
|
54
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) .NET Foundation and Contributors
|
|
4
|
+
|
|
5
|
+
All rights reserved.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
dotnet-aot-compat
|
|
2
|
+
Copyright (c) .NET Foundation and Contributors
|
|
3
|
+
Licensed under the MIT License. See LICENSE.
|
|
4
|
+
|
|
5
|
+
Upstream source: https://github.com/dotnet/skills/blob/a6bf43ab61967c30919334986515163ab24d7635/plugins/dotnet-upgrade/skills/dotnet-aot-compat/SKILL.md
|
|
6
|
+
|
|
7
|
+
Changes made by Cratis:
|
|
8
|
+
|
|
9
|
+
LICENSE:
|
|
10
|
+
- Copy upstream MIT license and copyright verbatim.
|
|
11
|
+
|
|
12
|
+
SKILL.md:
|
|
13
|
+
- Normalize frontmatter serialization, add MIT license where absent; preserve name and description.
|
|
14
|
+
- Add marked modification/source comment and applicability/Cratis precedence line with the skill-specific safety, ownership or compatibility caveat.
|
|
15
|
+
- Rewrite the description to the Cratis shape (what it does, Use when, Not for) and drop references to skills that are not part of this corpus.
|
|
16
|
+
- Remove the invalid [JsonSerializerContext] attribute from the source-generated JSON example; the context is a partial class deriving from JsonSerializerContext.
|
|
17
|
+
|
|
18
|
+
NOTICE: Add this attribution, change list, and any download/execution disclosure.
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: dotnet-aot-compat
|
|
3
|
+
description: 'Makes .NET projects compatible with Native AOT and trimming by resolving IL trim and AOT analyzer warnings. Use when making a project AOT-compatible, fixing trimming warnings, resolving IL2026, IL2067, IL2070, IL2072, or IL3050, adding DynamicallyAccessedMembers annotations, or enabling IsAotCompatible. Not for publishing native AOT binaries, optimizing binary size, replacing reflection-heavy libraries, or .NET Framework projects.'
|
|
4
|
+
license: MIT
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
<!-- Modified by Cratis from dotnet's original (MIT): frontmatter, applicability, navigation and the changes listed in NOTICE. Upstream: https://github.com/dotnet/skills (plugins/dotnet-upgrade/skills/dotnet-aot-compat/SKILL.md; see NOTICE) -->
|
|
8
|
+
|
|
9
|
+
# dotnet-aot-compat
|
|
10
|
+
|
|
11
|
+
Cratis rules in `.cratis/ai/rules/` (including `csharp.md`, `specs.csharp.md`, and `orleans.md`) and the `cratis-*` skills win where they differ. Arc host trimming/NativeAOT setup belongs to **cratis-arc-hosting**.
|
|
12
|
+
|
|
13
|
+
Make .NET projects compatible with Native AOT and trimming by systematically resolving all IL trim/AOT analyzer warnings.
|
|
14
|
+
|
|
15
|
+
## When to Use This Skill
|
|
16
|
+
|
|
17
|
+
- **"Make this project AOT-compatible"**
|
|
18
|
+
- **"Fix trimming warnings"** or **"fix IL warnings"**
|
|
19
|
+
- **"Resolve IL2070 / IL2067 / IL2072 / IL2026 / IL3050 warnings"**
|
|
20
|
+
- **"Add DynamicallyAccessedMembers annotations"**
|
|
21
|
+
- **"Enable IsAotCompatible in my .csproj"**
|
|
22
|
+
- **"My project has trim analyzer warnings after upgrading to net8.0"**
|
|
23
|
+
- **"Annotate reflection code for the trimmer"**
|
|
24
|
+
|
|
25
|
+
## When Not to Use This Skill
|
|
26
|
+
|
|
27
|
+
Do not use this skill when the project exclusively targets .NET Framework (net4x), which does not support the trim/AOT analyzers.
|
|
28
|
+
|
|
29
|
+
## Prerequisites
|
|
30
|
+
|
|
31
|
+
An existing .NET project targeting net8.0 or later (or multi-targeting with at least one net8.0+ TFM) and the corresponding .NET SDK installed.
|
|
32
|
+
|
|
33
|
+
## Background: What AOT Compatibility Means
|
|
34
|
+
|
|
35
|
+
Native AOT and the IL trimmer perform static analysis to determine what code is reachable. Reflection can break this analysis because the trimmer can't see what types/members are accessed at runtime. The `IsAotCompatible` property enables analyzers that flag these issues as build warnings (ILXXXX codes).
|
|
36
|
+
|
|
37
|
+
## Critical Rules
|
|
38
|
+
|
|
39
|
+
### ❌ Never suppress warnings incorrectly
|
|
40
|
+
|
|
41
|
+
- **NEVER** use `#pragma warning disable` for IL warnings. It hides warnings from the Roslyn analyzer at build time, but the IL linker and AOT compiler still see the issue. The code will fail at trim/publish time.
|
|
42
|
+
- **NEVER** use `[UnconditionalSuppressMessage]`. It tells both the analyzer AND the linker to ignore the warning, meaning the trimmer cannot verify safety. Raising an error at build time is always preferable to hiding the issue and having it silently break at runtime.
|
|
43
|
+
|
|
44
|
+
### 💡 Preferred approaches
|
|
45
|
+
|
|
46
|
+
- **Prefer** `[DynamicallyAccessedMembers]` annotations to flow type information through the call chain.
|
|
47
|
+
- **Prefer** refactoring to eliminate patterns that break annotation flow (e.g., boxing `Type` through `object[]`).
|
|
48
|
+
- **Use** `[RequiresUnreferencedCode]` / `[RequiresDynamicCode]` / `[RequiresAssemblyFiles]` to mark methods as fundamentally incompatible with trimming, propagating the requirement to callers. This surfaces the issue clearly rather than hiding it — callers must explicitly acknowledge the incompatibility.
|
|
49
|
+
|
|
50
|
+
### Annotation flow is key
|
|
51
|
+
|
|
52
|
+
The trimmer tracks `[DynamicallyAccessedMembers]` annotations through assignments, parameter passing, and return values. If this flow is broken (e.g., by boxing a `Type` into `object`, storing in an untyped collection, or casting through interfaces), the trimmer loses track and warns. The fix is to preserve the flow, not suppress the warning.
|
|
53
|
+
|
|
54
|
+
## Step-by-Step Procedure
|
|
55
|
+
|
|
56
|
+
> **Do not explore the codebase up-front.** The build warnings tell you exactly which files and lines need changes. Follow a tight loop: **build → pick a warning → open that file at that line → apply the fix recipe → rebuild**. Reading or analyzing source files beyond what a specific warning points you to is wasted effort and leads to timeouts. Let the compiler guide you.
|
|
57
|
+
>
|
|
58
|
+
> ❌ Do NOT run `find`, `ls`, or `grep` to understand the project structure before building. Do NOT read README, docs, or architecture files. Your first action should be Step 1 (enable AOT analysis), then build.
|
|
59
|
+
|
|
60
|
+
### Step 1: Enable AOT analysis in the .csproj
|
|
61
|
+
|
|
62
|
+
Add `IsAotCompatible`. If the project doesn't exclusively target net8.0+, add a TFM condition (AOT analysis requires net8.0+):
|
|
63
|
+
|
|
64
|
+
```xml
|
|
65
|
+
<PropertyGroup>
|
|
66
|
+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
|
|
67
|
+
</PropertyGroup>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
This automatically sets `EnableTrimAnalyzer=true` and `EnableAotAnalyzer=true` for compatible TFMs. For multi-targeting projects (e.g., `netstandard2.0;net8.0`), the condition ensures no `NETSDK1210` warnings on older TFMs.
|
|
71
|
+
|
|
72
|
+
### Step 2: Build and collect warnings
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
dotnet build <project.csproj> -f <net8.0-or-later-tfm> --no-incremental 2>&1 | grep 'IL[0-9]\{4\}'
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Sort and deduplicate. Common warning codes:
|
|
79
|
+
- **IL2070**: Reflection call on a `Type` parameter missing `[DynamicallyAccessedMembers]`
|
|
80
|
+
- **IL2067**: Passing an unannotated `Type` to a method expecting `[DynamicallyAccessedMembers]`
|
|
81
|
+
- **IL2072**: Return value or extracted value missing annotation (often from unboxing)
|
|
82
|
+
- **IL2057**: `Type.GetType(string)` with a non-constant argument
|
|
83
|
+
- **IL2026**: Calling a method marked `[RequiresUnreferencedCode]`
|
|
84
|
+
- **IL2050**: P/invoke method with COM marshalling parameters
|
|
85
|
+
- **IL2075**: Return value flows into reflection without annotation
|
|
86
|
+
- **IL2091**: Generic argument missing `[DynamicallyAccessedMembers]` required by constraint
|
|
87
|
+
- **IL3000**: `Assembly.Location` returns empty string in single-file/AOT apps
|
|
88
|
+
- **IL3050**: Calling a method marked `[RequiresDynamicCode]`
|
|
89
|
+
|
|
90
|
+
### Step 3: Triage warnings by code (do NOT read every file)
|
|
91
|
+
|
|
92
|
+
Group the warnings from Step 2 by warning code and count them. **Do not open individual files yet.** Identify the top 1-2 patterns by count — these drive your fix strategy:
|
|
93
|
+
|
|
94
|
+
| Pattern | Typical fix |
|
|
95
|
+
|---------|-------------|
|
|
96
|
+
| Many IL2026 + IL3050 from `JsonSerializer` | **Go to Strategy C immediately** — create a `JsonSerializerContext`, then batch-update all call sites |
|
|
97
|
+
| IL2070/IL2087 on `Type` parameters | Add `[DynamicallyAccessedMembers]` to the innermost method, then cascade outward |
|
|
98
|
+
| IL2067 passing unannotated `Type` | Annotate the parameter at the source |
|
|
99
|
+
|
|
100
|
+
**In most real projects, IL2026/IL3050 from JsonSerializer dominate.** Start with Strategy C unless the warning breakdown clearly shows otherwise. After the batch JSON fix, handle remaining warnings with Strategies A–B. Only use Strategy D as a last resort.
|
|
101
|
+
|
|
102
|
+
### Step 4: Fix warnings iteratively (innermost first)
|
|
103
|
+
|
|
104
|
+
Work from the **innermost** reflection call outward. Each fix may cascade new warnings to callers.
|
|
105
|
+
|
|
106
|
+
**Stay warning-driven.** For each warning, open only the file and line the compiler reported, identify the pattern, apply the matching fix recipe below, and move on. Do not scan the codebase for similar patterns or try to understand the full architecture — fix what the compiler tells you, rebuild, and let new warnings guide the next change. Fix a small batch of warnings (5-10), then rebuild immediately to check progress.
|
|
107
|
+
|
|
108
|
+
**Use sub-agents when available.** If you can launch sub-agents (e.g., via a `task` tool), dispatch **multiple sub-agents in parallel** to edit different files simultaneously. Keep the main loop focused on building, parsing warnings, and dispatching — delegate actual file edits to sub-agents. For batch JSON updates, give each sub-agent 5-10 files to update in one prompt. **After 2 build-fix cycles, dispatch all remaining file edits to sub-agents in parallel — do not continue fixing files sequentially.** Example:
|
|
109
|
+
|
|
110
|
+
> Update these files to use source-generated JSON: `src/Models/Resource.Serialization.cs`, `src/Models/Identity.Serialization.cs`, `src/Models/Plan.Serialization.cs`. In each file, replace `JsonSerializer.Serialize(writer, value)` with `JsonSerializer.Serialize(writer, value, MyProjectJsonContext.Default.TypeName)` and `JsonSerializer.Deserialize<T>(ref reader)` with `JsonSerializer.Deserialize(ref reader, MyProjectJsonContext.Default.TypeName)`. Only edit the JsonSerializer call sites.
|
|
111
|
+
|
|
112
|
+
#### Strategy A: Add `[DynamicallyAccessedMembers]` (preferred)
|
|
113
|
+
|
|
114
|
+
When a method uses reflection on a `Type` parameter, annotate the parameter to tell the trimmer what members are needed:
|
|
115
|
+
|
|
116
|
+
```csharp
|
|
117
|
+
using System.Diagnostics.CodeAnalysis;
|
|
118
|
+
|
|
119
|
+
// Before (warns IL2070):
|
|
120
|
+
void Process(Type t) {
|
|
121
|
+
var method = t.GetMethod("Foo"); // trimmer can't verify
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// After (clean):
|
|
125
|
+
void Process([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type t) {
|
|
126
|
+
var method = t.GetMethod("Foo"); // trimmer preserves public methods
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
When you annotate a parameter, **all callers** must now pass properly annotated types. This cascades outward — follow each caller and annotate or refactor as needed. **The caller's annotation must include at least the same member types as the callee's.** If the callee requires `PublicConstructors | NonPublicConstructors`, the caller must specify the same or a superset — using only `NonPublicConstructors` will produce IL2091.
|
|
131
|
+
|
|
132
|
+
#### Strategy B: Refactor to preserve annotation flow
|
|
133
|
+
|
|
134
|
+
When annotation flow is broken by boxing (storing `Type` in `object`, `object[]`, or untyped collections), **refactor** to pass the `Type` directly:
|
|
135
|
+
|
|
136
|
+
```csharp
|
|
137
|
+
// BROKEN: Type boxed into object[], annotation lost
|
|
138
|
+
void Process(object[] args) {
|
|
139
|
+
Type t = (Type)args[0]; // IL2072: annotation lost through boxing
|
|
140
|
+
Evaluate(t, ...);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// FIXED: Pass Type as a separate, annotated parameter
|
|
144
|
+
void Process(
|
|
145
|
+
object[] args,
|
|
146
|
+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type calleeType,
|
|
147
|
+
...) {
|
|
148
|
+
Evaluate(calleeType, ...); // annotation flows cleanly
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Common patterns that break flow and how to fix them:
|
|
153
|
+
- **`object[]` parameter bags**: Extract the `Type` into a dedicated annotated parameter
|
|
154
|
+
- **Dictionary/List storage**: Use a typed field with annotation instead
|
|
155
|
+
- **Interface indirection**: Add annotation to the interface method's parameter
|
|
156
|
+
- **Property with boxing getter**: Annotate the property's return type
|
|
157
|
+
|
|
158
|
+
#### Strategy C: Source-generated JSON serialization (batch fix)
|
|
159
|
+
|
|
160
|
+
When most warnings are IL2026/IL3050 from `JsonSerializer.Serialize`/`Deserialize`, this is a single mechanical fix applied in bulk:
|
|
161
|
+
|
|
162
|
+
1. **Collect affected types** — grep for all `JsonSerializer.Serialize` and `JsonSerializer.Deserialize` call sites. Extract the type being serialized (the `<T>` in `Deserialize<T>`, or the runtime type of the object in `Serialize`).
|
|
163
|
+
|
|
164
|
+
2. **Create one `JsonSerializerContext`** with `[JsonSerializable]` for every type found. **Skip types from external packages** (e.g., `ResponseError` from `Azure.Core`) — they won't source-generate for types you don't own. Handle external types separately via Gotcha #1 below.
|
|
165
|
+
|
|
166
|
+
```csharp
|
|
167
|
+
[JsonSerializable(typeof(ManagedServiceIdentity))]
|
|
168
|
+
[JsonSerializable(typeof(SystemData))]
|
|
169
|
+
// ... one attribute per type YOU OWN
|
|
170
|
+
// Do NOT add types from external packages (e.g., ResponseError)
|
|
171
|
+
internal partial class MyProjectJsonContext : JsonSerializerContext { }
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
3. **Batch-update all call sites** — do not read each file individually. Apply the pattern mechanically:
|
|
175
|
+
- `JsonSerializer.Serialize(obj)` → `JsonSerializer.Serialize(obj, MyProjectJsonContext.Default.TypeName)`
|
|
176
|
+
- `JsonSerializer.Deserialize<T>(json)` → `JsonSerializer.Deserialize(json, MyProjectJsonContext.Default.TypeName)`
|
|
177
|
+
|
|
178
|
+
Find and update all call sites in one pass:
|
|
179
|
+
```bash
|
|
180
|
+
# Find all files with JsonSerializer calls
|
|
181
|
+
grep -rl 'JsonSerializer\.\(Serialize\|Deserialize\)' src/ --include='*.cs'
|
|
182
|
+
```
|
|
183
|
+
Then use sequential `edit` calls to apply the same transformation to every matching file. **Do not use `sed` for C# code** — generics like `Deserialize<T>()` have angle brackets and nested parentheses that sed will mangle.
|
|
184
|
+
|
|
185
|
+
4. **Build once** to verify. Remaining warnings will be non-serialization issues — handle those with Strategies A–B or D.
|
|
186
|
+
|
|
187
|
+
#### Strategy D: `[RequiresUnreferencedCode]` (last resort)
|
|
188
|
+
|
|
189
|
+
When a method fundamentally requires arbitrary reflection that cannot be statically described:
|
|
190
|
+
|
|
191
|
+
```csharp
|
|
192
|
+
[RequiresUnreferencedCode("Loads plugins by name using Assembly.Load")]
|
|
193
|
+
public void LoadPlugin(string assemblyName) {
|
|
194
|
+
var asm = Assembly.Load(assemblyName);
|
|
195
|
+
// ...
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
This propagates to callers — they must also be annotated with `[RequiresUnreferencedCode]`. Use sparingly; it marks the entire call chain as trim-incompatible.
|
|
200
|
+
|
|
201
|
+
### Step 5: Rebuild and repeat
|
|
202
|
+
|
|
203
|
+
After each small batch of fixes (5-10 warnings), rebuild with `--no-incremental` and check for new warnings. **Do not attempt to fix all warnings before rebuilding** — frequent rebuilds catch mistakes early and reveal cascading warnings. Fixes cascade — annotating an inner method may surface warnings in its callers. Repeat until `0 Warning(s)`.
|
|
204
|
+
|
|
205
|
+
### Step 6: Validate all TFMs
|
|
206
|
+
|
|
207
|
+
Build all target frameworks to ensure:
|
|
208
|
+
- **0 IL warnings** on net8.0+ TFMs
|
|
209
|
+
- **No NETSDK1210 warnings** (the `IsAotCompatible` condition handles this)
|
|
210
|
+
- **Clean builds** on older TFMs (netstandard2.0, net472, etc.)
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
dotnet build <project.csproj> # builds all TFMs
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Stop Signals
|
|
217
|
+
|
|
218
|
+
- **Do not analyze more than 2-3 representative files per warning pattern.** After identifying the fix for a pattern, apply it to all matching files without reading each one first.
|
|
219
|
+
- **Start fixing after one build.** Do not do a second analysis pass — begin implementing fixes for the most common warning pattern immediately after Step 3 triage.
|
|
220
|
+
- Stop after achieving **0 IL warnings** for net8.0+ TFMs. Don't optimize or refactor already-clean annotations.
|
|
221
|
+
- If a warning requires **architectural refactoring** beyond annotation flow fixes (e.g., replacing an entire serialization layer), document it and stop — don't rewrite large subsystems.
|
|
222
|
+
- Limit to **3 build-fix iterations** per warning. If annotation flow doesn't resolve it after 3 attempts, escalate to `[RequiresUnreferencedCode]`.
|
|
223
|
+
- Don't chase warnings in **third-party dependencies** you can't modify. Note them and move on.
|
|
224
|
+
- If the user asked a scoped question (e.g., "fix warnings in this file"), don't expand to the entire project.
|
|
225
|
+
|
|
226
|
+
## Polyfills for Older TFMs
|
|
227
|
+
|
|
228
|
+
For multi-targeting projects that include netstandard2.0 or net472, you need polyfills for `DynamicallyAccessedMembersAttribute` and related types. See [references/polyfills.md](references/polyfills.md).
|
|
229
|
+
|
|
230
|
+
## Common Gotchas
|
|
231
|
+
|
|
232
|
+
1. **External types without AOT-safe serialization**: When a type comes from a dependency you can't modify (e.g., `ResponseError` from `Azure.Core`) and it lacks a source-generated serializer, `Options.GetConverter<T>()` is reflection-based and will produce IL warnings. First check if the type implements `IJsonModel<T>` (common in Azure SDK) — if so, bypass `JsonSerializer` entirely:
|
|
233
|
+
|
|
234
|
+
```csharp
|
|
235
|
+
// Before (IL2026 — JsonSerializer uses reflection):
|
|
236
|
+
JsonSerializer.Serialize(writer, errorValue);
|
|
237
|
+
|
|
238
|
+
// After (AOT-safe — uses IJsonModel directly):
|
|
239
|
+
((IJsonModel<ResponseError>)errorValue).Write(writer, ModelReaderWriterOptions.Json);
|
|
240
|
+
|
|
241
|
+
// For deserialization:
|
|
242
|
+
var error = ((IJsonModel<ResponseError>)new ResponseError()).Create(ref reader, ModelReaderWriterOptions.Json);
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Do **not** add the external type to your `JsonSerializerContext` — it won't source-generate for types you don't own. If the type doesn't implement `IJsonModel<T>`, write a custom `JsonConverter<T>` with manual `Utf8JsonReader`/`Utf8JsonWriter` logic and register it via `[JsonSourceGenerationOptions]` on your context.
|
|
246
|
+
|
|
247
|
+
2. **Serialization libraries**: Most reflection-based serializers (e.g., `Newtonsoft.Json`, `XmlSerializer`) are not AOT-compatible. Migrate to a source-generation-based serializer such as `System.Text.Json` with a `JsonSerializerContext`. If migration is not feasible, mark the serialization call site with `[RequiresUnreferencedCode]`.
|
|
248
|
+
|
|
249
|
+
3. **Shared projects / projitems**: When source is shared between multiple projects via `<Import>`, annotations added to shared code affect ALL consuming projects. Verify that all consumers still build cleanly.
|
|
250
|
+
|
|
251
|
+
## References
|
|
252
|
+
|
|
253
|
+
[Limitations](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=windows%2Cnet8#limitations-of-native-aot-deployment)
|
|
254
|
+
[Conceptual: Understanding trimming](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-concepts)
|
|
255
|
+
[How-to: trim compat](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/fixing-warnings)
|
|
256
|
+
|
|
257
|
+
## Checklist
|
|
258
|
+
|
|
259
|
+
- [ ] Added `<IsAotCompatible>` with TFM condition to .csproj
|
|
260
|
+
- [ ] Built with AOT analyzers enabled (net8.0+ TFM)
|
|
261
|
+
- [ ] Fixed all IL warnings via annotations or refactoring
|
|
262
|
+
- [ ] No `#pragma warning disable` or `[UnconditionalSuppressMessage]` used for any IL warning
|
|
263
|
+
- [ ] Polyfills present for older TFMs if needed
|
|
264
|
+
- [ ] All target frameworks build with 0 warnings
|
|
265
|
+
- [ ] Verified shared/linked source doesn't break sibling projects
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Polyfills for Older TFMs
|
|
2
|
+
|
|
3
|
+
`DynamicallyAccessedMembersAttribute` shipped in .NET 5. For projects targeting netstandard2.0 or net472, you need a polyfill. The trimmer recognizes the attribute by name, so a local copy works:
|
|
4
|
+
|
|
5
|
+
```csharp
|
|
6
|
+
#if !NET
|
|
7
|
+
namespace System.Diagnostics.CodeAnalysis
|
|
8
|
+
{
|
|
9
|
+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.ReturnValue |
|
|
10
|
+
AttributeTargets.GenericParameter | AttributeTargets.Parameter |
|
|
11
|
+
AttributeTargets.Property, Inherited = false)]
|
|
12
|
+
internal sealed class DynamicallyAccessedMembersAttribute : Attribute
|
|
13
|
+
{
|
|
14
|
+
public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes memberTypes)
|
|
15
|
+
=> MemberTypes = memberTypes;
|
|
16
|
+
public DynamicallyAccessedMemberTypes MemberTypes { get; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
[Flags]
|
|
20
|
+
internal enum DynamicallyAccessedMemberTypes
|
|
21
|
+
{
|
|
22
|
+
None = 0,
|
|
23
|
+
PublicParameterlessConstructor = 0x0001,
|
|
24
|
+
PublicConstructors = 0x0002 | PublicParameterlessConstructor,
|
|
25
|
+
NonPublicConstructors = 0x0004,
|
|
26
|
+
PublicMethods = 0x0008,
|
|
27
|
+
NonPublicMethods = 0x0010,
|
|
28
|
+
PublicFields = 0x0020,
|
|
29
|
+
NonPublicFields = 0x0040,
|
|
30
|
+
PublicNestedTypes = 0x0080,
|
|
31
|
+
NonPublicNestedTypes = 0x0100,
|
|
32
|
+
PublicProperties = 0x0200,
|
|
33
|
+
NonPublicProperties = 0x0400,
|
|
34
|
+
PublicEvents = 0x0800,
|
|
35
|
+
NonPublicEvents = 0x1000,
|
|
36
|
+
Interfaces = 0x2000,
|
|
37
|
+
All = ~None // Discouraged — prefer specific flags
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
#endif
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Similarly for `RequiresUnreferencedCodeAttribute` and `UnconditionalSuppressMessageAttribute` if needed on older TFMs.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) .NET Foundation and Contributors
|
|
4
|
+
|
|
5
|
+
All rights reserved.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|