Skip to content

Commit d371cc5

Browse files
No warning about module being imported (#1258)
* try this * Manually add commands * add comment about extension * patrick's feedback * misc cosmetic changes
1 parent 56e0e71 commit d371cc5

File tree

7 files changed

+75
-15
lines changed

7 files changed

+75
-15
lines changed

module/PowerShellEditorServices/PowerShellEditorServices.psd1

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ Copyright = '(c) 2017 Microsoft. All rights reserved.'
7676
FunctionsToExport = @()
7777

7878
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
79-
CmdletsToExport = @(
80-
'Start-EditorServices',
81-
'__Invoke-ReadLineForEditorServices',
82-
'__Invoke-ReadLineConstructor'
83-
)
79+
CmdletsToExport = @('Start-EditorServices')
8480

8581
# Variables to export from this module
8682
VariablesToExport = @()

src/PowerShellEditorServices.Hosting/Commands/InvokeReadLineConstructorCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Microsoft.PowerShell.EditorServices.Commands
1212
/// <summary>
1313
/// The Start-EditorServices command, the conventional entrypoint for PowerShell Editor Services.
1414
/// </summary>
15-
[Cmdlet("__Invoke", "ReadLineConstructor")]
1615
public sealed class InvokeReadLineConstructorCommand : PSCmdlet
1716
{
1817
protected override void EndProcessing()

src/PowerShellEditorServices.Hosting/Commands/InvokeReadLineForEditorServicesCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Microsoft.PowerShell.EditorServices.Commands
1414
/// <summary>
1515
/// The Start-EditorServices command, the conventional entrypoint for PowerShell Editor Services.
1616
/// </summary>
17-
[Cmdlet("__Invoke", "ReadLineForEditorServices")]
1817
public sealed class InvokeReadLineForEditorServicesCommand : PSCmdlet
1918
{
2019
private delegate string ReadLineInvoker(

src/PowerShellEditorServices/Server/PsesDebugServer.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.Extensions.Logging;
1313
using Microsoft.PowerShell.EditorServices.Handlers;
1414
using Microsoft.PowerShell.EditorServices.Services;
15+
using Microsoft.PowerShell.EditorServices.Utility;
1516
using OmniSharp.Extensions.DebugAdapter.Protocol.Serialization;
1617
using OmniSharp.Extensions.JsonRpc;
1718
using OmniSharp.Extensions.LanguageServer.Server;
@@ -28,6 +29,12 @@ internal class PsesDebugServer : IDisposable
2829
/// </summary>
2930
private static int s_hasRunPsrlStaticCtor = 0;
3031

32+
private static readonly Lazy<CmdletInfo> s_lazyInvokeReadLineConstructorCmdletInfo = new Lazy<CmdletInfo>(() =>
33+
{
34+
var type = Type.GetType("Microsoft.PowerShell.EditorServices.Commands.InvokeReadLineConstructorCommand, Microsoft.PowerShell.EditorServices.Hosting");
35+
return new CmdletInfo("__Invoke-ReadLineConstructor", type);
36+
});
37+
3138
private readonly Stream _inputStream;
3239
private readonly Stream _outputStream;
3340
private readonly bool _useTempSession;
@@ -80,8 +87,10 @@ public async Task StartAsync()
8087
// This is only needed for Temp sessions who only have a debug server.
8188
if (_usePSReadLine && _useTempSession && Interlocked.Exchange(ref s_hasRunPsrlStaticCtor, 1) == 0)
8289
{
90+
var command = new PSCommand()
91+
.AddCommand(s_lazyInvokeReadLineConstructorCmdletInfo.Value);
92+
8393
// This must be run synchronously to ensure debugging works
84-
var command = new PSCommand().AddCommand("__Invoke-ReadLineConstructor");
8594
_powerShellContextService
8695
.ExecuteCommandAsync<object>(command, sendOutputToHost: true, sendErrorToHost: true)
8796
.GetAwaiter()

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,15 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
740740

741741

742742
PowerShell shell = this.PromptNest.GetPowerShell(executionOptions.IsReadLine);
743-
shell.Commands = psCommand;
743+
744+
// Due to the following PowerShell bug, we can't just assign shell.Commands to psCommand
745+
// because PowerShell strips out CommandInfo:
746+
// https://github.com/PowerShell/PowerShell/issues/12297
747+
shell.Commands.Clear();
748+
foreach (Command command in psCommand.Commands)
749+
{
750+
shell.Commands.AddCommand(command);
751+
}
744752

745753
// Don't change our SessionState for ReadLine.
746754
if (!executionOptions.IsReadLine)

src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using System;
7+
using System.Collections.Generic;
68
using System.Linq;
7-
using System.Runtime.InteropServices;
9+
using System.Management.Automation.Runspaces;
810
using System.Threading;
911
using System.Threading.Tasks;
10-
using System;
11-
using System.Management.Automation.Runspaces;
12+
using Microsoft.Extensions.Logging;
13+
using Microsoft.PowerShell.EditorServices.Utility;
1214

1315
namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
1416
{
15-
using System.Collections.Generic;
1617
using System.Management.Automation;
17-
using Microsoft.Extensions.Logging;
1818

1919
internal class PSReadLinePromptContext : IPromptContext
2020
{
@@ -35,6 +35,12 @@ internal class PSReadLinePromptContext : IPromptContext
3535
return [Microsoft.PowerShell.PSConsoleReadLine]
3636
}";
3737

38+
private static readonly Lazy<CmdletInfo> s_lazyInvokeReadLineForEditorServicesCmdletInfo = new Lazy<CmdletInfo>(() =>
39+
{
40+
var type = Type.GetType("Microsoft.PowerShell.EditorServices.Commands.InvokeReadLineForEditorServicesCommand, Microsoft.PowerShell.EditorServices.Hosting");
41+
return new CmdletInfo("__Invoke-ReadLineForEditorServices", type);
42+
});
43+
3844
private static ExecutionOptions s_psrlExecutionOptions = new ExecutionOptions
3945
{
4046
WriteErrorsToHost = false,
@@ -129,7 +135,7 @@ public async Task<string> InvokeReadLineAsync(bool isCommandLine, CancellationTo
129135
}
130136

131137
var readLineCommand = new PSCommand()
132-
.AddCommand("__Invoke-ReadLineForEditorServices")
138+
.AddCommand(s_lazyInvokeReadLineForEditorServicesCmdletInfo.Value)
133139
.AddParameter("CancellationToken", _readLineCancellationSource.Token);
134140

135141
IEnumerable<string> readLineResults = await _powerShellContext.ExecuteCommandAsync<string>(
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Linq.Expressions;
8+
using System.Management.Automation;
9+
using System.Management.Automation.Runspaces;
10+
using System.Reflection;
11+
12+
namespace Microsoft.PowerShell.EditorServices.Utility
13+
{
14+
internal static class PSCommandExtensions
15+
{
16+
private static readonly Func<CommandInfo, Command> s_commandCtor;
17+
18+
static PSCommandExtensions()
19+
{
20+
var ctor = typeof(Command).GetConstructor(
21+
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
22+
binder: null,
23+
new[] { typeof(CommandInfo) },
24+
modifiers: null);
25+
26+
ParameterExpression commandInfo = Expression.Parameter(typeof(CommandInfo), nameof(commandInfo));
27+
28+
s_commandCtor = Expression.Lambda<Func<CommandInfo, Command>>(
29+
Expression.New(ctor, commandInfo),
30+
new[] { commandInfo })
31+
.Compile();
32+
}
33+
34+
// PowerShell's missing an API for us to AddCommand using a CommandInfo.
35+
// An issue was filed here: https://github.com/PowerShell/PowerShell/issues/12295
36+
// This works around this by creating a `Command` and passing it into PSCommand.AddCommand(Command command)
37+
internal static PSCommand AddCommand(this PSCommand command, CommandInfo commandInfo)
38+
{
39+
var rsCommand = s_commandCtor(commandInfo);
40+
return command.AddCommand(rsCommand);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)