Skip to content

Fix execute_code failing on the BOM-only CodeDom error#1192

Open
lgarczyn wants to merge 1 commit into
CoplayDev:betafrom
lgarczyn:fix/execute-code-bom-compile
Open

Fix execute_code failing on the BOM-only CodeDom error#1192
lgarczyn wants to merge 1 commit into
CoplayDev:betafrom
lgarczyn:fix/execute-code-bom-compile

Conversation

@lgarczyn
Copy link
Copy Markdown

@lgarczyn lgarczyn commented Jun 7, 2026

Description

execute_code intermittently fails with a spurious "Line 1: " compilation error
even when the submitted code is valid. This fixes that false negative.

On Mono, mcs emits a stray UTF-8 BOM line on stdout. CodeDom can't parse it, so it
fabricates an error that has no error number and whose text is just the BOM, then refuses
to return the compiled assembly — even though mcs exited 0 and wrote the DLL. Users see
this as a spurious "Line 1: " compilation failure (#1186).

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • Test update

Changes Made

  • Compile to a temp DLL on disk (GenerateInMemory = false + OutputAssembly) instead of in-memory.
  • Skip the content-less BOM "error" CodeDom fabricates (no error number, text is just the
    BOM/whitespace) while still surfacing every real compiler error exactly as before.
  • Assembly.Load the DLL that mcs actually produced, and clean the temp file up in a finally block.

Stripping the BOM from the input (as suggested in #1186) doesn't help, because the BOM comes
from the compiler's own stdout, not the source. Going through a real output assembly lets us tell
"the compiler emitted junk on stdout" apart from "the code didn't compile": if there are no real
errors and a DLL exists on disk, we load it.

Compatibility / Package Source

  • Unity version(s) tested: 2022.3 (Mono backend)
  • Package source used (#beta, #main, tag, branch, or file:): file: (local package)
  • Resolved commit hash from Packages/packages-lock.json (if using a Git package URL): N/A (local file: package)

Testing/Screenshots/Recordings

  • Python tests (cd Server && uv run pytest tests/ -v)
  • Unity EditMode tests
  • Unity PlayMode tests
  • Package import/compile check
  • Not applicable (explain why in Additional Notes)

Documentation Updates

  • I have added/removed/modified tools or resources
  • If yes, I have updated all documentation files using:
    • The LLM prompt at tools/UPDATE_DOCS_PROMPT.md (recommended)
    • Manual review of the generated changes

Related Issues

Fixes #1186

Additional Notes

Single-file change (MCPForUnity/Editor/Tools/ExecuteCode.cs), no tool/resource surface changes, so no docs update needed.

Not unit-tested in CI: the failure depends on the host Mono mcs emitting a BOM on stdout, which
isn't reproducible from a pure EditMode test. Verified manually — reproduced #1186 (valid snippet
returning "Line 1: "), confirmed it now executes, and confirmed genuinely broken code still
reports the real compiler errors.

Summary by CodeRabbit

  • Bug Fixes
    • Improved code compilation reliability by filtering spurious compiler errors and verifying build success, resulting in more accurate error reporting and enhanced stability of the code execution system.

On Mono, mcs emits a stray UTF-8 BOM line on stdout. CodeDom can't parse
it, so it fabricates an error that has no error number and whose text is
just the BOM, then refuses to return the compiled assembly — even though
mcs exited 0 and wrote the DLL. Users see this as a spurious
"Line 1: " compilation failure (CoplayDev#1186).

Compile to a temp DLL on disk instead of in-memory, skip the
content-less BOM "error" while still surfacing real compiler errors, and
Assembly.Load the DLL that mcs actually produced. The temp DLL is cleaned
up in a finally block.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 7, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 68fbbbf0-5c18-4200-b08b-c3383813f27b

📥 Commits

Reviewing files that changed from the base of the PR and between c0908b8 and 31d7f92.

📒 Files selected for processing (1)
  • MCPForUnity/Editor/Tools/ExecuteCode.cs

📝 Walkthrough

Walkthrough

The CodeDomCompile method is updated to compile C# code into a temporary on-disk DLL instead of in-memory. A Mono CodeDom spurious "error" filter is added to ignore empty error entries (BOM/whitespace-related), allowing only real compilation errors to fail the build. The output DLL is verified to exist, and cleanup deletes the temporary file.

Changes

CodeDom Disk-Based Compilation

Layer / File(s) Summary
Disk-based DLL compilation with spurious error filtering
MCPForUnity/Editor/Tools/ExecuteCode.cs
Compilation configuration switches from GenerateInMemory = true to GenerateInMemory = false with an explicit OutputAssembly path. Error filtering skips Mono CodeDom's spurious "BOM/whitespace" entries (where ErrorNumber is empty and trimmed ErrorText is empty), accumulates only real errors, and loads the DLL from disk after compilation. Output DLL file existence is verified, and the temporary file is deleted in a finally block.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • CoplayDev/unity-mcp#1170: Also modifies the CodeDomCompile flow in ExecuteCode.cs, handling referenced assemblies via a response file that interacts with the new disk-based compilation path.

Suggested labels

safe-to-test, full-matrix

Poem

🐰 A temp DLL file, born then gone,
No more BOM ghosts to lead us on!
Mono's spurious whispers now ignored,
Real errors shine—the truth restored.
Hoppy hops

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: fixing execute_code failing on spurious BOM-only CodeDom errors.
Description check ✅ Passed The PR description is well-structured, follows the template, and includes all required sections with thorough details and rationale.
Linked Issues check ✅ Passed The PR fully addresses the objective in #1186 by implementing a solution that prevents spurious BOM compilation errors and allows valid code to execute successfully.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the BOM error issue; only ExecuteCode.cs was modified with no unrelated alterations.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: execute_code tool fails with BOM character (U+FEFF) in code parameter

1 participant