fix(execute_code): route CodeDom references through a response file (#1144)#1170
Conversation
…oplayDev#1144) `CodeDomCompile` in `MCPForUnity/Editor/Tools/ExecuteCode.cs` pushed every filtered assembly path into `CompilerParameters.ReferencedAssemblies`. Mono's `CSharpCodeCompiler.BuildArgs` (verified at mcs/class/System/Microsoft.CSharp/CSharpCodeCompiler.cs:388-392) turns each reference into a literal `/r:"<absolute_path>"` flag and concatenates them inline on the `mono.exe csc ...` command line. Projects with ~100+ asmdefs (a perfectly normal large Unity project) overflow Windows' 32 KB CreateProcess argument limit and `Process.Start` throws Win32 `ERROR_FILENAME_EXCED_RANGE`, which Mono surfaces as: SystemException: Error running …mono.exe: The filename or extension is too long. …exactly the failure reported in CoplayDev#1144 at ExecuteCode.cs:115 on Windows 10/11 + Unity 2022.3.62f2 + MCP for Unity 9.6.9-beta.8. Fix: write all `/r:"…"` lines to a GUID-named temp response file and pass `@"<path>"` via `CompilerParameters.CompilerOptions` (which `BuildArgs` appends verbatim, confirmed at line 396 of the same Mono source). One short argument regardless of reference count — the 32 KB ceiling is no longer reachable. Both legacy mcs and Roslyn csc accept `@responsefile`, so the change is cross-platform: macOS/Linux Mono behaves identically, and the path doesn't have a 32 KB limit there to begin with. Response file is cleaned up in a `finally` block (best-effort; OS reaps temp on its own otherwise). Tests: added two EditMode regression tests in `ExecuteCodeTests.cs` that exercise the codedom backend end-to-end: - `Execute_CodedomBackend_CompilesAndRuns` — basic compile + execute. - `Execute_CodedomBackend_ResolvesUnityTypes` — verifies Unity references resolve through the response file. Could not reproduce the exact 32 KB failure on macOS (Mono on POSIX doesn't hit the limit), but the response-file path is the only path in the new code, so the fix is mechanically equivalent for any reference-set size on any platform. Sources: - Mono CSharpCodeCompiler.cs — BuildArgs converts ReferencedAssemblies to `/r:"…"` inline and appends CompilerOptions verbatim. - C# compiler — ResponseFiles option (`@responsefile` syntax).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughCodeDOM compiler backend now writes assembly references to a temporary response file instead of passing them as inline command-line arguments, avoiding Windows process argument length limits on projects with many assemblies. Regression tests verify successful codedom compilation and Unity API resolution. ChangesCodeDOM Windows Argument Limit Fix
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Pull request overview
Fixes a Windows-specific failure in the execute_code tool when using the CodeDom backend in large Unity projects by moving long /r: reference lists off the process command line and into a temporary @responsefile.
Changes:
- Update CodeDom compilation to write assembly references to a temp
.rspfile and pass it viaCompilerParameters.CompilerOptions. - Add EditMode regression tests verifying the CodeDom backend can compile/execute and resolve Unity types.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
MCPForUnity/Editor/Tools/ExecuteCode.cs |
Routes CodeDom assembly references through a temp response file to avoid Windows command-line length limits. |
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs |
Adds regression coverage for the CodeDom execution path and Unity-type resolution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| using (var writer = new StreamWriter(responseFilePath, append: false, Encoding.UTF8)) | ||
| { | ||
| GenerateInMemory = true, | ||
| GenerateExecutable = false, | ||
| TreatWarningsAsErrors = false, | ||
| }; | ||
| foreach (var path in filtered) | ||
| { | ||
| writer.Write("/r:\""); |
Summary
Fixes #1144 —
execute_codewithcompiler=\"codedom\"fails on Windows in large projects with:Root cause
CodeDomCompileshoves every entry fromGetAssemblyPaths()intoCompilerParameters.ReferencedAssemblies. Mono'sCSharpCodeCompiler.BuildArgs(mcs source, lines 388-392) turns each reference into a literal/r:\"<absolute_path>\"flag and concatenates them inline on themono.exe csc …command line. Projects with ~100+ asmdefs (very common in large Unity projects) overflow Windows' 32 KBCreateProcessargument limit,Process.StartraisesERROR_FILENAME_EXCED_RANGE, and Mono rethrows it asSystemException.Reporter's repro: Windows 10/11, Unity 2022.3.62f2, MCP for Unity 9.6.9-beta.8, project with 100+ asmdefs.
Fix
Write all
/r:\"…\"lines to a GUID-named temp response file underPath.GetTempPath(), then pass@\"<path>\"viaCompilerParameters.CompilerOptions.BuildArgs(line 396 of the same Mono source) appendsCompilerOptionsverbatim to the csc invocation, so the inline reference flags are replaced by a single short argument regardless of reference count.Both legacy mcs and Roslyn csc support
@responsefilesyntax (it's standard C# compiler behavior, documented here). So the change is:finally(best-effort; OS reapsTempon its own otherwise).Rejected alternatives
CSharpCodeProviderhas no API for it; would require building a chunking layer.Tests
Two new EditMode regression tests in
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs, scoped to the codedom backend:Execute_CodedomBackend_CompilesAndRuns— basic compile + execute, asserts result +compiler == \"codedom\".Execute_CodedomBackend_ResolvesUnityTypes— verifies Unity references resolve through the response file by callingUnityEngine.Application.unityVersion.What I couldn't verify
Process.Startdoesn't hit the limit. The response-file path is mechanically equivalent regardless of reference count, but a Windows + ~100-asmdef smoke test from a reviewer (e.g. the reporter @lgarczyn) would confirm end-to-end.tools/check-unity-versions.shreports all 4 as skipped.Test plan
safe-to-test+full-matrixso the Unity test suite fans out across 2021.3.45f2 + 2022.3.62f1 + 6000.0.75f1 + 6000.4.8f1.Related
Summary by CodeRabbit
Bug Fixes
Tests