Skip to content

Commit 823144c

Browse files
authored
Do not use alias name as key for command info cache to fix the problem where UseCorrectCasing corrects aliases (#1216)
* Do not use alias name as key for command info cache to fix the problem where UseCorrectCasing corrects aliases * Remove unused aliasName parameter and left-over GetCommandInfoInternal function from refactoring a few weeks ago
1 parent fa5dbe1 commit 823144c

File tree

2 files changed

+4
-32
lines changed

2 files changed

+4
-32
lines changed

Engine/CommandInfoCache.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,16 @@ public CommandInfoCache(Helper pssaHelperInstance)
3030
/// Retrieve a command info object about a command.
3131
/// </summary>
3232
/// <param name="commandName">Name of the command to get a commandinfo object for.</param>
33-
/// <param name="aliasName">The alias of the command to be used in the cache key. If not given, uses the command name.</param>
3433
/// <param name="commandTypes">What types of command are needed. If omitted, all types are retrieved.</param>
3534
/// <returns></returns>
36-
public CommandInfo GetCommandInfo(string commandName, string aliasName = null, CommandTypes? commandTypes = null)
35+
public CommandInfo GetCommandInfo(string commandName, CommandTypes? commandTypes = null)
3736
{
3837
if (string.IsNullOrWhiteSpace(commandName))
3938
{
4039
return null;
4140
}
4241

43-
// If alias name is given, we store the entry under that, but search with the command name
44-
var key = new CommandLookupKey(aliasName ?? commandName, commandTypes);
45-
42+
var key = new CommandLookupKey(commandName, commandTypes);
4643
// Atomically either use PowerShell to query a command info object, or fetch it from the cache
4744
return _commandInfoCache.GetOrAdd(key, new Lazy<CommandInfo>(() => GetCommandInfoInternal(commandName, commandTypes))).Value;
4845
}
@@ -60,7 +57,7 @@ public CommandInfo GetCommandInfoLegacy(string commandOrAliasName, CommandTypes?
6057

6158
return string.IsNullOrEmpty(commandName)
6259
? GetCommandInfo(commandOrAliasName, commandTypes: commandTypes)
63-
: GetCommandInfo(commandName, aliasName: commandOrAliasName, commandTypes: commandTypes);
60+
: GetCommandInfo(commandName, commandTypes: commandTypes);
6461
}
6562

6663
/// <summary>

Engine/Helper.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public HashSet<string> GetExportedFunction(Ast ast)
402402
IEnumerable<Ast> cmdAsts = ast.FindAll(item => item is CommandAst
403403
&& exportFunctionsCmdlet.Contains((item as CommandAst).GetCommandName(), StringComparer.OrdinalIgnoreCase), true);
404404

405-
CommandInfo exportMM = Helper.Instance.GetCommandInfoLegacy("export-modulemember", CommandTypes.Cmdlet);
405+
CommandInfo exportMM = Helper.Instance.GetCommandInfo("export-modulemember", CommandTypes.Cmdlet);
406406

407407
// switch parameters
408408
IEnumerable<ParameterMetadata> switchParams = (exportMM != null) ? exportMM.Parameters.Values.Where<ParameterMetadata>(pm => pm.SwitchParameter) : Enumerable.Empty<ParameterMetadata>();
@@ -661,31 +661,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanTwoPositiona
661661
return moreThanTwoPositional ? argumentsWithoutProcedingParameters > 2 : argumentsWithoutProcedingParameters > 0;
662662
}
663663

664-
665-
/// <summary>
666-
/// Get a CommandInfo object of the given command name
667-
/// </summary>
668-
/// <returns>Returns null if command does not exists</returns>
669-
private CommandInfo GetCommandInfoInternal(string cmdName, CommandTypes? commandType)
670-
{
671-
using (var ps = System.Management.Automation.PowerShell.Create())
672-
{
673-
var psCommand = ps.AddCommand("Get-Command")
674-
.AddParameter("Name", cmdName)
675-
.AddParameter("ErrorAction", "SilentlyContinue");
676-
677-
if(commandType!=null)
678-
{
679-
psCommand.AddParameter("CommandType", commandType);
680-
}
681-
682-
var commandInfo = psCommand.Invoke<CommandInfo>()
683-
.FirstOrDefault();
684-
685-
return commandInfo;
686-
}
687-
}
688-
689664
/// <summary>
690665
/// Legacy method, new callers should use <see cref="GetCommandInfo"/> instead.
691666
/// Given a command's name, checks whether it exists. It does not use the passed in CommandTypes parameter, which is a bug.

0 commit comments

Comments
 (0)