@@ -16,6 +16,10 @@ namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
16
16
/// </summary>
17
17
internal static class CommandHelpers
18
18
{
19
+ public record struct AliasMap (
20
+ Dictionary < string , List < string > > CmdletToAliases ,
21
+ Dictionary < string , string > AliasToCmdlets ) ;
22
+
19
23
private static readonly HashSet < string > s_nounExclusionList = new ( )
20
24
{
21
25
// PowerShellGet v2 nouns
@@ -180,19 +184,21 @@ not CommandTypes.Function and
180
184
/// Gets all aliases found in the runspace
181
185
/// </summary>
182
186
/// <param name="executionService"></param>
183
- public static async Task < ( Dictionary < string , List < string > > , Dictionary < string , string > ) > GetAliasesAsync ( IInternalPowerShellExecutionService executionService )
187
+ /// <param name="cancellationToken"></param>
188
+ public static async Task < AliasMap > GetAliasesAsync (
189
+ IInternalPowerShellExecutionService executionService ,
190
+ CancellationToken cancellationToken = default )
184
191
{
185
192
Validate . IsNotNull ( nameof ( executionService ) , executionService ) ;
186
193
187
- IEnumerable < CommandInfo > aliases = await executionService . ExecuteDelegateAsync (
188
- nameof ( GetAliasesAsync ) ,
189
- executionOptions : null ,
190
- ( pwsh , _ ) =>
191
- {
192
- CommandInvocationIntrinsics invokeCommand = pwsh . Runspace . SessionStateProxy . InvokeCommand ;
193
- return invokeCommand . GetCommands ( "*" , CommandTypes . Alias , nameIsPattern : true ) ;
194
- } ,
195
- CancellationToken . None ) . ConfigureAwait ( false ) ;
194
+ // Need to execute a PSCommand here as Runspace.SessionStateProxy cannot be used from
195
+ // our PSRL on idle handler.
196
+ IReadOnlyList < CommandInfo > aliases = await executionService . ExecutePSCommandAsync < CommandInfo > (
197
+ new PSCommand ( )
198
+ . AddCommand ( "Microsoft.PowerShell.Core\\ Get-Command" )
199
+ . AddParameter ( "ListImported" , true )
200
+ . AddParameter ( "CommandType" , CommandTypes . Alias ) ,
201
+ cancellationToken ) . ConfigureAwait ( false ) ;
196
202
197
203
foreach ( AliasInfo aliasInfo in aliases )
198
204
{
@@ -206,7 +212,8 @@ not CommandTypes.Function and
206
212
s_aliasToCmdletCache . TryAdd ( aliasInfo . Name , aliasInfo . Definition ) ;
207
213
}
208
214
209
- return ( new Dictionary < string , List < string > > ( s_cmdletToAliasCache ) ,
215
+ return new AliasMap (
216
+ new Dictionary < string , List < string > > ( s_cmdletToAliasCache ) ,
210
217
new Dictionary < string , string > ( s_aliasToCmdletCache ) ) ;
211
218
}
212
219
}
0 commit comments