Exception at EditorCompilationInterface.EmitExceptionAsError

Hi,

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

InvalidOperationException: Sequence contains no matching element
at System.Linq.Enumerable.Single[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) [0x00070] in <839a3cb835c04d14aeb58d83bb7bc4bd>:0
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
at UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface+<CompileScripts>c__AnonStorey2.<>m__0 () [0x00017] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:212
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

I see no menu items for our Editor scripts, which were available in Unity 5.5.0p4. Is it connected to mentioned InvalidOperationException?

/cc @Vagaus

The same on Unity 2018.2.15.

In log file the error follows this line:

- Starting compile Library/ScriptAssemblies/UnityEditor.StandardEvents.dll

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

https://fogbugz.unity3d.com/default.asp?1100751_nk16uannstjd093k

Thanks, but a repro case would be really helpful in order to identify the root problem.

Adriano

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:

InvalidOperationException: Operation is not valid due to the current state of the object
  at System.Linq.Enumerable.Single[CustomScriptAssembly] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00000] in <filename unknown>:0
  at System.Linq.Enumerable.Single[CustomScriptAssembly] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0
  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
  at UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface+<CompileScripts>c__AnonStorey2.<>m__0 () [0x00017] in C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:177
  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
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:LogException(Exception) (at C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:55)
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:EmitExceptionAsError(Func`1, Boolean) (at C:\buildslave\unity\build\Editor\Mono\Scripting\ScriptCompilation\EditorCompilationInterface.cs:79)
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?

3896191–331519–u-2017.4.15-Editor.log.zip (3.32 MB)

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

Ok, here is what dotPeek says:

public bool CompileScripts(EditorScriptCompilationOptions options, BuildTargetGroup platformGroup, BuildTarget platform)
{
  ScriptAssemblySettings assemblySettings = this.CreateScriptAssemblySettings(platformGroup, platform, options);
  EditorBuildRules.TargetAssembly[] notCompiledTargetAssemblies = (EditorBuildRules.TargetAssembly[]) null;
  bool flag = this.CompileScripts(assemblySettings, EditorCompilation.EditorTempPath, options, ref notCompiledTargetAssemblies);
  if (notCompiledTargetAssemblies != null)
  {
    foreach (EditorBuildRules.TargetAssembly targetAssembly in notCompiledTargetAssemblies)
    {
      EditorCompilation.\u003CCompileScripts\u003Ec__AnonStorey7 scriptsCAnonStorey7 = new EditorCompilation.\u003CCompileScripts\u003Ec__AnonStorey7();
      scriptsCAnonStorey7.targetAssembly = targetAssembly;
      // ISSUE: method pointer
      string str = ((IEnumerable<CustomScriptAssembly>) this.customScriptAssemblies).Single<CustomScriptAssembly>(new Func<CustomScriptAssembly, bool>((object) scriptsCAnonStorey7, __methodptr(\u003C\u003Em__0))).FilePath;
      if (str.StartsWith(this.projectDirectory))
        str = str.Substring(this.projectDirectory.Length);
      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));
    }
  }
  return flag;
}

where scriptsCAnonStorey7 is decompiled anonymous closure:

[CompilerGenerated]
private sealed class \u003CCompileScripts\u003Ec__AnonStorey7
{
  internal EditorBuildRules.TargetAssembly targetAssembly;
  public \u003CCompileScripts\u003Ec__AnonStorey7()
  {
    base.\u002Ector();
  }
  internal bool \u003C\u003Em__0(CustomScriptAssembly a)
  {
    return a.Name == AssetPath.GetAssemblyNameWithoutExtension(this.targetAssembly.Filename);
  }
}

How does it happen that customScriptAssemblies contains no or more than one specified assemblies at this point?

/cc @xoofx @Yury-Habets

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.

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

I’ve tried to patch UnityEditor.dll with dnSpy, but Unity crashed at startup…

But at least I’ve managed to attach with dnSpy to Unity and stop at breakpoint.

public bool CompileScripts(EditorScriptCompilationOptions options, BuildTargetGroup platformGroup, BuildTarget platform)
{
    ScriptAssemblySettings scriptAssemblySettings = this.CreateScriptAssemblySettings(platformGroup, platform, options);
    EditorBuildRules.TargetAssembly[] array = null;
    bool result = this.CompileScripts(scriptAssemblySettings, EditorCompilation.EditorTempPath, options, ref array);
    if (array != null)
    {
        EditorBuildRules.TargetAssembly[] array2 = array;
        for (int i = 0; i < array2.Length; i++)
        {
            EditorBuildRules.TargetAssembly targetAssembly = array2[i];
            CustomScriptAssembly customScriptAssembly = this.customScriptAssemblies.Single((CustomScriptAssembly a) => a.Name == AssetPath.GetAssemblyNameWithoutExtension(targetAssembly.Filename));
            string text = customScriptAssembly.FilePath;
            if (text.StartsWith(this.projectDirectory))
            {
                text = text.Substring(this.projectDirectory.Length);
            }
            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));
        }
    }
    return result;
}

At that point content of array is:

[0] "Assembly-CSharp-firstpass.dll"
[1] "Assembly-CSharp.dll"
[2] "Assembly-UnityScript-firstpass.dll"
[3] "Assembly-UnityScript.dll"
[4] "Assembly-CSharp-Editor.dll"
[5] "Assembly-CSharp-Editor-firstpass.dll"

And customScriptAssemblies are (Name and FilePath):

[0] "UnityEditor.StandardEvents" "Packages/com.unity.standardevents/Editor/Editor.asmdef"
[1] "Unity.TextMeshPro.Editor" "Packages/com.unity.textmeshpro/Scripts/Editor/Unity.TextMeshPro.Editor.asmdef"
[2] "Unity.PackageManagerUI.EditorTests" "Packages/com.unity.package-manager-ui/Tests/Editor/Unity.PackageManagerUI.EditorTests.asmdef"
[3] "Unity.TextMeshPro.Tests" "Packages/com.unity.textmeshpro/Tests/Runtime/Unity.TextMeshPro.Tests.asmdef"
[4] "Unity.PackageManagerUI.Editor" "Packages/com.unity.package-manager-ui/Editor/Unity.PackageManagerUI.Editor.asmdef"
[5] "Unity.TextMeshPro.Editor.Tests" "Packages/com.unity.textmeshpro/Tests/Editor/Unity.TextMeshPro.Editor.Tests.asmdef"
[6] "Unity.TextMeshPro" "Packages/com.unity.textmeshpro/Scripts/Runtime/Unity.TextMeshPro.asmdef"

Any news on fixing this bug? Will it be fixed in 2017.4.x branch?

We need a bug report with a repro project in order to fix this bug.

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?

3963073--339934--screenshot-size.png

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.

I’ve tried to attach with dnSpy debugger to save some state, like local variables: https://discussions.unity.com/t/720956/14

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:

ArgumentNullException: Argument cannot be null.
Parameter name: source
at System.Linq.Check.SourceAndPredicate (System.Object source, System.Object predicate) [0x00000] in <filename unknown>:0
at System.Linq.Enumerable.Single[CustomScriptAssembly] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0
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
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

(Filename: C:/buildslave/unity/build/Editor/Mono/Scripting/ScriptCompilation/EditorCompilation.cs Line: 354)

My main problem is this one: https://discussions.unity.com/t/723381

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.ā€

@lukaszunity Also, thank you very much for responding and supporting me! I appreciate this, now I feel not so lonely in this thread :slight_smile: