From 82e1403722c25efea2d655b261e86d2f3739504e Mon Sep 17 00:00:00 2001 From: labbbirder <502100554@qq.com> Date: Fri, 9 May 2025 18:02:13 +0800 Subject: [PATCH 1/2] opt: improved analyzer collect approach --- .../Editor/Compilation/DotnetExeCompilator.cs | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs b/Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs index 883cfdd..c630774 100644 --- a/Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs +++ b/Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs @@ -8,6 +8,7 @@ using System.Runtime.CompilerServices; using System.Text; using System.Threading; +using System.Xml.Linq; using FastScriptReload.Editor.AssemblyPostProcess; using HarmonyLib; using ImmersiveVRTools.Editor.Common.Cache; @@ -31,8 +32,7 @@ public class DotnetExeDynamicCompilation : DynamicCompilationBase private static readonly List _analyzers = new List(); static DotnetExeDynamicCompilation() { - const string RoslynAnalyzerExtension = ".dll"; - const string RoslynAnalyzerKeyword = "RoslynAnalyzer"; + const string DefaultUnityProjectFilePath = "Assembly-CSharp.csproj"; #if UNITY_EDITOR_WIN const string dotnetExecutablePath = "dotnet.exe"; #else @@ -43,17 +43,26 @@ static DotnetExeDynamicCompilation() _cscDll = FindFileOrThrow("csc.dll"); //even on mac/linux need to find dll and use, not no extension one _tempFolder = Path.GetTempPath(); - foreach (var guid in AssetDatabase.FindAssets("t: " + nameof(DefaultAsset))) + if (File.Exists(DefaultUnityProjectFilePath)) { - var assetPath = AssetDatabase.GUIDToAssetPath(guid); - if (!assetPath.EndsWith(RoslynAnalyzerExtension)) continue; - var asset = AssetDatabase.LoadAssetAtPath(assetPath); - var assetLabels = AssetDatabase.GetLabels(asset); - if (assetLabels.Contains(RoslynAnalyzerKeyword)) + var xmlCSProj = File.ReadAllText(DefaultUnityProjectFilePath); + var xdoc = XDocument.Parse(xmlCSProj); + var analyzers = xdoc.Root.Elements("ItemGroup") + .Elements("Analyzer") + .Select(e => e.Attribute("Include").Value) + .Where(File.Exists) + .ToArray() + ; + + foreach (var analyzer in analyzers) { - _analyzers.Add(Path.GetFullPath(assetPath)); + _analyzers.Add(analyzer); } } + else + { + LoggerScoped.LogWarning($"{DefaultUnityProjectFilePath} does not exists, hence analyzers in project are ignored."); + } EditorApplication.playModeStateChanged += obj => { From efa56bed2c9975fd7805d55c4a1b6a952ab50ea8 Mon Sep 17 00:00:00 2001 From: labbbirder <502100554@qq.com> Date: Mon, 12 May 2025 16:34:49 +0800 Subject: [PATCH 2/2] fix: add essential exception handler in roslyn analyzer collecting --- .../Editor/Compilation/DotnetExeCompilator.cs | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs b/Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs index c630774..4a6967a 100644 --- a/Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs +++ b/Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs @@ -45,18 +45,25 @@ static DotnetExeDynamicCompilation() if (File.Exists(DefaultUnityProjectFilePath)) { - var xmlCSProj = File.ReadAllText(DefaultUnityProjectFilePath); - var xdoc = XDocument.Parse(xmlCSProj); - var analyzers = xdoc.Root.Elements("ItemGroup") - .Elements("Analyzer") - .Select(e => e.Attribute("Include").Value) - .Where(File.Exists) - .ToArray() - ; - - foreach (var analyzer in analyzers) + try { - _analyzers.Add(analyzer); + var xmlCSProj = File.ReadAllText(DefaultUnityProjectFilePath); + var xdoc = XDocument.Parse(xmlCSProj); + var analyzers = xdoc.Root.Elements("ItemGroup") + .Elements("Analyzer") + .Select(e => e.Attribute("Include").Value) + .Where(File.Exists) + .ToArray() + ; + + foreach (var analyzer in analyzers) + { + _analyzers.Add(analyzer); + } + } + catch (Exception e) + { + LoggerScoped.LogWarning($"Unable to collect roslyn analyzers. {e}"); } } else