From 92158844dc8f75e5878e87eb992bb66a45750f41 Mon Sep 17 00:00:00 2001 From: hatayama Date: Mon, 23 Nov 2026 15:51:42 +0900 Subject: [PATCH 1/5] test: Cover hot reload of types in a keyword-named namespace A host type in a namespace with a keyword-named segment (@event) fails hot reload with "Type '...@event.' was not found", because the worker escapes the segment in the metadata name it emits. These end-to-end tests edit a method body, a property getter, a call to an added method, and a nested type's method of hosts in such a namespace, and expect each row to be Patched with the edited value. The getter and added-method cases also guard the shim namespace, which is emitted as C# source and must keep the escaped spelling. --- .../HotReloadKeywordNamespaceE2ETests.cs | 165 ++++++++++++++++++ .../HotReloadKeywordNamespaceE2ETests.cs.meta | 11 ++ .../HotReloadKeywordNamespaceFixtures.cs | 39 +++++ .../HotReloadKeywordNamespaceFixtures.cs.meta | 11 ++ 4 files changed, 226 insertions(+) create mode 100644 Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs create mode 100644 Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs.meta create mode 100644 Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs create mode 100644 Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs.meta diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs new file mode 100644 index 000000000..dd07c7f05 --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs @@ -0,0 +1,165 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +using NUnit.Framework; + +using UnityEngine; + +using io.github.hatayama.UnityCliLoop.FirstPartyTools; +using io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.@event; + +namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload +{ + /// + /// End-to-end coverage for hot reload of types declared in a namespace with a keyword-named + /// segment (@event): a method body, a property getter, a call to an added method and a nested + /// type's method are all patched, and RevertAll restores the compiled values. + /// + public class HotReloadKeywordNamespaceE2ETests + { + private const string FixtureFileName = "HotReloadKeywordNamespaceFixtures.cs"; + + private const string AddedMethodAnchor = " public int CallsAddedMethod()"; + + private const string AddedMethodSource = + " private int AddedKeywordNamespaceValue()\n" + + " {\n" + + " return 35998;\n" + + " }\n\n"; + + private HotReloadDomainTestScope _scope; + + [SetUp] + public void SetUp() + { + _scope = new HotReloadDomainTestScope(); + HotReloadAutoRefreshHold.SyncToActiveChanges(); + } + + [TearDown] + public void TearDown() + { + _scope.Dispose(); + HotReloadAutoRefreshHold.SyncToActiveChanges(); + } + + /// + /// What: an edited method body of a type in a keyword-named namespace is Patched and returns + /// the edited value; RevertAll restores the compiled value. + /// + [Test] + public async Task Run_MethodOfTypeInKeywordNamedNamespace_IsPatchedAndReverts() + { + HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost(); + Assert.That(host.MethodValue(), Is.EqualTo(35990)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace("return 35990;", "return 35991;")); + + AssertKind(result, "MethodValue", HotReloadMethodOutcomeKind.Patched); + Assert.That(host.MethodValue(), Is.EqualTo(35991)); + HotReloadCompositionRoot.Services.Patcher.RevertAll(); + Assert.That(host.MethodValue(), Is.EqualTo(35990)); + } + + /// + /// What: an edited property getter of a type in a keyword-named namespace is Patched and + /// returns the edited value, so the getter shim declared in that namespace compiles. + /// + [Test] + public async Task Run_PropertyGetterOfTypeInKeywordNamedNamespace_IsPatched() + { + HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost(); + Assert.That(host.GetterValue, Is.EqualTo(35993)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace("return 35993;", "return 35994;")); + + AssertKind(result, "get_GetterValue", HotReloadMethodOutcomeKind.Patched); + Assert.That(host.GetterValue, Is.EqualTo(35994)); + } + + /// + /// What: a method of a type in a keyword-named namespace that now calls a method this reload + /// adds is Patched and returns what the added method computes, so the call the rewrite + /// qualifies with the shim's namespace compiles. + /// + [Test] + public async Task Run_CallToAddedMethodOfTypeInKeywordNamedNamespace_IsPatched() + { + HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost(); + Assert.That(host.CallsAddedMethod(), Is.EqualTo(35996)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk + .Replace(AddedMethodAnchor, AddedMethodSource + AddedMethodAnchor) + .Replace("return 35996;", "return AddedKeywordNamespaceValue();")); + + AssertKind(result, "CallsAddedMethod", HotReloadMethodOutcomeKind.Patched); + Assert.That(host.CallsAddedMethod(), Is.EqualTo(35998)); + } + + /// + /// What: an edited method body of a type nested in a type of a keyword-named namespace is + /// Patched and returns the edited value; RevertAll restores the compiled value. + /// + [Test] + public async Task Run_MethodOfNestedTypeInKeywordNamedNamespace_IsPatchedAndReverts() + { + HotReloadKeywordNamespaceHost.Nested nested = new HotReloadKeywordNamespaceHost.Nested(); + Assert.That(nested.NestedValue(), Is.EqualTo(35999)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace("return 35999;", "return 36000;")); + + AssertKind(result, "NestedValue", HotReloadMethodOutcomeKind.Patched); + Assert.That(nested.NestedValue(), Is.EqualTo(36000)); + HotReloadCompositionRoot.Services.Patcher.RevertAll(); + Assert.That(nested.NestedValue(), Is.EqualTo(35999)); + } + + private static async Task RunEditAsync(Func edit) + { + string fixturePath = Path.GetFullPath( + Path.Combine(Application.dataPath, "Tests", "Editor", "HotReload", FixtureFileName)); + string onDisk = File.ReadAllText(fixturePath); + string edited = edit(onDisk); + Assert.That(edited, Is.Not.EqualTo(onDisk), "Precondition: the edit must change the fixture."); + string editedPath = HotReloadTestSourceWriter.WriteEditedSource("KeywordNamespaceE2E.cs", edited); + return await HotReloadCompositionRoot.Services.Orchestrator.RunAsync( + new[] { fixturePath }, + editedPath, + CancellationToken.None); + } + + private static void AssertKind( + HotReloadOrchestratorResult result, + string methodName, + HotReloadMethodOutcomeKind expectedKind) + { + foreach (HotReloadMethodOutcome outcome in result.Methods) + { + if (outcome.Method.Contains("." + methodName + "(")) + { + Assert.That(outcome.Kind, Is.EqualTo(expectedKind), outcome.Method + ": " + outcome.Reason); + return; + } + } + + Assert.Fail("No row for " + methodName + ". Rows: " + DescribeRows(result)); + } + + private static string DescribeRows(HotReloadOrchestratorResult result) + { + string rows = string.Empty; + foreach (HotReloadMethodOutcome outcome in result.Methods) + { + rows += outcome.Method + " = " + outcome.Kind + " (" + outcome.Reason + "); "; + } + + return rows; + } + } +} diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs.meta b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs.meta new file mode 100644 index 000000000..2b2c7f50a --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 29e1f6c248584468c9b31e8534b8222c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs new file mode 100644 index 000000000..11222ac94 --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs @@ -0,0 +1,39 @@ +namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.@event +{ + /// + /// Host types declared in a namespace whose last segment is a C# keyword. Tests edit their + /// bodies to check that hot reload patches them: the escaped spelling (@event) appears only in + /// source, while the compiled metadata names the namespace "event". + /// + public sealed class HotReloadKeywordNamespaceHost + { + public int MethodValue() + { + return 35990; + } + + public int GetterValue + { + get + { + return 35993; + } + } + + public int CallsAddedMethod() + { + return 35996; + } + + /// + /// Nested host, so the conversion of a nested type name in that namespace is covered too. + /// + public sealed class Nested + { + public int NestedValue() + { + return 35999; + } + } + } +} diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs.meta b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs.meta new file mode 100644 index 000000000..04e6a79d2 --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6700df0513cd94d51848148484f7d12d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From c9cf7d85a090e5d1a9e2e874cc63ac57b306fd7e Mon Sep 17 00:00:00 2001 From: hatayama Date: Mon, 23 Nov 2026 15:51:43 +0900 Subject: [PATCH 2/5] fix: Hot reload patches types in a namespace with a keyword-named segment CecilTypeNames.ToMetadataName built the namespace part with INamespaceSymbol.ToDisplayString(), which escapes keyword identifiers (@event). Compiled metadata names the namespace "event", so the orchestrator could not find the host type and failed the row. Every metadata name derived from ToMetadataName (patch entry host types, method keys, introduced-type lookups, and the reflection names made from it) carried the same escape. The segment join that #3597 added privately to ConstDriftCollector now lives in CecilTypeNames.ToNamespaceMetadataName, and both metadata-name builders use it. The shim namespaces in OrdinaryMethodShimTypes and PropertyGetterEmitter stay escaped on purpose: they are emitted as C# source (namespace declarations and global:: qualified calls), where the unescaped keyword would not parse. Fixes #3599 --- .../TransformWorker~/CecilTypeNames.cs | 22 ++++++++++++++++++- .../TransformWorker~/ConstDriftCollector.cs | 18 +-------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/CecilTypeNames.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/CecilTypeNames.cs index 586eebc71..9c6474bbf 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/CecilTypeNames.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/CecilTypeNames.cs @@ -29,7 +29,27 @@ public static string ToMetadataName(INamedTypeSymbol typeSymbol) return typeSymbol.MetadataName; } - return typeSymbol.ContainingNamespace.ToDisplayString() + "." + typeSymbol.MetadataName; + return ToNamespaceMetadataName(typeSymbol.ContainingNamespace) + "." + typeSymbol.MetadataName; + } + + /// + /// Builds the dotted namespace name as metadata records it, for both metadata and reflection + /// type names. Returns an empty string for the global namespace. + /// + // Why the names and not ToDisplayString: the display escapes a keyword identifier (@event), + // which no metadata name carries, so a lookup by that key would miss the type. + internal static string ToNamespaceMetadataName(INamespaceSymbol namespaceSymbol) + { + List names = new List(); + for (INamespaceSymbol current = namespaceSymbol; + current != null && !current.IsGlobalNamespace; + current = current.ContainingNamespace) + { + names.Add(current.Name); + } + + names.Reverse(); + return string.Join(".", names); } public static string ToParameterTypeFullName(IParameterSymbol parameterSymbol) diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/ConstDriftCollector.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/ConstDriftCollector.cs index 94c99b644..0d264a818 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/ConstDriftCollector.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/ConstDriftCollector.cs @@ -174,23 +174,7 @@ internal static string ToReflectionMetadataName(INamedTypeSymbol typeSymbol) return typeSymbol.MetadataName; } - return ToNamespaceMetadataName(typeSymbol.ContainingNamespace) + "." + typeSymbol.MetadataName; - } - - // Why the names and not ToDisplayString: the display escapes a keyword identifier (@event), - // which no metadata name carries, so a lookup by that key would miss the type. - private static string ToNamespaceMetadataName(INamespaceSymbol namespaceSymbol) - { - List names = new List(); - for (INamespaceSymbol current = namespaceSymbol; - current != null && !current.IsGlobalNamespace; - current = current.ContainingNamespace) - { - names.Add(current.Name); - } - - names.Reverse(); - return string.Join(".", names); + return CecilTypeNames.ToNamespaceMetadataName(typeSymbol.ContainingNamespace) + "." + typeSymbol.MetadataName; } /// From ee989709d93c9dbd24c0d620a54152c629bbd5a8 Mon Sep 17 00:00:00 2001 From: hatayama Date: Mon, 23 Nov 2026 16:01:42 +0900 Subject: [PATCH 3/5] test: Cover const drift, kind change and introduced types in a keyword-named namespace The issue asks for a keyword-segment regression test on every place that turns a namespace into a name. Add the remaining paths: - const drift and the compiled property kind-change warning, which look the compiled type up through ConstDriftCollector.ToReflectionMetadataName and now share the moved namespace helper; - a type introduced into the @event namespace by one reload and body edited by the next, which goes through CecilTypeNames.ToMetadataName in the introduced-type lookup. Without the fix the introducing reload already fails with "artifact does not define every requested type". --- .../HotReloadKeywordNamespaceE2ETests.cs | 47 +++++++ .../HotReloadKeywordNamespaceFixtures.cs | 9 ++ ...dKeywordNamespaceIntroducedTypeE2ETests.cs | 122 ++++++++++++++++++ ...ordNamespaceIntroducedTypeE2ETests.cs.meta | 11 ++ 4 files changed, 189 insertions(+) create mode 100644 Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs create mode 100644 Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs.meta diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs index dd07c7f05..a555d357f 100644 --- a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs @@ -120,6 +120,53 @@ public async Task Run_MethodOfNestedTypeInKeywordNamedNamespace_IsPatchedAndReve Assert.That(nested.NestedValue(), Is.EqualTo(35999)); } + /// + /// What: a changed const of a type in a keyword-named namespace produces the const drift + /// warning naming that const, so the compiled type is found by its metadata name. + /// + [Test] + public async Task Run_ConstChangedInTypeOfKeywordNamedNamespace_WarnsAboutTheDrift() + { + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace("KeywordTuning = 4;", "KeywordTuning = 6;")); + + Assert.That( + FindWarningContaining(result, "KeywordTuning"), + Does.Contain("is 6 in the edited source but 4 in the compiled assembly"), + string.Join("\n", result.Warnings)); + } + + /// + /// What: removing a compiled property of a type in a keyword-named namespace produces the + /// kind-change warning naming that property, so the compiled type is found by its metadata + /// name. + /// + [Test] + public async Task Run_PropertyRemovedFromTypeOfKeywordNamedNamespace_WarnsAboutTheKindChange() + { + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace(" public int KindChangeValue => 36002;\n", string.Empty)); + + Assert.That( + FindWarningContaining(result, "KindChangeValue"), + Does.StartWith("Compiled property '"), + string.Join("\n", result.Warnings)); + } + + private static string FindWarningContaining(HotReloadOrchestratorResult result, string fragment) + { + foreach (string warning in result.Warnings) + { + if (warning.Contains(fragment)) + { + return warning; + } + } + + Assert.Fail("No warning names " + fragment + ".\n" + string.Join("\n", result.Warnings)); + return null; + } + private static async Task RunEditAsync(Func edit) { string fixturePath = Path.GetFullPath( diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs index 11222ac94..b9d18c210 100644 --- a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs @@ -7,6 +7,8 @@ namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.@event /// public sealed class HotReloadKeywordNamespaceHost { + public const int KeywordTuning = 4; + public int MethodValue() { return 35990; @@ -25,6 +27,13 @@ public int CallsAddedMethod() return 35996; } + public int CallsIntroducedType() + { + return 36001; + } + + public int KindChangeValue => 36002; + /// /// Nested host, so the conversion of a nested type name in that namespace is covered too. /// diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs new file mode 100644 index 000000000..568661e3a --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +using NUnit.Framework; + +using io.github.hatayama.UnityCliLoop.FirstPartyTools; + +namespace io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload +{ + /// + /// End-to-end coverage of a type an earlier reload introduced into a namespace with a + /// keyword-named segment (@event): a later body-only edit is patched on the retained artifact, + /// which is found by the type's metadata name. + /// + public class HotReloadKeywordNamespaceIntroducedTypeE2ETests : HotReloadIntroducedTypeE2ETestBase + { + private const string FixtureFileName = "HotReloadKeywordNamespaceFixtures.cs"; + private const string HostTypeAnchor = " public sealed class HotReloadKeywordNamespaceHost"; + private const string CallerBody = "return 36001;"; + private const string IntroducedTypeSimpleName = "HotReloadKeywordNamespaceIntroduced"; + + // Why "event" without the escape: this is the name the compiled metadata carries. + private const string IntroducedTypeMetadataName = + "io.github.hatayama.UnityCliLoop.Tests.Editor.HotReload.event." + IntroducedTypeSimpleName; + + /// + /// What: editing only the body of a type that an earlier reload introduced into a + /// keyword-named namespace patches it on the retained artifact without failures, and a call + /// into the artifact runs the edited body. + /// + [Test] + public async Task Run_IntroducedTypeInKeywordNamedNamespaceBodyEdited_PatchesTheRetainedArtifact() + { + string fixturePath = FixturePath(FixtureFileName); + + await RunInIntroducedTypeDomainAsync(async readArtifact => + { + HotReloadOrchestratorResult first = await RunAsync( + fixturePath, + "KeywordNamespaceIntroducing.cs", + "36003"); + Assert.That(CountFailures(first), Is.EqualTo(0), DescribeOutcomes(first)); + Assert.That(ReadComputedValue(readArtifact()), Is.EqualTo(36003)); + + HotReloadOrchestratorResult second = await RunAsync( + fixturePath, + "KeywordNamespaceIntroducedBodyEdited.cs", + "36004"); + + Assert.That(CountFailures(second), Is.EqualTo(0), DescribeOutcomes(second)); + Assert.That( + HasPatchedCompute(second), + Is.True, + "The edited body of the introduced type must be reported as patched.\n" + DescribeOutcomes(second)); + Assert.That(ReadComputedValue(readArtifact()), Is.EqualTo(36004)); + }); + } + + private static Task RunAsync( + string fixturePath, + string editedFileName, + string computedValue) + { + string onDisk = File.ReadAllText(fixturePath); + Assert.That(onDisk, Does.Contain(HostTypeAnchor), "Precondition: host type anchor must exist."); + Assert.That(onDisk, Does.Contain(CallerBody), "Precondition: caller body must exist."); + string introduced = + " public sealed class " + IntroducedTypeSimpleName + "\n" + + " {\n" + + " [System.Runtime.CompilerServices.MethodImpl(\n" + + " System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]\n" + + " public int Compute()\n" + + " {\n" + + " return " + computedValue + ";\n" + + " }\n" + + " }\n" + + "\n"; + string edited = onDisk + .Replace(HostTypeAnchor, introduced + HostTypeAnchor, StringComparison.Ordinal) + .Replace( + CallerBody, + "return new " + IntroducedTypeSimpleName + "().Compute();", + StringComparison.Ordinal); + return HotReloadCompositionRoot.Services.Orchestrator.RunAsync( + new[] { fixturePath }, + contentPathOverride: null, + CancellationToken.None, + new Dictionary + { + [fixturePath] = HotReloadTestSourceWriter.WriteEditedSource(editedFileName, edited) + }); + } + + private static bool HasPatchedCompute(HotReloadOrchestratorResult result) + { + foreach (HotReloadMethodOutcome outcome in result.Methods) + { + if (outcome.Kind == HotReloadMethodOutcomeKind.Patched + && outcome.Method != null + && outcome.Method.Contains("." + IntroducedTypeSimpleName + ".Compute()", StringComparison.Ordinal)) + { + return true; + } + } + + return false; + } + + private static int ReadComputedValue(HotReloadIntroducedTypeArtifact artifact) + { + Assert.That(artifact, Is.Not.Null, "A reload had to introduce the type before this check."); + Type introducedType = artifact.Assembly.GetType(IntroducedTypeMetadataName, throwOnError: false); + Assert.That(introducedType, Is.Not.Null, "The artifact must hold " + IntroducedTypeMetadataName + "."); + MethodInfo compute = introducedType.GetMethod("Compute", BindingFlags.Instance | BindingFlags.Public); + return (int)compute.Invoke(Activator.CreateInstance(introducedType), Array.Empty()); + } + } +} diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs.meta b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs.meta new file mode 100644 index 000000000..85ceda89b --- /dev/null +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceIntroducedTypeE2ETests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 48b39b36c15f141b9ad9b349cbb5477b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From dee8caefff438732ee665566b7388d9946467492 Mon Sep 17 00:00:00 2001 From: hatayama Date: Mon, 23 Nov 2026 22:24:11 +0900 Subject: [PATCH 4/5] fix: Syntax keys name keyword-named namespaces and types without the escape The kind-change warning keys a removed compiled property by the reflection name of its type, built from symbol names (namespace "event"). The drift check strips the matching snapshot declaration by a key that WorkerSyntaxIndex builds from syntax, which kept the source spelling (@event, @class). The two keys never met, so a removed property of such a type also raised the file-level "Edits outside method bodies" warning. WorkerSyntaxIndex now builds the type part of every syntax key from unescaped names: namespace segments and type identifiers use ValueText. Every other syntax key set is built and looked up through the same builders, so their comparisons stay consistent. The kind-change tests now assert that no drift warning appears, and a row with a keyword-named type (@class) covers the type identifier. --- .../HotReloadKeywordNamespaceE2ETests.cs | 37 +++++++++++++++++-- .../HotReloadKeywordNamespaceFixtures.cs | 9 +++++ .../TransformWorker~/WorkerSyntaxIndex.cs | 27 +++++++++----- 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs index a555d357f..d63fbe038 100644 --- a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs @@ -138,11 +138,12 @@ public async Task Run_ConstChangedInTypeOfKeywordNamedNamespace_WarnsAboutTheDri /// /// What: removing a compiled property of a type in a keyword-named namespace produces the - /// kind-change warning naming that property, so the compiled type is found by its metadata - /// name. + /// kind-change warning naming that property, and no outside-method-body drift warning, + /// because the removed declaration is matched to the kind-change key and left out of the + /// drift comparison. /// [Test] - public async Task Run_PropertyRemovedFromTypeOfKeywordNamedNamespace_WarnsAboutTheKindChange() + public async Task Run_PropertyRemovedFromTypeOfKeywordNamedNamespace_WarnsAboutTheKindChangeOnly() { HotReloadOrchestratorResult result = await RunEditAsync( onDisk => onDisk.Replace(" public int KindChangeValue => 36002;\n", string.Empty)); @@ -151,6 +152,36 @@ public async Task Run_PropertyRemovedFromTypeOfKeywordNamedNamespace_WarnsAboutT FindWarningContaining(result, "KindChangeValue"), Does.StartWith("Compiled property '"), string.Join("\n", result.Warnings)); + AssertNoOutsideMethodBodyDriftWarning(result); + } + + /// + /// What: removing a compiled property of a keyword-named type (@class) in a keyword-named + /// namespace produces the kind-change warning naming that property and no + /// outside-method-body drift warning. + /// + [Test] + public async Task Run_PropertyRemovedFromKeywordNamedType_WarnsAboutTheKindChangeOnly() + { + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace(" public int ClassKindChangeValue => 36005;\n", string.Empty)); + + Assert.That( + FindWarningContaining(result, "ClassKindChangeValue"), + Does.StartWith("Compiled property '"), + string.Join("\n", result.Warnings)); + AssertNoOutsideMethodBodyDriftWarning(result); + } + + private static void AssertNoOutsideMethodBodyDriftWarning(HotReloadOrchestratorResult result) + { + foreach (string warning in result.Warnings) + { + Assert.That( + warning, + Does.Not.StartWith("Edits outside method bodies"), + string.Join("\n", result.Warnings)); + } } private static string FindWarningContaining(HotReloadOrchestratorResult result, string fragment) diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs index b9d18c210..eb935150b 100644 --- a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs @@ -45,4 +45,13 @@ public int NestedValue() } } } + + /// + /// Host whose own name is a C# keyword, so a key built from its identifier must drop the @ + /// as well as the namespace's. + /// + public sealed class @class + { + public int ClassKindChangeValue => 36005; + } } diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs index 1126e9a5b..6029533f5 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs @@ -74,7 +74,9 @@ internal static string BuildTypeMetadataNameFromSyntax(TypeDeclarationSyntax typ TypeDeclarationSyntax current = typeDeclaration; while (current != null) { - string simpleName = current.Identifier.Text; + // Why ValueText: the key stands for a metadata name, and kind-change keys built from + // symbols meet these keys; Text keeps the @ of a keyword-named type such as @class. + string simpleName = current.Identifier.ValueText; if (current.TypeParameterList != null && current.TypeParameterList.Parameters.Count > 0) { simpleName += "`" + current.TypeParameterList.Parameters.Count.ToString(CultureInfo.InvariantCulture); @@ -102,7 +104,7 @@ internal static string BuildTypeMetadataNameFromSyntax(TypeDeclarationSyntax typ internal static string BuildEnumMetadataNameFromSyntax(EnumDeclarationSyntax enumDeclaration) { - string simpleName = enumDeclaration.Identifier.Text; + string simpleName = enumDeclaration.Identifier.ValueText; if (enumDeclaration.Parent is TypeDeclarationSyntax containingType) { return BuildTypeMetadataNameFromSyntax(containingType) + "+" + simpleName; @@ -125,14 +127,9 @@ internal static string GetContainingNamespaceName(SyntaxNode node) SyntaxNode current = node.Parent; while (current != null) { - // Why NormalizeWhitespace: trivia in nested namespace names must not invent distinct keys. - if (current is NamespaceDeclarationSyntax namespaceDeclaration) + if (current is BaseNamespaceDeclarationSyntax namespaceDeclaration) { - parts.Add(namespaceDeclaration.Name.NormalizeWhitespace().ToString()); - } - else if (current is FileScopedNamespaceDeclarationSyntax fileScopedNamespace) - { - parts.Add(fileScopedNamespace.Name.NormalizeWhitespace().ToString()); + parts.Add(ToUnescapedName(namespaceDeclaration.Name)); } current = current.Parent; @@ -147,6 +144,18 @@ internal static string GetContainingNamespaceName(SyntaxNode node) return string.Join(".", parts); } + // Why segment ValueTexts rather than the name text: the text keeps trivia and the @ of a + // keyword-named segment such as @event, which no metadata name carries, so a key built from a + // symbol would never meet it. + private static string ToUnescapedName(NameSyntax name) + { + return string.Join( + ".", + name.DescendantNodesAndSelf() + .OfType() + .Select(simpleName => simpleName.Identifier.ValueText)); + } + internal static string BuildSyntaxPropertyKey( string typeMetadataName, PropertyDeclarationSyntax propertyDeclaration) From d2e0df5a9fbff9bcd12d1c513cc2002962ab0e67 Mon Sep 17 00:00:00 2001 From: hatayama Date: Mon, 23 Nov 2026 23:10:04 +0900 Subject: [PATCH 5/5] fix: Property and field syntax keys name keyword-named members without the escape A keyword-named member kept its @ in the syntax keys while the keys built from symbols drop it, so the two never met: - removing a compiled property such as @checked raised the file-level "Edits outside method bodies" warning next to the kind-change warning, because the kind-change key uses the compiled member name; - adding a field such as @event raised the same warning, because the added-field key uses the field symbol name while the snapshot map and the strip rewriter keyed the declaration by its text. Property keys in WorkerSyntaxIndex and the field keys built from declarators now use the identifier ValueText. Method and event keys are unchanged; no test shows them crossing to a symbol-built key. --- .../HotReloadKeywordNamespaceE2ETests.cs | 39 +++++++++++++++++++ .../HotReloadKeywordNamespaceFixtures.cs | 7 ++++ .../TransformWorker~/AddedFieldClassifier.cs | 6 +-- .../StripHandledMemberDeclarationsRewriter.cs | 4 +- .../TransformWorker~/WorkerSyntaxIndex.cs | 4 +- .../WorkerSyntaxMemberMaps.cs | 4 +- 6 files changed, 56 insertions(+), 8 deletions(-) diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs index d63fbe038..3d7204a51 100644 --- a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceE2ETests.cs @@ -173,6 +173,45 @@ public async Task Run_PropertyRemovedFromKeywordNamedType_WarnsAboutTheKindChang AssertNoOutsideMethodBodyDriftWarning(result); } + /// + /// What: removing a compiled keyword-named property (@checked) produces the kind-change + /// warning naming it and no outside-method-body drift warning. + /// + [Test] + public async Task Run_KeywordNamedPropertyRemoved_WarnsAboutTheKindChangeOnly() + { + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk.Replace(" public int @checked => 36007;\n", string.Empty)); + + Assert.That( + FindWarningContaining(result, ".checked'"), + Does.StartWith("Compiled property '"), + string.Join("\n", result.Warnings)); + AssertNoOutsideMethodBodyDriftWarning(result); + } + + /// + /// What: a keyword-named field (@event) that the edit adds and a method now reads is Patched + /// with the field's initial value, and raises no outside-method-body drift warning. + /// + [Test] + public async Task Run_KeywordNamedFieldAdded_IsPatchedWithoutDriftWarning() + { + HotReloadKeywordNamespaceHost host = new HotReloadKeywordNamespaceHost(); + Assert.That(host.ReadsAddedField(), Is.EqualTo(36008)); + + HotReloadOrchestratorResult result = await RunEditAsync( + onDisk => onDisk + .Replace( + " public int ReadsAddedField()", + " private int @event = 36009;\n\n public int ReadsAddedField()") + .Replace("return 36008;", "return @event;")); + + AssertKind(result, "ReadsAddedField", HotReloadMethodOutcomeKind.Patched); + Assert.That(host.ReadsAddedField(), Is.EqualTo(36009)); + AssertNoOutsideMethodBodyDriftWarning(result); + } + private static void AssertNoOutsideMethodBodyDriftWarning(HotReloadOrchestratorResult result) { foreach (string warning in result.Warnings) diff --git a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs index eb935150b..49e5ea7fd 100644 --- a/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs +++ b/Assets/Tests/Editor/HotReload/HotReloadKeywordNamespaceFixtures.cs @@ -34,6 +34,13 @@ public int CallsIntroducedType() public int KindChangeValue => 36002; + public int @checked => 36007; + + public int ReadsAddedField() + { + return 36008; + } + /// /// Nested host, so the conversion of a nested type name in that namespace is covered too. /// diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/AddedFieldClassifier.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/AddedFieldClassifier.cs index d508f66f5..6335e5fec 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/AddedFieldClassifier.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/AddedFieldClassifier.cs @@ -87,9 +87,9 @@ private static bool SnapshotDeclaresSameFieldType( Dictionary snapshotFieldMap = WorkerSyntaxMemberMaps.BuildSyntaxFieldMapOrNull(baseline.SnapshotRoot); - // Why the identifier text, not the symbol name: the snapshot map is keyed by the text as - // written, which keeps the @ of an escaped identifier such as @event. - string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeState.TypeMetadataNameFromSyntax, variable.Identifier.Text); + // Why ValueText: field keys drop the @ of a keyword-named field such as @event, so the + // added-field keys built from symbol names meet the snapshot map built from syntax. + string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeState.TypeMetadataNameFromSyntax, variable.Identifier.ValueText); if (snapshotFieldMap == null || !snapshotFieldMap.TryGetValue(syntaxKey, out VariableDeclaratorSyntax snapshotVariable) || !(snapshotVariable.Parent is VariableDeclarationSyntax snapshotDeclaration)) diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/StripHandledMemberDeclarationsRewriter.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/StripHandledMemberDeclarationsRewriter.cs index 55e1d4f38..1bd9c738f 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/StripHandledMemberDeclarationsRewriter.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/StripHandledMemberDeclarationsRewriter.cs @@ -240,7 +240,7 @@ public override SyntaxNode VisitEventFieldDeclaration(EventFieldDeclarationSynta { string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey( typeMetadataName, - variable.Identifier.Text); + variable.Identifier.ValueText); if (!_syntaxKeysToStrip.Contains(syntaxKey)) { remaining.Add(variable); @@ -273,7 +273,7 @@ public override SyntaxNode VisitFieldDeclaration(FieldDeclarationSyntax node) List remaining = new List(); foreach (VariableDeclaratorSyntax variable in node.Declaration.Variables) { - string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.Text); + string syntaxKey = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.ValueText); if (!_syntaxKeysToStrip.Contains(syntaxKey)) { remaining.Add(variable); diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs index 6029533f5..c051e023b 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxIndex.cs @@ -160,7 +160,9 @@ internal static string BuildSyntaxPropertyKey( string typeMetadataName, PropertyDeclarationSyntax propertyDeclaration) { - string name = propertyDeclaration.Identifier.Text; + // Why ValueText: kind-change keys name the compiled property by its symbol name, which + // drops the @ of a keyword-named property such as @checked. + string name = propertyDeclaration.Identifier.ValueText; if (propertyDeclaration.ExplicitInterfaceSpecifier != null) { // Why NormalizeWhitespace: keep property keys symmetric with BuildSyntaxMethodKey so diff --git a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxMemberMaps.cs b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxMemberMaps.cs index 570aefe02..4f07ca825 100644 --- a/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxMemberMaps.cs +++ b/Packages/src/Editor/FirstPartyTools/HotReload/TransformWorker~/WorkerSyntaxMemberMaps.cs @@ -43,7 +43,7 @@ internal static Dictionary BuildSyntaxFieldMap { foreach (VariableDeclaratorSyntax variable in fieldDeclaration.Declaration.Variables) { - string key = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.Text); + string key = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.ValueText); if (map.ContainsKey(key)) { return null; @@ -215,7 +215,7 @@ internal static Dictionary BuildSyntaxEventFie { // Why field key format: kind-change identity is type metadata + "::" + name, // the same shape BuildSyntaxFieldKey already uses. - string key = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.Text); + string key = WorkerSyntaxIndex.BuildSyntaxFieldKey(typeMetadataName, variable.Identifier.ValueText); if (map.ContainsKey(key)) { return null;