Search Unity

Exception at EditorCompilationInterface.EmitExceptionAsError

Discussion in 'Editor & General Support' started by Qbit86, Nov 8, 2018.

  1. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Hi,

    I have an issue on Unity 2018.2.14 (Windows 10 x64), updating from Unity 5.5.0p4 (Android, IL2CPP, .NET 4.x).

    Code (csharp):
    1. InvalidOperationException: Sequence contains no matching element
    2. at System.Linq.Enumerable.Single[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) [0x00070] in <839a3cb835c04d14aeb58d83bb7bc4bd>:0
    3. at UnityEditor.Scripting.ScriptCompilation.EditorCompilation.CompileScripts (UnityEditor.Scripting.ScriptCompilation.EditorScriptCompilationOptions options, UnityEditor.BuildTargetGroup platformGroup, UnityEditor.BuildTarget platform) [0x00054] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilation.cs:759
    4. at UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface+<CompileScripts>c__AnonStorey2.<>m__0 () [0x00017] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:212
    5. at UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface.EmitExceptionAsError[T] (System.Func`1[TResult] func, T returnValue) [0x00003] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:85
    Looks definitely like an error somewhere in
    Editor\Mono\Scripting\ScriptCompilation\EditorCompilation.cs:759

    VCS link from log file:
    Built from '2018.2/release' branch; Version is '2018.2.14f1 (3262fb3b0716) revision 3302139'; Using compiler version '191225831'


    /cc @JoshPeterson
     
  2. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    I see no menu items for our Editor scripts, which were available in Unity 5.5.0p4. Is it connected to mentioned InvalidOperationException?

    /cc @AdrianoVerona_Unity
     
  3. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    The same on Unity 2018.2.15.
     
  4. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    In log file the error follows this line:
    Code (csharp):
    1. - Starting compile Library/ScriptAssemblies/UnityEditor.StandardEvents.dll
     
  5. AdrianoVerona_Unity

    AdrianoVerona_Unity

    Unity Technologies

    Joined:
    Apr 11, 2013
    Posts:
    317
    Most likely it is. Looks like a bug to me. Please file an issue and we'll take a look (most likely it is a bug related to the compilation pipeline /cc @lukaszunity)

    Best
     
  6. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
  7. AdrianoVerona_Unity

    AdrianoVerona_Unity

    Unity Technologies

    Joined:
    Apr 11, 2013
    Posts:
    317
  8. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    I cannot narrow down repro; project contains over 4000 cs-files. But similar bug reproduces in 2017.4.15 too (I'm trying different versions of Unity), slightly different trace this time:
    Code (csharp):
    1. InvalidOperationException: Operation is not valid due to the current state of the object
    2.   at System.Linq.Enumerable.Single[CustomScriptAssembly] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00000] in <filename unknown>:0
    3.   at System.Linq.Enumerable.Single[CustomScriptAssembly] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0
    4.   at UnityEditor.Scripting.ScriptCompilation.EditorCompilation.CompileScripts (EditorScriptCompilationOptions options, BuildTargetGroup platformGroup, BuildTarget platform) [0x00054] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilation.cs:589
    5.   at UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface+<CompileScripts>c__AnonStorey2.<>m__0 () [0x00017] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:177
    6.   at UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface.EmitExceptionAsError[Boolean] (System.Func`1 func, Boolean returnValue) [0x00003] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:75
    7. UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
    8. UnityEngine.DebugLogHandler:LogException(Exception, Object)
    9. UnityEngine.Logger:LogException(Exception, Object)
    10. UnityEngine.Debug:LogException(Exception)
    11. UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:LogException(Exception) (at C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:55)
    12. UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:EmitExceptionAsError(Func`1, Boolean) (at C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:79)
    13. UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:CompileScripts(EditorScriptCompilationOptions, BuildTargetGroup, BuildTarget) (at C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:177)
    I will try to attach log file (150 KLoC)... Failed: “The uploaded file is too large.” Maybe zipped will fit?
     

    Attached Files:

  9. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    What does `EditorCompilation.cs` do in method `CompileScripts()` on line 759 of changeset (65e0713a5949) revision 6676593 of 2018.2/staging branch? Why does it call `.Single()` assuming there is exactly one such element?

    /cc @lukaszunity @JoshPeterson
     
  10. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Ok, here is what dotPeek says:
    Code (CSharp):
    1. public bool CompileScripts(EditorScriptCompilationOptions options, BuildTargetGroup platformGroup, BuildTarget platform)
    2. {
    3.   ScriptAssemblySettings assemblySettings = this.CreateScriptAssemblySettings(platformGroup, platform, options);
    4.   EditorBuildRules.TargetAssembly[] notCompiledTargetAssemblies = (EditorBuildRules.TargetAssembly[]) null;
    5.   bool flag = this.CompileScripts(assemblySettings, EditorCompilation.EditorTempPath, options, ref notCompiledTargetAssemblies);
    6.   if (notCompiledTargetAssemblies != null)
    7.   {
    8.     foreach (EditorBuildRules.TargetAssembly targetAssembly in notCompiledTargetAssemblies)
    9.     {
    10.       EditorCompilation.\u003CCompileScripts\u003Ec__AnonStorey7 scriptsCAnonStorey7 = new EditorCompilation.\u003CCompileScripts\u003Ec__AnonStorey7();
    11.       scriptsCAnonStorey7.targetAssembly = targetAssembly;
    12.       // ISSUE: method pointer
    13.       string str = ((IEnumerable<CustomScriptAssembly>) this.customScriptAssemblies).Single<CustomScriptAssembly>(new Func<CustomScriptAssembly, bool>((object) scriptsCAnonStorey7, __methodptr(\u003C\u003Em__0))).FilePath;
    14.       if (str.StartsWith(this.projectDirectory))
    15.         str = str.Substring(this.projectDirectory.Length);
    16.       Debug.LogWarning((object) string.Format("Script assembly '{0}' has not been compiled. Folder containing assembly definition file '{1}' contains script files for different script languages. Folder must only contain script files for one script language.", (object) scriptsCAnonStorey7.targetAssembly.Filename, (object) str));
    17.     }
    18.   }
    19.   return flag;
    20. }
    where `scriptsCAnonStorey7` is decompiled anonymous closure:
    Code (csharp):
    1. [CompilerGenerated]
    2. private sealed class \u003CCompileScripts\u003Ec__AnonStorey7
    3. {
    4.   internal EditorBuildRules.TargetAssembly targetAssembly;
    5.   public \u003CCompileScripts\u003Ec__AnonStorey7()
    6.   {
    7.     base.\u002Ector();
    8.   }
    9.   internal bool \u003C\u003Em__0(CustomScriptAssembly a)
    10.   {
    11.     return a.Name == AssetPath.GetAssemblyNameWithoutExtension(this.targetAssembly.Filename);
    12.   }
    13. }
    How does it happen that `customScriptAssemblies` contains no or more than one specified assemblies at this point?

    /cc @xoofx @Yury-Habets
     
  11. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    The same in '2018.2.16f1 (39a4ac3d51f6) revision 3777708'. All game objects in scene display “The associated script cannot be loaded”, and Editor reports internal incorrect use of `System.Linq.Enumerable.Single`.
     
  12. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Could you please at least replace `.Single()` call with `.FirstOrDefault()` + null-check?

    Please guys, don't make me feel abandoned in hopeless despair, alone with hostile environment of Unity.

    (Maybe I just should patch UnityEditor.dll with Reflector or smth, I don't know. Looks pretty complicated, and I'm not sure if it is not protected with fingerprints/check sums, at least it is not strong-named.)

    /cc @JoshPeterson
     
  13. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    I've tried to patch UnityEditor.dll with dnSpy, but Unity crashed at startup...
     
  14. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    But at least I've managed to attach with dnSpy to Unity and stop at breakpoint.

    Code (csharp):
    1. public bool CompileScripts(EditorScriptCompilationOptions options, BuildTargetGroup platformGroup, BuildTarget platform)
    2. {
    3.     ScriptAssemblySettings scriptAssemblySettings = this.CreateScriptAssemblySettings(platformGroup, platform, options);
    4.     EditorBuildRules.TargetAssembly[] array = null;
    5.     bool result = this.CompileScripts(scriptAssemblySettings, EditorCompilation.EditorTempPath, options, ref array);
    6.     if (array != null)
    7.     {
    8.         EditorBuildRules.TargetAssembly[] array2 = array;
    9.         for (int i = 0; i < array2.Length; i++)
    10.         {
    11.             EditorBuildRules.TargetAssembly targetAssembly = array2[i];
    12.             CustomScriptAssembly customScriptAssembly = this.customScriptAssemblies.Single((CustomScriptAssembly a) => a.Name == AssetPath.GetAssemblyNameWithoutExtension(targetAssembly.Filename));
    13.             string text = customScriptAssembly.FilePath;
    14.             if (text.StartsWith(this.projectDirectory))
    15.             {
    16.                 text = text.Substring(this.projectDirectory.Length);
    17.             }
    18.             Debug.LogWarning(string.Format("Script assembly '{0}' has not been compiled. Folder containing assembly definition file '{1}' contains script files for different script languages. Folder must only contain script files for one script language.", targetAssembly.Filename, text));
    19.         }
    20.     }
    21.     return result;
    22. }
    At that point content of `array` is:
    Code (csharp):
    1. [0] "Assembly-CSharp-firstpass.dll"
    2. [1] "Assembly-CSharp.dll"
    3. [2] "Assembly-UnityScript-firstpass.dll"
    4. [3] "Assembly-UnityScript.dll"
    5. [4] "Assembly-CSharp-Editor.dll"
    6. [5] "Assembly-CSharp-Editor-firstpass.dll"
    And `customScriptAssemblies` are (`Name` and `FilePath`):
    Code (csharp):
    1. [0] "UnityEditor.StandardEvents" "Packages/com.unity.standardevents/Editor/Editor.asmdef"
    2. [1] "Unity.TextMeshPro.Editor" "Packages/com.unity.textmeshpro/Scripts/Editor/Unity.TextMeshPro.Editor.asmdef"
    3. [2] "Unity.PackageManagerUI.EditorTests" "Packages/com.unity.package-manager-ui/Tests/Editor/Unity.PackageManagerUI.EditorTests.asmdef"
    4. [3] "Unity.TextMeshPro.Tests" "Packages/com.unity.textmeshpro/Tests/Runtime/Unity.TextMeshPro.Tests.asmdef"
    5. [4] "Unity.PackageManagerUI.Editor" "Packages/com.unity.package-manager-ui/Editor/Unity.PackageManagerUI.Editor.asmdef"
    6. [5] "Unity.TextMeshPro.Editor.Tests" "Packages/com.unity.textmeshpro/Tests/Editor/Unity.TextMeshPro.Editor.Tests.asmdef"
    7. [6] "Unity.TextMeshPro" "Packages/com.unity.textmeshpro/Scripts/Runtime/Unity.TextMeshPro.asmdef"
     
  15. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Any news on fixing this bug? Will it be fixed in 2017.4.x branch?
     
  16. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    We need a bug report with a repro project in order to fix this bug.
     
  17. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Bug report is here: https://fogbugz.unity3d.com/default.asp?1100751_nk16uannstjd093k But project itself is 14.3 GB in size and under NDA. I cannot attach this project as repro.

    Isn't having
    1) file name
    2) class name
    3) class method
    4) exact line in cs-source file
    5) branch name and revision id
    enough to locate and fix the issue?

    screenshot-size.png
     
  18. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    We need to know which state lead to the issue, so we can reproduce it in a test and fix it permanently.

    Could you perhaps strip down the project and from there create a smaller test project where the issues reproduces, but does not have any of your NDA code? It looks like it is the setup of scripts and possibly some editor extensions that is causing the issue and not the contents of the scripts themselves.
     
  19. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    I've tried to attach with dnSpy debugger to save some state, like local variables: https://forum.unity.com/threads/exc...ace-emitexceptionaserror.580711/#post-3909103

    I'm not sure if I can narrow down the repro, but if I succeed I'll share the project.

    I'm trying several versions of Unity. At the moment I'm trying old 2017.1.5f1, and have similar (but slightly different) issue:
    Code (csharp):
    1. ArgumentNullException: Argument cannot be null.
    2. Parameter name: source
    3. at System.Linq.Check.SourceAndPredicate (System.Object source, System.Object predicate) [0x00000] in <filename unknown>:0
    4. at System.Linq.Enumerable.Single[CustomScriptAssembly] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0
    5. at UnityEditor.Scripting.ScriptCompilation.EditorCompilation.CompileScripts (EditorScriptCompilationOptions definesOptions, BuildTargetGroup platformGroup, BuildTarget platform) [0x00072] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilation.cs:354
    6. at UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface.CompileScripts (EditorScriptCompilationOptions definesOptions, BuildTargetGroup platformGroup, BuildTarget platform) [0x00009] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:80
    7.  
    8. (Filename: C:/buildslave/unity/build/Editor/Mono/Scripting/ScriptCompilation/EditorCompilation.cs Line: 354)
    My main problem is this one: https://forum.unity.com/threads/unity-fails-to-load-outdated-project.590842/

    Looks like there are some usual issues in source code (obsolete API calls or something like that), but some bug in Unity prevents it from printing actual errors. Visual Studio also doesn't report any errors.

    Even if I introduce intentional error like `#error Test error.` Unity doesn't print this error in Console, just its “
    ArgumentNullException: Argument cannot be null.”
     
  20. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    @lukaszunity Also, thank you very much for responding and supporting me! I appreciate this, now I feel not so lonely in this thread :)
     
  21. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
  22. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    Are you also using the incremental compiler? If so, you could try removing it from your project and see if that fixes the issues.

    The incremental compiler injects it self into the compilation pipeline in Unity using reflection and is only compatible with specific versions of Unity.

    If you are not using the incremental compiler, then your issue is unrelated to the incremental compiler issue. They just happen to emit the same exception.
     
    Qbit86 likes this.
  23. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    No, AFAIK; at least I didn't enable it explicitly.

    Agree, this should be considered unrelated until there are stronger evidences of the opposite.

    Anyway, this is not that kind of exceptions that should leak out from any app. Such exceptions must not even be caught somewhere in the in-app handlers — they should be prevented by fixing logic, enforcing invariants, reconsidering assumptions/asserts.
     
  24. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    Yes, provide a repro for this issue and we will fix the logic :)
     
  25. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    But my inability of providing repro shouldn't be plausible formal reason to not fix the bug, should it?

    Earlier in the thread I've disassembled UnityEngine.dll — but this is not necessary since we have code dumps on GitHub! Let's take a look at selected line:

    https://github.com/Unity-Technologi...g/ScriptCompilation/EditorCompilation.cs#L767

    Calling `.Single()` implies an assumption that there is one and only one element satisfying given predicate.

    Why are you so sure in this? Under which circumstances this condition can be violated? Maybe this assert should be relaxed by calling `.SingleOrDefault()`, or `.First()`, or `.FirstOrDefault()`?
     
    Last edited: Dec 6, 2018
  26. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Repro from scratch: https://mega.nz/#!Ug4wFCZb!6-uo8I9FdovyS_OomVAbGj-pWC2rPnc1ZWLrS5N_FZY
    There are: one empty scene, one trivial script on camera, and bunch of third party managed assemblies. Project created in 2017.2.4f1, then opened in 2017.4.16f1.
    Expected behavior: sensible errors are displayed in case if there are some issues with assets.
    Actual behavior: Nothing Selected instead of script on game object and yellow exclamation. And InvalidOperationException in System.Linq.Enumerable.Single[TSource]
    nothing-selected-2017.4.16.png
     
  27. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    Great, thanks!

    Could you report this through the Unity bug reporter? Then QA can verify the issue and they will forward it to us for fixing.
     
  28. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Last edited by a moderator: Apr 22, 2021
  29. lukaszunity

    lukaszunity

    Administrator

    Joined:
    Jun 11, 2014
    Posts:
    461
    Thank you. QA will verify the issue and you will be kept updated on the issue through the fogbugz case.