Commit a4c10b1
authored
Always generate aliased usings (#12)
Context
The XrmPluginCore.SourceGenerator ships analyzers + code-fix providers that wire up type-safe Pre/Post image handler signatures. When a RegisterStep<TEntity, TService>(...) registration declares an image via WithPreImage/WithPostImage/AddImage but the referenced handler method's signature does not match, two diagnostics/code-fixes come into play:
FixHandlerSignatureCodeFixProvider (XrmPluginCore.SourceGenerator/CodeFixes/FixHandlerSignatureCodeFixProvider.cs) — fixes an existing handler method's parameter list to accept the registered PreImage/PostImage wrapper types. Fires on DiagnosticDescriptors.HandlerSignatureMismatch.Id and DiagnosticDescriptors.HandlerSignatureMismatchError.Id.
CreateHandlerMethodCodeFixProvider (XrmPluginCore.SourceGenerator/CodeFixes/CreateHandlerMethodCodeFixProvider.cs) — creates a missing handler method on the service interface with the correct image parameters. Fires on DiagnosticDescriptors.HandlerMethodNotFound.Id.
Each registration's image wrapper classes (PreImage, PostImage) are generated into an isolated namespace produced by RegisterStepHelper.GetExpectedImageNamespace(...) (XrmPluginCore.SourceGenerator/Helpers/RegisterStepHelper.cs:17), with the shape:
{PluginNamespace}.PluginRegistrations.{PluginClassName}.{EntityTypeName}{Operation}{Stage}
e.g. Some.Namespace.Plugins.PluginRegistrations.SomePlugin.LeadUpdatePostOperation. The shared marker for these namespaces is the substring .PluginRegistrations. (SyntaxFactoryHelper.IsImageRegistrationNamespace, SyntaxFactoryHelper.cs:207). The alias used is the last namespace segment (GetLastNamespaceSegment, SyntaxFactoryHelper.cs:212), e.g. LeadUpdatePostOperation. Both PreImage and PostImage are simple type names (Constants.PreImageTypeName = "PreImage", Constants.PostImageTypeName = "PostImage"), so two registrations in the same file both expose a PreImage/PostImage type.
The bug: Today the code fixes try to be clever — they only convert to aliased usings when an ambiguity is detected, otherwise they add a plain (non-aliased) using. This is driven by SyntaxFactoryHelper.DetectImageAmbiguity(...) (SyntaxFactoryHelper.cs:118):
In FixHandlerSignatureCodeFixProvider.FixMethodDeclarationsAsync (lines 199–235): ambiguity = DetectImageAmbiguity(...); CreateImageParameterList(hasPreImage, hasPostImage, ambiguity.needsAlias ? ambiguity.alias : null); then if (ambiguity.needsAlias) ConvertToAliasedUsingsAndQualifyRefs(...) else AddUsingDirectiveIfMissing(...).
In CreateHandlerMethodCodeFixProvider.CreateMethodAsync (lines 135–152): same DetectImageAmbiguity / conditional pattern via CreateMethodDeclaration(..., needsAlias ? alias : null) and the same if (needsAlias) ConvertToAliasedUsingsAndQualifyRefs else AddUsingDirectiveIfMissing branch.
When multiple plugins use the same service and more than one of those registrations declares Pre/Post images, the automatic generation of usings and modification of parameters fails, because the plain-using path produces bare PreImage/PostImage references that collide across the multiple image namespaces, and the on-demand conversion logic (ImageAmbiguityRewriter, SyntaxFactoryHelper.cs:234) assumes "there should be exactly one existing plain image using at this point" (SyntaxFactoryHelper.cs:309, the break; after taking the first entry of _typeToExistingAlias). That assumption breaks with multiple image usings.
The decision: Stop trying to convert on demand. Always emit aliased usings and always qualify the image parameter types with the alias. This makes every emitted reference unambiguous regardless of how many same-service registrations exist in the file.
Existing tests assert the old behavior and must be updated. See:
XrmPluginCore.SourceGenerator.Tests/DiagnosticTests/FixHandlerSignatureCodeFixProviderTests.cs — Should_Fix_Signature_And_Add_Using_For_PreImage (line ~61/64), ..._For_PostImage (line ~114/117), ..._For_Both_Images (line ~120), and Should_Avoid_Ambiguous_Usings (line 231).
XrmPluginCore.SourceGenerator.Tests/DiagnosticTests/CreateHandlerMethodCodeFixProviderTests.cs (same directory).1 parent d14d6dc commit a4c10b1
9 files changed
Lines changed: 1311 additions & 110 deletions
File tree
- XrmPluginCore.SourceGenerator.Tests/DiagnosticTests
- XrmPluginCore.SourceGenerator
- CodeFixes
- Helpers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
401 | 401 | | |
402 | 402 | | |
403 | 403 | | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
Lines changed: 70 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
85 | 155 | | |
86 | 156 | | |
87 | 157 | | |
| |||
Lines changed: 191 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | | - | |
59 | | - | |
| 59 | + | |
| 60 | + | |
60 | 61 | | |
61 | | - | |
62 | | - | |
| 62 | + | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
110 | | - | |
111 | | - | |
| 111 | + | |
| 112 | + | |
112 | 113 | | |
113 | | - | |
114 | | - | |
| 114 | + | |
| 115 | + | |
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
| |||
165 | 166 | | |
166 | 167 | | |
167 | 168 | | |
168 | | - | |
| 169 | + | |
169 | 170 | | |
170 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
171 | 174 | | |
172 | 175 | | |
173 | 176 | | |
| |||
211 | 214 | | |
212 | 215 | | |
213 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
214 | 229 | | |
215 | 230 | | |
216 | 231 | | |
217 | 232 | | |
218 | 233 | | |
219 | | - | |
| 234 | + | |
220 | 235 | | |
221 | 236 | | |
222 | | - | |
| 237 | + | |
223 | 238 | | |
224 | 239 | | |
225 | | - | |
| 240 | + | |
226 | 241 | | |
227 | | - | |
| 242 | + | |
228 | 243 | | |
229 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
230 | 402 | | |
231 | 403 | | |
232 | 404 | | |
| |||
244 | 416 | | |
245 | 417 | | |
246 | 418 | | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
247 | 424 | | |
0 commit comments