fix: Hot reload patches types in a namespace with a keyword-named segment such as @event

Merged
HA
7d ago
+551-35

Summary

A type declared in a namespace with a keyword-named segment (for example namespace Game.@event) failed hot reload:

Type '<Namespace>.@event.<Host>' was not found in assembly '<Assembly>'.

CecilTypeNames.ToMetadataName built the namespace part with INamespaceSymbol.ToDisplayString(), which escapes keyword identifiers. Compiled metadata names the namespace event, so the host type lookup missed.

Before / After

  • Before: editing a method, a property getter, or a nested type's method of a type in ...@event gives a Failed row with the message above.
  • After: the rows are Patched and the edited values run; RevertAll restores the compiled values.

Changes

  • The segment join that #3597 added privately to ConstDriftCollector moved to CecilTypeNames.ToNamespaceMetadataName; both CecilTypeNames.ToMetadataName (metadata names, / nesting) and ConstDriftCollector.ToReflectionMetadataName (reflection names, + nesting) use it. Every metadata or reflection name derived from ToMetadataName (patch entry host types, method keys, introduced-type lookups, .Replace('/', '+') reflection names) is fixed by the same change.
  • New end-to-end tests with a fixture in a new file. HotReloadKeywordNamespaceE2ETests: method body, property getter, call to an added method, nested type's method, const drift warning, and compiled property kind-change warning. HotReloadKeywordNamespaceIntroducedTypeE2ETests: a type introduced into the namespace by one reload and body-edited by the next.

Namespace and type display strings checked

SiteFormResult
CecilTypeNames.ToMetadataNamemetadata namefixed
OrdinaryMethodShimTypes / PropertyGetterEmitter shim namespaceC# source (namespace declaration, global:: qualified calls)unchanged: the escaped @event is required to parse; covered by the getter and added-method tests
ShimMethodEmitter.IsUnityEngineMonoBehaviourDerivedcomparison with "UnityEngine"unaffected
AccessorPlan.BuildMemberKey, AccessibilityRules twin map, BaseCallRules, ExplicitImplementationMatcher, EventAccessorRules event typessymbol-to-symbol keys in one display formatunaffected
ImplicitCallTargets.Implements, BaseCallRules / EventAccessorRules attribute checks, UnpassedInternalAccessorLookupcomparison with System.* constantsunaffected
UnpassedAwaitCallLookup, ImplicitCallTargets, EventAccessorShimRewrite, NestedTypeNameQualifierC# source (FullyQualifiedFormat)unaffected
PlannedAddedMemberNames.CollectCompiledEnumMembersmatched against CS0117 compiler messages, which use the same escaped displayunaffected
warning / reason texts (ConstDriftCollector, CompiledMemberKindChangeWarnings, ExtensionClassNameBind, EventAccessorRules, MethodTransformDecider, ...)display textunaffected

Syntax keys

The kind-change warning keys a removed compiled property by the reflection name of its type (built from symbol names, namespace event), and the drift check strips the matching snapshot declaration by a key WorkerSyntaxIndex built from syntax, which kept @event / @class. The keys never met, so the removal also raised the file-level "Edits outside method bodies" warning. WorkerSyntaxIndex now builds the type part of every syntax key from unescaped names (namespace segment and type identifier ValueText).

Comparisons of syntax keys with keys from another source, checked:

SiteOther sideResult
OutsideMethodBodyDriftChecker / StripHandledMemberDeclarationsRewriter vs CompiledMemberKindChangeWarnings kind-change keysConstDriftCollector.ToReflectionMetadataName + compiled member namethe mismatch; fixed
RetainedDeclarationStage retained type names, OrdinaryMethodShimTypes added type keyWorkerSyntaxIndex builderssame builder on both sides
RetainedBodyEditHome changed method / getter keysIntroducedTypeDeclarationMemberIndex, built with WorkerSyntaxIndexsame builder
PartialTypeParts / PartialTypePartLoader needed and changed part namesWorkerSyntaxIndex builderssame builder
RemovedMemberCollector, AddedFieldCatalog, AddedMethodCatalog, UnsupportedMemberSkipCollector, AddedPropertyClassifier, WorkerSyntaxMemberMaps snapshot mapsWorkerSyntaxIndex builderssame builder

None relies on the escaped form.

Member identifiers had the same split: property keys and the field keys built from declarators kept the @ of a keyword-named member (@checked, @event) while the kind-change and added-field keys use symbol names. Removing such a property, or adding such a field, raised a spurious "Edits outside method bodies" warning. Those keys now use the identifier ValueText; method and event keys are unchanged, since no test shows them crossing to a symbol-built key.

Verification

  • Without the fix: the four patch tests fail with the message above, and the introduced-type test fails with "Introduced-type artifact does not define every requested type". The const drift and kind-change tests pass on main too (that path was fixed by #3597); they pin the moved helper. With the fix: 10/10 pass (two more rows for a keyword-named property removal and a keyword-named field addition, both failing before the member-key fix). The two kind-change rows (keyword-named namespace, and keyword-named type @class) assert that no drift warning appears; both fail before the syntax-key fix.
  • The extension-class lookup is covered by Run_ClosureCallingExtensionInKeywordNamedNamespace_IsPatched in HotReloadNonPublicClassExtensionCallE2ETests (#3597), which passes.
  • Single-class runs, all passed: TransformWorkerBaseCallTests (26), TransformWorkerPartialTypeTests (126), TransformWorkerNonPublicClassExtensionCallTests (26), HotReloadNonPublicClassExtensionCallE2ETests (9), TransformWorkerClientTests (157), TransformWorkerIntroducedTypeTests (36), TransformWorkerIntroducedTypeAccessibilityTests (50), HotReloadPartialTypeE2ETests (7).
  • scripts/check-file-length.sh and the C# part of scripts/check-code-complexity.sh: no findings.

Fixes #3599