Skip to content

No warning about module being imported #1258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ Copyright = '(c) 2017 Microsoft. All rights reserved.'
FunctionsToExport = @()

# 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.
CmdletsToExport = @(
'Start-EditorServices',
'__Invoke-ReadLineForEditorServices',
'__Invoke-ReadLineConstructor'
)
CmdletsToExport = @('Start-EditorServices')

# Variables to export from this module
VariablesToExport = @()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Microsoft.PowerShell.EditorServices.Commands
/// <summary>
/// The Start-EditorServices command, the conventional entrypoint for PowerShell Editor Services.
/// </summary>
[Cmdlet("__Invoke", "ReadLineConstructor")]
public sealed class InvokeReadLineConstructorCommand : PSCmdlet
{
protected override void EndProcessing()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Microsoft.PowerShell.EditorServices.Commands
/// <summary>
/// The Start-EditorServices command, the conventional entrypoint for PowerShell Editor Services.
/// </summary>
[Cmdlet("__Invoke", "ReadLineForEditorServices")]
public sealed class InvokeReadLineForEditorServicesCommand : PSCmdlet
{
private delegate string ReadLineInvoker(
Expand Down
8 changes: 7 additions & 1 deletion src/PowerShellEditorServices/Server/PsesDebugServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Handlers;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Utility;
using OmniSharp.Extensions.DebugAdapter.Protocol.Serialization;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Server;
Expand All @@ -28,6 +29,9 @@ internal class PsesDebugServer : IDisposable
/// </summary>
private static int s_hasRunPsrlStaticCtor = 0;

private static readonly Lazy<Type> s_lazyReadLineCmdletType = new Lazy<Type>(() =>
Type.GetType("Microsoft.PowerShell.EditorServices.Commands.InvokeReadLineConstructorCommand, Microsoft.PowerShell.EditorServices.Hosting"));

private readonly Stream _inputStream;
private readonly Stream _outputStream;
private readonly bool _useTempSession;
Expand Down Expand Up @@ -80,8 +84,10 @@ public async Task StartAsync()
// This is only needed for Temp sessions who only have a debug server.
if (_usePSReadLine && _useTempSession && Interlocked.Exchange(ref s_hasRunPsrlStaticCtor, 1) == 0)
{
var command = new PSCommand()
.AddCommand(new CmdletInfo("__Invoke-ReadLineConstructor", s_lazyReadLineCmdletType.Value));

// This must be run synchronously to ensure debugging works
var command = new PSCommand().AddCommand("__Invoke-ReadLineConstructor");
_powerShellContextService
.ExecuteCommandAsync<object>(command, sendOutputToHost: true, sendErrorToHost: true)
.GetAwaiter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,15 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(


PowerShell shell = this.PromptNest.GetPowerShell(executionOptions.IsReadLine);
shell.Commands = psCommand;

// Due to the following PowerShell bug, we can't just assign shell.Commands to psCommand
// because PowerShell strips out CommandInfo:
// https://github.com/PowerShell/PowerShell/issues/12297
shell.Commands.Clear();
foreach (Command command in psCommand.Commands)
{
shell.Commands.AddCommand(command);
}

// Don't change our SessionState for ReadLine.
if (!executionOptions.IsReadLine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Management.Automation.Runspaces;
using System.Threading;
using System.Threading.Tasks;
using System;
using System.Management.Automation.Runspaces;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Utility;

namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
{
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Extensions.Logging;

internal class PSReadLinePromptContext : IPromptContext
{
Expand Down Expand Up @@ -44,6 +44,9 @@ internal class PSReadLinePromptContext : IPromptContext
IsReadLine = true,
};

private static readonly Lazy<Type> s_lazyReadLineCmdletType = new Lazy<Type>(() =>
Type.GetType("Microsoft.PowerShell.EditorServices.Commands.InvokeReadLineForEditorServicesCommand, Microsoft.PowerShell.EditorServices.Hosting"));

private readonly PowerShellContextService _powerShellContext;

private readonly PromptNest _promptNest;
Expand Down Expand Up @@ -129,7 +132,7 @@ public async Task<string> InvokeReadLineAsync(bool isCommandLine, CancellationTo
}

var readLineCommand = new PSCommand()
.AddCommand("__Invoke-ReadLineForEditorServices")
.AddCommand(new CmdletInfo("__Invoke-ReadLineForEditorServices", s_lazyReadLineCmdletType.Value))
.AddParameter("CancellationToken", _readLineCancellationSource.Token);

IEnumerable<string> readLineResults = await _powerShellContext.ExecuteCommandAsync<string>(
Expand Down
32 changes: 32 additions & 0 deletions src/PowerShellEditorServices/Utility/PSCommandExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;

namespace Microsoft.PowerShell.EditorServices.Utility
{
internal static class PSCommandExtensions
{
private static ConstructorInfo s_commandCtor =
typeof(Command).GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
binder: null,
new[] { typeof(CommandInfo) },
modifiers: null);

// PowerShell's missing an API for us to AddCommand using a CommandInfo.
// An issue was filed here: https://github.com/PowerShell/PowerShell/issues/12295
// This works around this by creating a `Command` and passing it into PSCommand.AddCommand(Command command)
internal static PSCommand AddCommand(this PSCommand command, CommandInfo commandInfo)
{
var rsCommand = (Command) s_commandCtor
.Invoke(new object[] { commandInfo });

return command.AddCommand(rsCommand);
}
}
}