From 11218ea4d88cd4a700e20975611d0b4886139655 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Tue, 26 Aug 2025 10:54:58 -0500 Subject: [PATCH] CmdPal: Make debugging extension load errors easier (#41251) Turns out we didn't log all the HRESULTs for failing to load an extension. This adds more logging. It also adds a context command on the log command to make it easier to get to the log files --- src/common/ManagedCommon/Logger.cs | 29 ++++++++++++------- .../Commands/FallbackLogItem.cs | 5 ++++ .../Models/ExtensionWrapper.cs | 4 +++ .../Properties/Resources.Designer.cs | 9 ++++++ .../Properties/Resources.resx | 3 ++ 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/common/ManagedCommon/Logger.cs b/src/common/ManagedCommon/Logger.cs index 51b42039c1..e926c074f4 100644 --- a/src/common/ManagedCommon/Logger.cs +++ b/src/common/ManagedCommon/Logger.cs @@ -32,17 +32,8 @@ namespace ManagedCommon /// If the process using Logger is a low-privilege process. public static void InitializeLogger(string applicationLogPath, bool isLocalLow = false) { - string basePath; - if (isLocalLow) - { - basePath = Environment.GetEnvironmentVariable("userprofile") + "\\appdata\\LocalLow\\Microsoft\\PowerToys" + applicationLogPath; - } - else - { - basePath = Constants.AppDataPath() + applicationLogPath; - } - - string versionedPath = Path.Combine(basePath, Version); + string versionedPath = LogDirectoryPath(applicationLogPath, isLocalLow); + string basePath = Path.GetDirectoryName(versionedPath); if (!Directory.Exists(versionedPath)) { @@ -59,6 +50,22 @@ namespace ManagedCommon Task.Run(() => DeleteOldVersionLogFolders(basePath, versionedPath)); } + public static string LogDirectoryPath(string applicationLogPath, bool isLocalLow = false) + { + string basePath; + if (isLocalLow) + { + basePath = Environment.GetEnvironmentVariable("userprofile") + "\\appdata\\LocalLow\\Microsoft\\PowerToys" + applicationLogPath; + } + else + { + basePath = Constants.AppDataPath() + applicationLogPath; + } + + string versionedPath = Path.Combine(basePath, Version); + return versionedPath; + } + /// /// Deletes old version log folders, keeping only the current version's folder. /// diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackLogItem.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackLogItem.cs index f5b76484e5..a96d49ff79 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackLogItem.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackLogItem.cs @@ -2,6 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using ManagedCommon; using Microsoft.CmdPal.UI.ViewModels.Commands; using Microsoft.CmdPal.UI.ViewModels.Properties; using Microsoft.CommandPalette.Extensions.Toolkit; @@ -19,6 +20,10 @@ internal sealed partial class FallbackLogItem : FallbackCommandItem Title = string.Empty; _logMessagesPage.Name = string.Empty; Subtitle = Properties.Resources.builtin_log_subtitle; + + var logPath = Logger.LogDirectoryPath("\\CmdPal\\Logs\\"); + var openLogCommand = new OpenFileCommand(logPath) { Name = Resources.builtin_log_folder_command_name }; + MoreCommands = [new CommandContextItem(openLogCommand)]; } public override void UpdateQuery(string query) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Models/ExtensionWrapper.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Models/ExtensionWrapper.cs index 5cab466b6f..32526c6544 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Models/ExtensionWrapper.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Models/ExtensionWrapper.cs @@ -126,6 +126,10 @@ public class ExtensionWrapper : IExtensionWrapper // We'll just return out nothing. return; } + else if (hr.Value != 0) + { + Logger.LogError($"Failed to find {ExtensionDisplayName}: {hr.Value}"); + } // Marshal.ThrowExceptionForHR(hr); _extensionObject = MarshalInterface.FromAbi((nint)extensionPtr); diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.Designer.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.Designer.cs index 1deb9cdf74..6924e48d75 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.Designer.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.Designer.cs @@ -285,6 +285,15 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties { } } + /// + /// Looks up a localized string similar to View log folder. + /// + public static string builtin_log_folder_command_name { + get { + return ResourceManager.GetString("builtin_log_folder_command_name", resourceCulture); + } + } + /// /// Looks up a localized string similar to View log. /// diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.resx b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.resx index 0d341e3981..a69fa5b00e 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.resx +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.resx @@ -135,6 +135,9 @@ View log + + View log folder + Reload Command Palette extensions