Skip to content

Fix: Handle ReflectionTypeLoadException when loading assemblies#405

Closed
numandina wants to merge 1 commit intoIvanMurzak:mainfrom
numandina:fix/safe-assembly-introspection
Closed

Fix: Handle ReflectionTypeLoadException when loading assemblies#405
numandina wants to merge 1 commit intoIvanMurzak:mainfrom
numandina:fix/safe-assembly-introspection

Conversation

@numandina
Copy link
Copy Markdown

Plugin crashed during build when my project contained assemblies that reference optional DLLs not present on my machine. Calling GetTypes() on these assemblies throws a ReflectionTypeLoadException.

Added GetSafeAssemblies() method that wraps GetTypes() in a try-catch and skips problematic assemblies with a warning log instead of crashing.

Assemblies with missing dependencies now get skipped instead.

NOTE: This works well for my case which was external DLLs with optional dependencies. But it skips those DLLs entirely. A more sophisticated approach could extract partial types of the DLL if it's relevant.

Assemblies with missing dependencies now get skipped instead of crashing
the MCP plugin build.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 8, 2026 05:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds robust error handling for assembly loading to prevent crashes when Unity projects contain assemblies with missing optional dependencies. The fix implements a defensive filtering approach that logs warnings for problematic assemblies instead of crashing the entire plugin.

Key Changes:

  • Introduced GetSafeAssemblies() method that wraps assembly type loading in try-catch blocks
  • Catches ReflectionTypeLoadException specifically for assemblies with missing dependencies
  • Skips dynamic assemblies which cannot be safely reflected

Comment on lines +104 to +125
protected virtual IEnumerable<Assembly> GetSafeAssemblies()
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.IsDynamic)
continue;

try
{
assembly.GetTypes();
yield return assembly;
}
catch (ReflectionTypeLoadException)
{
_logger.LogWarning("Skipping assembly with missing dependencies: {assembly}", assembly.GetName().Name);
}
catch (Exception ex)
{
_logger.LogWarning("Skipping assembly {assembly}: {error}", assembly.GetName().Name, ex.Message);
}
}
}
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new GetSafeAssemblies() method lacks test coverage. Given that the repository has comprehensive test coverage (including tests for McpPlugin, ConnectionManager, and various tools), this new error handling logic should be tested. Consider adding a test that verifies assemblies with missing dependencies are properly skipped and logged, and that valid assemblies are correctly returned.

Copilot uses AI. Check for mistakes.
Comment thread Unity-MCP-Plugin/Assets/root/Runtime/UnityMcpPlugin.Build.cs
@IvanMurzak
Copy link
Copy Markdown
Owner

Hi @numandina
Optional DLLs? Hm... that is interesting. Your fix will not work because the are other places with the same code that looks into Assemblies. It is a good point. I will take care about it. Thank you for reporting and for trying to fix it.

@numandina
Copy link
Copy Markdown
Author

@IvanMurzak Right, the terminal saw red when executing scripts and skirted around the errors, but I was content with the MCP just building and being able to use simple commands. Good luck and hope to see a fix soon.

As for optional DLLs, my project has one that references other DLLs that don't need to be installed (building construction library looking for platform specific or software specific libraries). The libraries vary by user but the main DLL looks at them all.

GetTypes() tried to load reference AutoCAD APIs I don't have, because I was using another software for the same application.

@IvanMurzak
Copy link
Copy Markdown
Owner

I am working on the fix in this pull request:


You may need to know, your project setup with an optional setup will slowdown AI Game Developer execution. Because it will throw and catch that exceptions hundreds of times per a simpler operation. If you notice a noticeable slowdown, consider to add missed DLLs to resolve it.

@numandina
Copy link
Copy Markdown
Author

@IvanMurzak awesome, thank you. Will gladly test once it's implemented. Currently testing the try catch applied to 3 other scripts (any gettypes call) locally. Looking forward to the caching implementation.

@numandina numandina closed this Jan 8, 2026
@IvanMurzak
Copy link
Copy Markdown
Owner

@numandina the fix was added to AI Game Developer in this pull request:

Thanks again for the report, please await for the next release to try it out. Feel free to chat here if for any reason it won't work anyway.

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.

3 participants