Skip to content

Commit

Permalink
Do not use alias name as key for command info cache to fix the proble…
Browse files Browse the repository at this point in the history
…m 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
  • Loading branch information
bergmeister authored Apr 16, 2019
1 parent fa5dbe1 commit 823144c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 32 deletions.
9 changes: 3 additions & 6 deletions Engine/CommandInfoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@ public CommandInfoCache(Helper pssaHelperInstance)
/// Retrieve a command info object about a command.
/// </summary>
/// <param name="commandName">Name of the command to get a commandinfo object for.</param>
/// <param name="aliasName">The alias of the command to be used in the cache key. If not given, uses the command name.</param>
/// <param name="commandTypes">What types of command are needed. If omitted, all types are retrieved.</param>
/// <returns></returns>
public CommandInfo GetCommandInfo(string commandName, string aliasName = null, CommandTypes? commandTypes = null)
public CommandInfo GetCommandInfo(string commandName, CommandTypes? commandTypes = null)
{
if (string.IsNullOrWhiteSpace(commandName))
{
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<CommandInfo>(() => GetCommandInfoInternal(commandName, commandTypes))).Value;
}
Expand All @@ -60,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);
}

/// <summary>
Expand Down
27 changes: 1 addition & 26 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public HashSet<string> GetExportedFunction(Ast ast)
IEnumerable<Ast> 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<ParameterMetadata> switchParams = (exportMM != null) ? exportMM.Parameters.Values.Where<ParameterMetadata>(pm => pm.SwitchParameter) : Enumerable.Empty<ParameterMetadata>();
Expand Down Expand Up @@ -661,31 +661,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanTwoPositiona
return moreThanTwoPositional ? argumentsWithoutProcedingParameters > 2 : argumentsWithoutProcedingParameters > 0;
}


/// <summary>
/// Get a CommandInfo object of the given command name
/// </summary>
/// <returns>Returns null if command does not exists</returns>
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<CommandInfo>()
.FirstOrDefault();

return commandInfo;
}
}

/// <summary>
/// Legacy method, new callers should use <see cref="GetCommandInfo"/> instead.
/// Given a command's name, checks whether it exists. It does not use the passed in CommandTypes parameter, which is a bug.
Expand Down

0 comments on commit 823144c

Please sign in to comment.