From c0bb71dacb781910bc6833900b0d173c890cccbf Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 13 Apr 2019 19:46:57 +0100 Subject: [PATCH 1/2] Do not use alias name as key for command info cache to fix the problem where UseCorrectCasing corrects aliases --- Engine/CommandInfoCache.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Engine/CommandInfoCache.cs b/Engine/CommandInfoCache.cs index 20fd211aa..ee5748e5e 100644 --- a/Engine/CommandInfoCache.cs +++ b/Engine/CommandInfoCache.cs @@ -40,9 +40,7 @@ public CommandInfo GetCommandInfo(string commandName, string aliasName = null, C return null; } - // If alias name is given, we store the entry under that, but search with the command name - var key = new CommandLookupKey(aliasName ?? commandName, commandTypes); - + var key = new CommandLookupKey(commandName, commandTypes); // Atomically either use PowerShell to query a command info object, or fetch it from the cache return _commandInfoCache.GetOrAdd(key, new Lazy(() => GetCommandInfoInternal(commandName, commandTypes))).Value; } From 68255cc9f079a45dce0a2be4c6578ba433cec6b7 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 15 Apr 2019 21:28:45 +0100 Subject: [PATCH 2/2] Remove unused aliasName parameter and left-over GetCommandInfoInternal function from refactoring a few weeks ago --- Engine/CommandInfoCache.cs | 5 ++--- Engine/Helper.cs | 27 +-------------------------- 2 files changed, 3 insertions(+), 29 deletions(-) diff --git a/Engine/CommandInfoCache.cs b/Engine/CommandInfoCache.cs index ee5748e5e..042f43cfa 100644 --- a/Engine/CommandInfoCache.cs +++ b/Engine/CommandInfoCache.cs @@ -30,10 +30,9 @@ public CommandInfoCache(Helper pssaHelperInstance) /// Retrieve a command info object about a command. /// /// Name of the command to get a commandinfo object for. - /// The alias of the command to be used in the cache key. If not given, uses the command name. /// What types of command are needed. If omitted, all types are retrieved. /// - public CommandInfo GetCommandInfo(string commandName, string aliasName = null, CommandTypes? commandTypes = null) + public CommandInfo GetCommandInfo(string commandName, CommandTypes? commandTypes = null) { if (string.IsNullOrWhiteSpace(commandName)) { @@ -58,7 +57,7 @@ public CommandInfo GetCommandInfoLegacy(string commandOrAliasName, CommandTypes? return string.IsNullOrEmpty(commandName) ? GetCommandInfo(commandOrAliasName, commandTypes: commandTypes) - : GetCommandInfo(commandName, aliasName: commandOrAliasName, commandTypes: commandTypes); + : GetCommandInfo(commandName, commandTypes: commandTypes); } /// diff --git a/Engine/Helper.cs b/Engine/Helper.cs index a3009a7ef..e51eb94ae 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -402,7 +402,7 @@ public HashSet GetExportedFunction(Ast ast) IEnumerable cmdAsts = ast.FindAll(item => item is CommandAst && exportFunctionsCmdlet.Contains((item as CommandAst).GetCommandName(), StringComparer.OrdinalIgnoreCase), true); - CommandInfo exportMM = Helper.Instance.GetCommandInfoLegacy("export-modulemember", CommandTypes.Cmdlet); + CommandInfo exportMM = Helper.Instance.GetCommandInfo("export-modulemember", CommandTypes.Cmdlet); // switch parameters IEnumerable switchParams = (exportMM != null) ? exportMM.Parameters.Values.Where(pm => pm.SwitchParameter) : Enumerable.Empty(); @@ -661,31 +661,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanTwoPositiona return moreThanTwoPositional ? argumentsWithoutProcedingParameters > 2 : argumentsWithoutProcedingParameters > 0; } - - /// - /// Get a CommandInfo object of the given command name - /// - /// Returns null if command does not exists - private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? commandType) - { - using (var ps = System.Management.Automation.PowerShell.Create()) - { - var psCommand = ps.AddCommand("Get-Command") - .AddParameter("Name", cmdName) - .AddParameter("ErrorAction", "SilentlyContinue"); - - if(commandType!=null) - { - psCommand.AddParameter("CommandType", commandType); - } - - var commandInfo = psCommand.Invoke() - .FirstOrDefault(); - - return commandInfo; - } - } - /// /// Legacy method, new callers should use instead. /// Given a command's name, checks whether it exists. It does not use the passed in CommandTypes parameter, which is a bug.