From 07514511512e3cd45c2c737cb0b393b9ea701f72 Mon Sep 17 00:00:00 2001 From: JoC0de <53140583+JoC0de@users.noreply.github.com> Date: Sun, 5 Jan 2025 21:58:41 +0100 Subject: [PATCH] Ignore the exception when unity is starting Ignore the 'Must set an output directory through SetCompileScriptsOutputDirectory before compiling' error when unity is starting --- .../Editor/UnityPreImportedLibraryResolver.cs | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/NuGetForUnity/Editor/UnityPreImportedLibraryResolver.cs b/src/NuGetForUnity/Editor/UnityPreImportedLibraryResolver.cs index 921b00eb..4adb2fc8 100644 --- a/src/NuGetForUnity/Editor/UnityPreImportedLibraryResolver.cs +++ b/src/NuGetForUnity/Editor/UnityPreImportedLibraryResolver.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using JetBrains.Annotations; @@ -82,8 +83,23 @@ private static HashSet GetAlreadyImportedLibs() #else const AssembliesType assemblyType = AssembliesType.Player; #endif - var projectAssemblies = CompilationPipeline.GetAssemblies(assemblyType) - .Where(playerAssembly => playerAssembly.flags != AssemblyFlags.EditorAssembly); + IEnumerable projectAssemblies; + var failedToGetAssembliesFromUnity = false; + try + { + projectAssemblies = CompilationPipeline.GetAssemblies(assemblyType) + .Where(playerAssembly => playerAssembly.flags != AssemblyFlags.EditorAssembly); + } + catch (Exception exception) + { + // ignore the 'Must set an output directory through SetCompileScriptsOutputDirectory before compiling' + // error that seems to happen if this method is called before unity is started completely + NugetLogger.LogVerbose( + "Failed to get assemblies from the compilation pipeline with error: {0}. We ignore the error and try it the next time.", + exception); + projectAssemblies = Array.Empty(); + failedToGetAssembliesFromUnity = true; + } // Collect all referenced assemblies but exclude all assemblies installed by NuGetForUnity. var projectReferences = projectAssemblies.SelectMany(playerAssembly => playerAssembly.allReferences); @@ -101,9 +117,20 @@ private static HashSet GetAlreadyImportedLibs() // the compiler / language is available by default alreadyImportedLibs.Add("Microsoft.CSharp"); - var editorOnlyAssemblies = CompilationPipeline.GetAssemblies(AssembliesType.Editor) - .Where(assembly => assembly.flags == AssemblyFlags.EditorAssembly) - .ToList(); + IEnumerable editorOnlyAssemblies; + try + { + editorOnlyAssemblies = CompilationPipeline.GetAssemblies(AssembliesType.Editor) + .Where(assembly => assembly.flags == AssemblyFlags.EditorAssembly); + } + catch (Exception exception) + { + NugetLogger.LogVerbose( + "Failed to get assemblies from the compilation pipeline with error: {0}. We ignore the error and try it the next time.", + exception); + editorOnlyAssemblies = Array.Empty(); + } + alreadyImportedEditorOnlyLibraries = new HashSet(); // com.unity.visualscripting uses .net 4.8 so it implicitly has System.CodeDom @@ -116,7 +143,14 @@ private static HashSet GetAlreadyImportedLibs() NugetLogger.LogVerbose("Already imported libs: {0}", string.Join(", ", alreadyImportedLibs)); NugetLogger.LogVerbose("Already imported editor only libraries: {0}", string.Join(", ", alreadyImportedEditorOnlyLibraries)); - return alreadyImportedLibs; + var returnAlreadyImportedLibs = alreadyImportedLibs; + if (failedToGetAssembliesFromUnity) + { + // set to null so it is not cached -> we retry the next time this method is called + alreadyImportedLibs = null; + } + + return returnAlreadyImportedLibs; } } }