From 7f8cb2f7298eab310e8bcdc448f9d33a55ce9aec Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Tue, 28 Nov 2023 11:58:43 +0800 Subject: [PATCH] Fix .NET 8 SDK Artifacts output layout --- .../Views/AvaloniaDesigner.xaml.cs | 25 ++++++++++++++++--- .../AvaloniaCompilationAssemblyProvider.cs | 14 ++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/AvaloniaVS.Shared/Views/AvaloniaDesigner.xaml.cs b/AvaloniaVS.Shared/Views/AvaloniaDesigner.xaml.cs index 748de54e..06973bae 100644 --- a/AvaloniaVS.Shared/Views/AvaloniaDesigner.xaml.cs +++ b/AvaloniaVS.Shared/Views/AvaloniaDesigner.xaml.cs @@ -519,6 +519,26 @@ private async Task StartProcessAsync() Log.Logger.Verbose("Finished AvaloniaDesigner.StartProcessAsync()"); } + private string GetIntermediateOutputPath(IVsBuildPropertyStorage storage) + { + // .NET 8 SDK Artifacts output layout + // https://learn.microsoft.com/en-us/dotnet/core/sdk/artifacts-output + // Example + // MSBuildProjectDirectory: X:\abcd\src\Mobius.Windows\ + // IntermediateOutputPath: X:\abcd\src\artifacts\obj\Mobius.Windows\debug_net8.0-windows10.0.19041.0 + + var intermediateOutputPath = GetMSBuildProperty("IntermediateOutputPath", storage); + if (Path.IsPathRooted(intermediateOutputPath)) + { + return Path.Combine(intermediateOutputPath, "Avalonia", "references"); + } + else + { + var projDir = GetMSBuildProperty("MSBuildProjectDirectory", storage); + return Path.Combine(projDir, intermediateOutputPath.TrimStart(Path.DirectorySeparatorChar), "Avalonia", "references"); + } + } + private void RebuildMetadata(string assemblyPath) { assemblyPath ??= SelectedTarget?.XamlAssembly; @@ -531,8 +551,7 @@ private void RebuildMetadata(string assemblyPath) () => new XamlBufferMetadata()); buffer.Properties["AssemblyName"] = Path.GetFileNameWithoutExtension(assemblyPath); var storage = GetMSBuildPropertyStorage(SelectedTarget.Project); - string intermediateOutputPath = GetMSBuildProperty("MSBuildProjectDirectory", storage) + "\\" - + GetMSBuildProperty("IntermediateOutputPath", storage) + "Avalonia\\references"; + string intermediateOutputPath = GetIntermediateOutputPath(storage); if (metadata.CompletionMetadata == null || metadata.NeedInvalidation) { CreateCompletionMetadataAsync(intermediateOutputPath, metadata).FireAndForget(); @@ -556,7 +575,7 @@ private static async Task CreateCompletionMetadataAsync( dte.Events.BuildEvents.OnBuildBegin += (s, e) => _metadataCache.Clear(); } - Log.Logger.Verbose("Started AvaloniaDesigner.CreateCompletionMetadataAsync() for {ExecutablePath}", intermediateOutputPath); + Log.Logger.Information("Started AvaloniaDesigner.CreateCompletionMetadataAsync() for {ExecutablePath}", intermediateOutputPath); try { diff --git a/CompletionEngine/Avalonia.Ide.CompletionEngine/AssemblyMetadata/AvaloniaCompilationAssemblyProvider.cs b/CompletionEngine/Avalonia.Ide.CompletionEngine/AssemblyMetadata/AvaloniaCompilationAssemblyProvider.cs index 9f51b61d..4e1c129e 100644 --- a/CompletionEngine/Avalonia.Ide.CompletionEngine/AssemblyMetadata/AvaloniaCompilationAssemblyProvider.cs +++ b/CompletionEngine/Avalonia.Ide.CompletionEngine/AssemblyMetadata/AvaloniaCompilationAssemblyProvider.cs @@ -17,7 +17,19 @@ public AvaloniaCompilationAssemblyProvider(string path) public IEnumerable GetAssemblies() { - return File.ReadAllText(_path).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + try + { + return File.ReadAllText(_path).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + } + catch (Exception ex) when + (ex is DirectoryNotFoundException || ex is FileNotFoundException) + { + return Array.Empty(); + } + catch (Exception ex) + { + throw new IOException($"Failed to read file '{_path}'.", ex); + } } } }