-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(execute_code): load Roslyn from Unity instead of falling back to C# 6 #1374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: beta
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -537,6 +537,29 @@ private static void AssertCompilerSuccess(CompilerResults results) | |
| Assert.IsFalse(results.Errors.HasErrors, string.Join("\n", errors)); | ||
| } | ||
|
|
||
|
|
||
| // ──────────────────── Execute: compiler selection ──────────────────── | ||
|
|
||
| [Test] | ||
| public void Execute_Auto_UsesRoslynLoadedFromUnity() | ||
| { | ||
| var result = Execute("return 1;"); | ||
|
|
||
| Assert.IsTrue(result.Value<bool>("success"), result.ToString()); | ||
| Assert.AreEqual("roslyn", result["data"]["compiler"].Value<string>(), | ||
| "auto fell back to CodeDom, so execute_code is silently limited to C# 6"); | ||
|
Comment on lines
+549
to
+550
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- applicable repository conventions ---'
find /tmp/coderabbit-repo-knowledge/coplaydev-unity-mcp-ac22bd92 -maxdepth 2 -type f -name '*.md' -print \
| sort \
| while read -r f; do
case "$f" in
*/conventions/*|*/learnings/*|*/architecture/*)
printf '\n### %s\n' "$f"
sed -n '1,220p' "$f"
;;
esac
done
printf '%s\n' '--- changed hunk ---'
git diff --unified=40 -- TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs
printf '%s\n' '--- test file outline ---'
ast-grep outline TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs
printf '%s\n' '--- relevant test source ---'
sed -n '500,580p' TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs
printf '%s\n' '--- Roslyn and fallback symbols ---'
rg -n -S 'TryLoadUnityRoslyn|Initialize|CodeDom|ScriptUpdater|applicationContentsPath|compiler' \
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools \
--glob '*.cs'Repository: CoplayDev/unity-mcp Length of output: 19488 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- ExecuteCode files ---'
fd -i 'ExecuteCode' . --type f
printf '%s\n' '--- ExecuteCode declarations and call sites ---'
rg -n -S 'class ExecuteCode|TryLoadUnityRoslyn|Roslyn|CodeDom|Initialize\(' . \
--glob '*ExecuteCode*.cs' \
--glob '!Library/**' \
--glob '!Temp/**'
printf '%s\n' '--- candidate outlines ---'
for f in $(fd -i 'ExecuteCode' . --type f); do
echo "### $f"
ast-grep outline "$f"
doneRepository: CoplayDev/unity-mcp Length of output: 11864 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- compiler selection and initialization ---'
sed -n '190,255p' MCPForUnity/Editor/Tools/ExecuteCode.cs
sed -n '625,785p' MCPForUnity/Editor/Tools/ExecuteCode.cs
printf '%s\n' '--- lifecycle and test setup ---'
sed -n '1,90p' MCPForUnity/Editor/Tools/ExecuteCode.cs
sed -n '1,45p' TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs
rg -n -S 'RoslynCompiler|ResetCache|OnDomainReload|Initialize\(' \
MCPForUnity TestProjects/UnityMCPTests/Assets/Tests/EditMode \
--glob '*.cs'Repository: CoplayDev/unity-mcp Length of output: 19076 Isolate the Roslyn tests from external assemblies and fallback environments.
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| [Test] | ||
| public void Execute_UsingDeclaration_CompilesUnderRoslyn() | ||
| { | ||
| // C# 8: unavailable on the CodeDom fallback, so this also pins the language version | ||
| var result = Execute("using var s = new System.IO.MemoryStream();\nreturn s.CanRead;"); | ||
|
|
||
| Assert.IsTrue(result.Value<bool>("success"), result.ToString()); | ||
| Assert.IsTrue(result["data"]["result"].Value<bool>()); | ||
| } | ||
|
|
||
| private static JObject Execute(string code) | ||
| { | ||
| return ToJObject(ExecuteCode.HandleCommand(new JObject | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ description: "Execute arbitrary C# code inside the Unity Editor." | |
|
|
||
| ## Description | ||
|
|
||
| Execute arbitrary C# code inside the Unity Editor. The code runs as a method body with access to UnityEngine and UnityEditor namespaces. Use 'return' to send data back. Compiled in-memory — no script files created. Actions: execute (run code), get_history (list past executions), replay (re-run a history entry), clear_history. NOTE: safety_checks blocks known dangerous patterns but is not a full sandbox. Compiler options: 'auto' (Roslyn if available, else CodeDom), 'roslyn' (C# 12+, requires Microsoft.CodeAnalysis), 'codedom' (C# 6 only). | ||
| Execute arbitrary C# code inside the Unity Editor. The code runs as a method body with access to UnityEngine and UnityEditor namespaces. Use 'return' to send data back. Compiled in-memory — no script files created. Actions: execute (run code), get_history (list past executions), replay (re-run a history entry), clear_history. NOTE: safety_checks blocks known dangerous patterns but is not a full sandbox. Compiler options: 'auto' and 'roslyn' both use the Roslyn that ships with Unity (C# 9); 'codedom' forces the legacy provider (C# 6 only). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win Synchronize the editor dependency UI.
Update the UI to distinguish bundled C#9 support from optional external Roslyn and use the same availability logic. 🤖 Prompt for AI Agents |
||
|
|
||
| ## Parameters | ||
|
|
||
|
|
@@ -23,7 +23,7 @@ Execute arbitrary C# code inside the Unity Editor. The code runs as a method bod | |
| | `safety_checks` | `bool` | — | Enable basic blocked-pattern checks (File.Delete, Process.Start, infinite loops, etc). Not a full sandbox — advanced bypass is possible. Default: true. | | ||
| | `index` | `int \| None` | — | History entry index to replay (for 'replay' action). | | ||
| | `limit` | `int` | — | Number of history entries to return (for 'get_history' action, 1-50). Default: 10. | | ||
| | `compiler` | `Literal['auto', 'roslyn', 'codedom']` | — | Compiler backend for 'execute' action. 'auto' uses Roslyn if Microsoft.CodeAnalysis is installed, else falls back to CodeDom. 'roslyn' forces Roslyn (C# 12+). 'codedom' forces legacy CSharpCodeProvider (C# 6). Default: auto. | | ||
| | `compiler` | `Literal['auto', 'roslyn', 'codedom']` | — | Compiler backend for 'execute' action. 'auto' and 'roslyn' both load the Roslyn shipped with the editor, which caps the language at C# 9. 'codedom' forces the legacy CSharpCodeProvider (C# 6). Default: auto. | | ||
|
|
||
| ## Returns | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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
Keep compiler-selection documentation consistent with the loader.
The implementation may retain an already-loaded external Roslyn assembly, attempts Unity's bundled assemblies only when needed, and lets only
autofall back to CodeDom.Server/src/services/tools/execute_code.py#L64-L65: document the actual precedence and fallback behavior in the tool parameter annotation.website/docs/reference/tools/scripting_ext/execute_code.md#L26-L26: regenerate or update the reference documentation with the same conditional behavior.📍 Affects 2 files
Server/src/services/tools/execute_code.py#L64-L65(this comment)website/docs/reference/tools/scripting_ext/execute_code.md#L26-L26🤖 Prompt for AI Agents