From 48b25da1470b8248ebfacd9de4f5312124297fba Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 17 Mar 2025 10:19:15 -0700 Subject: [PATCH] Remove the old fuzzy suggestion and fix the local script file name suggestion (#25177) --- .../FeedbackSubsystem/IFeedbackProvider.cs | 2 +- .../engine/hostifaces/HostUtilities.cs | 37 +++++-------------- .../resources/SuggestionStrings.resx | 3 ++ .../utils/Telemetry.cs | 2 +- test/xUnit/csharp/test_Feedback.cs | 4 +- 5 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs b/src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs index 328eb4eee8..0af184f4e9 100644 --- a/src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs +++ b/src/System.Management.Automation/engine/Subsystem/FeedbackSubsystem/IFeedbackProvider.cs @@ -243,7 +243,7 @@ namespace System.Management.Automation.Subsystem.Feedback public Guid Id => _guid; - public string Name => "general"; + public string Name => "General Feedback"; public string Description => "The built-in general feedback source for command errors."; diff --git a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs index 9e5612189f..003625791b 100644 --- a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs +++ b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs @@ -8,7 +8,6 @@ using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Management.Automation.Host; using System.Management.Automation.Internal; -using System.Management.Automation.Language; using System.Management.Automation.Runspaces; using System.Management.Automation.Subsystem.Feedback; using System.Runtime.InteropServices; @@ -67,13 +66,6 @@ namespace System.Management.Automation $formatString -f $lastError.TargetObject,"".\$($lastError.TargetObject)"" "; - private static readonly string s_getFuzzyMatchedCommands = @" - [System.Diagnostics.DebuggerHidden()] - param([string] $formatString) - - $formatString -f [string]::Join(', ', (Get-Command $lastError.TargetObject -UseFuzzyMatching -FuzzyMinimumDistance 1 | Select-Object -First 5 -Unique -ExpandProperty Name)) - "; - private static readonly List s_suggestions = InitializeSuggestions(); private static bool HostSupportUnicode() @@ -97,28 +89,17 @@ namespace System.Management.Automation private static List InitializeSuggestions() { - var suggestions = new List( - new Hashtable[] - { - NewSuggestion( - id: 3, - category: "General", - matchType: SuggestionMatchType.Dynamic, - rule: ScriptBlock.CreateDelayParsedScriptBlock(s_checkForCommandInCurrentDirectoryScript, isProductCode: true), - suggestion: ScriptBlock.CreateDelayParsedScriptBlock(s_createCommandExistsInCurrentDirectoryScript, isProductCode: true), - suggestionArgs: new object[] { CodeGeneration.EscapeSingleQuotedStringContent(SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory) }, - enabled: true) - }); - - suggestions.Add( + var suggestions = new List() + { NewSuggestion( - id: 4, + id: 3, category: "General", - matchType: SuggestionMatchType.ErrorId, - rule: "CommandNotFoundException", - suggestion: ScriptBlock.CreateDelayParsedScriptBlock(s_getFuzzyMatchedCommands, isProductCode: true), - suggestionArgs: new object[] { CodeGeneration.EscapeSingleQuotedStringContent(SuggestionStrings.Suggestion_CommandNotFound) }, - enabled: true)); + matchType: SuggestionMatchType.Dynamic, + rule: ScriptBlock.CreateDelayParsedScriptBlock(s_checkForCommandInCurrentDirectoryScript, isProductCode: true), + suggestion: ScriptBlock.CreateDelayParsedScriptBlock(s_createCommandExistsInCurrentDirectoryScript, isProductCode: true), + suggestionArgs: new object[] { SuggestionStrings.Suggestion_CommandExistsInCurrentDirectory_Legacy }, + enabled: true) + }; return suggestions; } diff --git a/src/System.Management.Automation/resources/SuggestionStrings.resx b/src/System.Management.Automation/resources/SuggestionStrings.resx index ea249db55e..9e325b2616 100644 --- a/src/System.Management.Automation/resources/SuggestionStrings.resx +++ b/src/System.Management.Automation/resources/SuggestionStrings.resx @@ -123,6 +123,9 @@ PowerShell does not load commands from the current location by default (see 'Get If you trust this command, run the following command instead: + + The command "{0}" was not found, but does exist in the current location. PowerShell does not load commands from the current location by default. If you trust this command, instead type: "{1}". See "get-help about_Command_Precedence" for more details. + The most similar commands are: diff --git a/src/System.Management.Automation/utils/Telemetry.cs b/src/System.Management.Automation/utils/Telemetry.cs index f611c36bf2..13a8d7d5ec 100644 --- a/src/System.Management.Automation/utils/Telemetry.cs +++ b/src/System.Management.Automation/utils/Telemetry.cs @@ -636,7 +636,7 @@ namespace Microsoft.PowerShell.Telemetry s_knownSubsystemNames = new HashSet(StringComparer.OrdinalIgnoreCase) { "Completion", - "general", + "General Feedback", "Windows Package Manager - WinGet", "Az Predictor" }; diff --git a/test/xUnit/csharp/test_Feedback.cs b/test/xUnit/csharp/test_Feedback.cs index 2c90118be5..eba627f7b0 100644 --- a/test/xUnit/csharp/test_Feedback.cs +++ b/test/xUnit/csharp/test_Feedback.cs @@ -97,7 +97,7 @@ namespace PSTests.Sequential // Test the result from the 'general' feedback provider. Assert.Single(feedbacks); - Assert.Equal("general", feedbacks[0].Name); + Assert.Equal("General Feedback", feedbacks[0].Name); Assert.Equal(expectedCmd, feedbacks[0].Item.RecommendedActions[0]); // Expect the result from both 'general' and the 'slow' feedback providers. @@ -107,7 +107,7 @@ namespace PSTests.Sequential Assert.Equal(2, feedbacks.Count); FeedbackResult entry1 = feedbacks[0]; - Assert.Equal("general", entry1.Name); + Assert.Equal("General Feedback", entry1.Name); Assert.Equal(expectedCmd, entry1.Item.RecommendedActions[0]); FeedbackResult entry2 = feedbacks[1];