Repository navigation
Conversation
…process The first execute_code after every domain reload took ~9 s, every later one ~1 s. The cost is the in-process Microsoft.CodeAnalysis backend running on the Editor's Mono, which JIT-compiles Roslyn afresh in each domain: measured on 6000.3, its first compile takes 7.6 s even against mscorlib alone, then ~0.8 s. A reload throws that away, so agents pay it after each script change, usually on the very next call. The Editor ships its own Roslyn for script compilation (Data/DotNetSdkRoslyn/csc.dll on Data/NetCoreRuntime/dotnet, precompiled for .NET). A new `csc` backend runs it out of process through its compiler server (-shared, VBCSCompiler), with references in a response file, and loads the DLL it writes. The server is a separate process, so a domain reload doesn't touch it. With the same 266 references, the first call after a reload now takes 0.45 s, 0.6 s when the server isn't running yet, and ~0.2-0.3 s after that. It takes the latest C# without the NuGet install. `auto` tries it first, then the in-process package, then CodeDom. It falls through only when the compiler couldn't run (not found, didn't start, timed out, exited without diagnostics). Compile errors are returned as they are, mapped to the snippet's lines like the other backends. `roslyn` and `codedom` are unchanged when asked for.
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Bundled compiler execution MCPForUnity/Editor/Tools/ExecuteCode.cs |
Adds a backend that locates the Editor’s dotnet runtime and csc.dll, compiles temporary source and reference files with a 60-second timeout, parses diagnostics, and loads successful output assemblies. |
Compiler selection and validation MCPForUnity/Editor/Tools/ExecuteCode.cs, Server/src/services/tools/execute_code.py, TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs, website/docs/reference/tools/scripting_ext/execute_code.md |
Adds explicit csc selection and tries the bundled compiler first in auto mode. Updates the server parameter and reference documentation. Adds tests for execution, modern syntax, line-numbered errors, and auto selection. |
Priority:
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Feature
Sequence Diagram(s)
sequenceDiagram
participant ExecuteCode
participant BundledCscCompiler
participant CscProcess
ExecuteCode->>BundledCscCompiler: Compile source with bundled csc
BundledCscCompiler->>CscProcess: Start compiler with timeout
CscProcess-->>BundledCscCompiler: Return diagnostics and output assembly
BundledCscCompiler->>BundledCscCompiler: Parse diagnostics or load assembly
Suggested reviewers: scriptwonder, kpkhxlgy0
Merge Risk: 
Minimal · up to 15413
-
The new compiler option appears ready to merge after normal checks; no concrete issue requiring a code change was found.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Description check
-
Explanation
The description follows the repository template and explains the motivation, implementation, fallback behavior, compatibility, testing, documentation updates, and related issue. It clearly notes that Unity EditMode tests were added but not run locally.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 3 files. (1 skipped: 1 unsupported.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Description
The first
execute_codeafter every domain reload takes ~9 s, and every later one ~1 s. Agents pay it after each script change, usually on the very next call: they recompile, then inspect withexecute_code.The cost is the in-process Microsoft.CodeAnalysis backend running on the Editor's Mono, which JIT-compiles Roslyn afresh in every domain. Measured on 6000.3.14f1 by timing each phase of the cold path right after a reload (driven through the CodeDom backend, so Roslyn itself stayed cold):
MetadataReference.CreateFromFile× 266mscorlibonlyThe first compile is as slow with one tiny reference, so the cost is JIT-compiling Roslyn, not reading metadata. A background warm-up after each reload wouldn't help the common case:
refresh_unityreturns right as the reload ends, so the next call would run into a warm-up that has barely started.The Editor ships its own Roslyn for script compilation (
Data/DotNetSdkRoslyn/csc.dllonData/NetCoreRuntime/dotnet), precompiled for .NET, plus its compiler server (VBCSCompiler). With the same 266 references from the command line: 0.37 s per compile as a fresh process, 0.09 s through the warm server. The server is a separate process, so a domain reload doesn't touch it.Type of Change
Changes Made
cscbackend (BundledCscCompilerinExecuteCode.cs). It writes the wrapped snippet and a response file with the references (~300-r:flags would overflow the command line), then runsdotnet csc.dll -noconfig -shared(-langversion:latest, 60 s timeout). It loads the DLL it writes the same way the other backends do, and maps compile errors to the snippet's lines (WrapperLineOffset). Work files go to a per-call temp folder that is deleted afterwards.autotries it first, then the in-process package, then CodeDom. It falls through only when the compiler couldn't run (not found, didn't start, timed out, exited without diagnostics), with a warning in the console. Compile errors are returned as they are.roslynandcodedomare unchanged when asked for.compileracceptscsc, and the tool and parameter descriptions say what each backend is and costs. The "Compiled in-memory — no script files created" line now says nothing is added to the project, since the work files go to the OS temp folder.website/docs/reference/tools/scripting_ext/execute_code.mdwas regenerated.Measured live on 6000.3.14f1 (a Game14-sized project, 266 references),
autowith this change:csc)refresh_unity(compile="request")Line 1: Invalid expression term ';'(Wall time through the MCP round trip; a non-compiling call takes ~0.3 s on the same setup.)
Compatibility / Package Source
file:(this branch'sMCPForUnity) in a Unity 6 projectfile:package)Testing/Screenshots/Recordings
cd Server && uv run pytest tests/ -v): 1591 passed, 3 skippedExecute_CscBackend_CompilesAndRuns,Execute_CscBackend_CompilesModernSyntax(tuple, positional pattern, static local function, switch expression),Execute_CscBackend_CompileErrorReportsUserLineandExecute_Auto_PrefersBundledCsc. TheyAssumethe bundled compiler is present. I couldn't run the test project locally (2021.3.45f2 isn't installed here), so I ran each new test's scenario and every existingExecuteCodeTestssnippet throughExecuteCode.HandleCommandin a 6000.3.14f1 project. All gave the asserted results, including the compile-cache check (the same snippet twice loads no second assembly). CI will be the first real run.Documentation Updates
execute_code'scompileroption and description)tools/UPDATE_DOCS_PROMPT.md(recommended)generate_docs_reference.py; it also rewritesmanage_material.mdandmanage_texture.md, already out of date onbeta, which I left out)Related Issues
Relates to #1498 (same goal: agents shouldn't wait on top of a refresh that has already finished)
Summary by CodeRabbit