fix: deduplicate CodeDom assembly references - #1300
Conversation
📝 WalkthroughWalkthroughCodeDom assembly filtering now caches filtered paths, deduplicates assembly identities, selects referenced or newer versions, and preserves unresolved paths. EditMode tests cover security assembly preservation, duplicate selection, and cache invalidation. ChangesCodeDom assembly filtering
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
|
Hi, thanks for the PR and identify the issue. One thing before merge: is it possible to also cache the CodeDomPaths instead of calling getAseemblyName on every path on every execute_code call? Just thinking on ways to optimize this, lmk what you think. |
|
Nice catch on the root cause, and the version-preference tests are the right shape. One thing before merge: Guard the memo with Also, the red CI isn't yours: |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs (1)
436-444: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winUse
cachedCodeDomAssemblyPathsFieldto assert cache state directly.
cachedCodeDomAssemblyPathsFieldis retrieved and null-checked at Line 443 but never read afterward. Adding direct assertions on this field strengthens the regression coverage that the PR's caching fix was meant to exercise: confirm it isnullright afterOnDomainReload(Line 448 and Line 463), and confirm it holdsfirstafter the initial call (Line 455) and after the cache-hit call (Line 460), instead of only inferring cache state indirectly throughReferenceEqualson the return value.♻️ Example of strengthening the assertions
var first = ExecuteCode.FilterAssemblyPathsForCodeDom(cachedAssemblyPaths); Assert.AreEqual(1, first.Length); + Assert.AreSame(first, cachedCodeDomAssemblyPathsField.GetValue(null)); File.WriteAllText(olderPath, "invalidated"); File.WriteAllText(newerPath, "invalidated"); var second = ExecuteCode.FilterAssemblyPathsForCodeDom(cachedAssemblyPaths); Assert.AreSame(first, second); onDomainReload.Invoke(null, null); + Assert.IsNull(cachedCodeDomAssemblyPathsField.GetValue(null)); cachedAssemblyPathsField.SetValue(null, cachedAssemblyPaths);🤖 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 `@TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs` around lines 436 - 444, Update the OnDomainReload test flow in ExecuteCodeTests to assert cachedCodeDomAssemblyPathsField.GetValue(null) is null immediately after each domain reload, equals first after the initial cache population, and still equals first after the cache-hit call. Keep the existing return-value assertions while adding these direct cache-state checks.
🤖 Prompt for all review comments with 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.
Nitpick comments:
In `@TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs`:
- Around line 436-444: Update the OnDomainReload test flow in ExecuteCodeTests
to assert cachedCodeDomAssemblyPathsField.GetValue(null) is null immediately
after each domain reload, equals first after the initial cache population, and
still equals first after the cache-hit call. Keep the existing return-value
assertions while adding these direct cache-state checks.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 8c5b53d0-97ce-42b7-a552-317a01f2e874
📒 Files selected for processing (2)
MCPForUnity/Editor/Tools/ExecuteCode.csTestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ExecuteCodeTests.cs
🚧 Files skipped from review as they are similar to previous changes (1)
- MCPForUnity/Editor/Tools/ExecuteCode.cs
Description
Fix CodeDom dynamic compilation failures when Unity has multiple loaded assemblies with the same simple name. The observed failure involved
System.Security2.0.0.0 and 4.0.0.0: excludingSystem.Securityentirely avoided the duplicate-reference error but made types defined only in that assembly unavailable.Type of Change
Changes Made
System.Securityinstead of removing it from CodeDom references.Compatibility / Package Source
#beta,#main, tag, branch, orfile:): local file checkout based onCoplayDev/unity-mcp#betaatfc70dda7Packages/packages-lock.json(if using a Git package URL): not applicable (file:checkout)Testing/Screenshots/Recordings
cd Server && uv run pytest tests/ -v)Validation in the active Unity 2022.3 project:
ExecuteCodeorerror CSConsole entries.System.Security, Version=4.0.0.0from simultaneous 2.0.0.0 and 4.0.0.0 inputs.System.Security.Cryptography.ProtectedDatafromSystem.Security, Version=4.0.0.0.System.Security.dllwithnetstandardpresent and selected referenced version 1.0.0.0 over unreferenced version 2.0.0.0.Documentation Updates
tools/UPDATE_DOCS_PROMPT.md(recommended)No tool or resource surface changed.
Related Issues
None.
Additional Notes
The two Unity EditMode regression tests were added to
ExecuteCodeTests.cs, but the standaloneUnityMCPTestsproject was not opened or run locally. The active host project was used for package compilation and equivalent runtime checks.Summary by CodeRabbit
Bug Fixes
Tests