Search Unity

Bug CSharpCodeProvider.CompileAssemblyFromFile => Error running mcs: Cannot find the specified file

Discussion in 'Linux' started by kiriri, May 4, 2018.

  1. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    Hi,
    Ubuntu 16.04. Blank install.
    Unity 2018.1.0f2

    Code that uses CSharpCodeProvider to compile code at runtime into an assembly cannot find mcs. Installing mono 4.5 will cause that mcs to run, instead of the one that ships with Unity :
    upload_2018-5-4_21-24-55.png
    (as you can see, mcs IS shipped with Unity)

    However, the mcs that ships with 4.5 does not seem to work with Unity.

    I tested this only in editor.

    Code :
    Code (CSharp):
    1. using System;
    2. using System.CodeDom.Compiler;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.IO;
    6. using System.Linq;
    7. using System.Reflection;
    8. using System.Text;
    9. using System.Xml;
    10. using System.Xml.Serialization;
    11. using Microsoft.CSharp;
    12. using UnityEngine;
    13.  
    14. public class MCS_Test : MonoBehaviour {
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         LoadModAssembly();
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.        
    24.     }
    25.  
    26.     public static Assembly LoadModAssembly()
    27.         {
    28.             var param = new CompilerParameters();
    29.            
    30.             // load all Unity assemblies so any custom script imports will work.
    31.             foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(p => !p.IsDynamic))
    32.             {
    33.             param.ReferencedAssemblies.Add(assembly.Location);
    34.             }
    35.  
    36.             // Generate a dll in memory
    37.             param.GenerateExecutable = false;
    38.             param.GenerateInMemory = true;
    39.  
    40.             DirectoryInfo dir = new DirectoryInfo(Application.streamingAssetsPath);
    41.             var files = dir.GetFiles("*.cs", SearchOption.AllDirectories);
    42.             var filesS = files.Select(f => f.FullName);
    43.            
    44.             // Print out the script files that are supposed to be loaded.
    45.             foreach (var VARIABLE in filesS)
    46.             {
    47.                 Debug.Log("Loading file : " + VARIABLE);
    48.             }
    49.            
    50.             CSharpCodeProvider provider = new CSharpCodeProvider();
    51.             CompilerResults results = provider.CompileAssemblyFromFile(param, filesS.ToArray());
    52.        
    53.             // check if there were any errors
    54.             if (results.Errors.Count > 0) {
    55.                 var msg = new StringBuilder();
    56.                 foreach (CompilerError error in results.Errors) {
    57.                     msg.AppendFormat("Error ({0}): {1}\n",
    58.                         error.ErrorNumber, error.ErrorText);
    59.                 }
    60.                 throw new Exception(msg.ToString());
    61.             }
    62.            
    63.             return results.CompiledAssembly;
    64.         }
    65. }
    Full Error stack trace :
    I made a test project and attached it.
     

    Attached Files:

    Last edited: May 5, 2018