Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Assets/Tests/Editor/CodexConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,46 @@ public void NormalizeForCompare_Should_ReturnEmpty_WhenInputIsEmpty()
Assert.AreEqual(string.Empty, result);
}

// ----------------------------------------------------------------
// Legacy table-style env migration
// ----------------------------------------------------------------

[Test]
public void RemoveLegacyEnvSection_Should_RemoveOnlyLegacyULoopEnvTable()
{
string toml = @"[mcp_servers.uLoopMCP]
command = ""node""
args = ['server.js']
env = { ""UNITY_TCP_PORT"" = ""8700"" }

[mcp_servers.uLoopMCP.env]
UNITY_TCP_PORT = ""8700""

[mcp_servers.other]
command = ""python""
";

string result = InvokeRemoveLegacyEnvSection(toml);

StringAssert.DoesNotContain("[mcp_servers.uLoopMCP.env]", result);
StringAssert.Contains("env = { \"UNITY_TCP_PORT\" = \"8700\" }", result);
StringAssert.Contains("[mcp_servers.other]", result);
}

[Test]
public void RemoveLegacyEnvSection_Should_ReturnUnchanged_WhenLegacyTableIsAbsent()
{
string toml = @"[mcp_servers.uLoopMCP]
command = ""node""
args = ['server.js']
env = { ""UNITY_TCP_PORT"" = ""8700"" }
";

string result = InvokeRemoveLegacyEnvSection(toml);

Assert.AreEqual(toml, result);
}

// ----------------------------------------------------------------
// MakeRelativeToConfigurationRoot
// ----------------------------------------------------------------
Expand Down Expand Up @@ -188,5 +228,12 @@ private static string InvokeNormalizeForCompare(string path)
Debug.Assert(method != null, "NormalizeForCompare method not found");
return (string)method.Invoke(null, new object[] { path });
}

private static string InvokeRemoveLegacyEnvSection(string content)
{
MethodInfo method = CodexServiceType.GetMethod("RemoveLegacyEnvSection", PrivateStatic);
Debug.Assert(method != null, "RemoveLegacyEnvSection method not found");
return (string)method.Invoke(null, new object[] { content });
}
}
}
16 changes: 15 additions & 1 deletion Packages/src/Editor/Config/CodexTomlConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public sealed class CodexTomlConfigService : IMcpConfigService
private static readonly Regex SectionRegex = new Regex(
@"(?ms)^\[mcp_servers\.uLoopMCP\]\s*.*?(?=^\[|\z)",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
private static readonly Regex LegacyEnvSectionRegex = new Regex(
@"(?ms)^\[mcp_servers\.uLoopMCP\.env\]\s*.*?(?=^\[|\z)",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
private static readonly Regex AnyMcpServerRegex = new Regex(
@"(?ms)^\[mcp_servers\.[^\]]+\]\s*.*?(?=^\[|\z)",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
Expand Down Expand Up @@ -50,6 +53,9 @@ public bool IsUpdateNeeded(int port)
if (!File.Exists(path)) return true;
string content = File.ReadAllText(path);

// Migrate the legacy table-style env section before writing the inline env value.
if (LegacyEnvSectionRegex.IsMatch(content)) return true;

string serverAbsolutePath = UnityMcpPathResolver.GetTypeScriptServerPath();
if (string.IsNullOrEmpty(serverAbsolutePath) || !File.Exists(serverAbsolutePath)) return false;
string expectedArg0 = UnityMcpPathResolver.MakeRelativeToConfigurationRoot(serverAbsolutePath);
Expand Down Expand Up @@ -81,6 +87,7 @@ public void AutoConfigure(int port)
string path = UnityMcpPathResolver.GetCodexConfigPath();
EnsureDirectory(path);
string content = File.Exists(path) ? File.ReadAllText(path) : string.Empty;
content = RemoveLegacyEnvSection(content);

string serverPath = UnityMcpPathResolver.GetTypeScriptServerPath();
if (string.IsNullOrEmpty(serverPath) || !File.Exists(serverPath))
Expand Down Expand Up @@ -135,6 +142,7 @@ public void DeleteConfiguration()

string content = File.ReadAllText(path);
string result = SectionRegex.Replace(content, string.Empty);
result = LegacyEnvSectionRegex.Replace(result, string.Empty);
if (ReferenceEquals(content, result))
{
return;
Expand All @@ -153,6 +161,7 @@ public void UpdateDevelopmentSettings(int port, bool developmentMode, bool enabl
}

string content = File.ReadAllText(path);
content = RemoveLegacyEnvSection(content);

// If section not exists, create it first and reload
if (!SectionRegex.IsMatch(content))
Expand Down Expand Up @@ -351,7 +360,12 @@ private static void EnsureDirectory(string filePath)
string dir = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
}

private static string RemoveLegacyEnvSection(string content)
{
string result = LegacyEnvSectionRegex.Replace(content, string.Empty);
return Regex.Replace(result, @"(\r?\n){3,}", System.Environment.NewLine + System.Environment.NewLine);
}
Comment on lines +364 to +368

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Restrict newline normalization to the removed section.

The helper currently collapses every run of three or more newlines in the entire TOML document, even when no legacy section is present. This can modify unrelated MCP configuration content and conflicts with the preservation objective. Remove only the matched section, or normalize whitespace only at its immediate boundary.

Proposed minimal fix
 private static string RemoveLegacyEnvSection(string content)
 {
-    string result = LegacyEnvSectionRegex.Replace(content, string.Empty);
-    return Regex.Replace(result, @"(\r?\n){3,}", System.Environment.NewLine + System.Environment.NewLine);
+    return LegacyEnvSectionRegex.Replace(content, string.Empty);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private static string RemoveLegacyEnvSection(string content)
{
string result = LegacyEnvSectionRegex.Replace(content, string.Empty);
return Regex.Replace(result, @"(\r?\n){3,}", System.Environment.NewLine + System.Environment.NewLine);
}
private static string RemoveLegacyEnvSection(string content)
{
return LegacyEnvSectionRegex.Replace(content, string.Empty);
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Packages/src/Editor/Config/CodexTomlConfigService.cs` around lines 364 - 368,
Update RemoveLegacyEnvSection so newline normalization is limited to the
boundaries left by LegacyEnvSectionRegex, rather than applying Regex.Replace
across the entire content. Preserve unrelated TOML/MCP configuration exactly,
including when no legacy section is present, while still removing the matched
legacy section and any immediately adjacent excess blank lines.

}
}